diff options
author | Philip Häusler <msquare@notrademark.de> | 2014-05-13 15:51:45 +0200 |
---|---|---|
committer | Philip Häusler <msquare@notrademark.de> | 2014-05-13 15:51:45 +0200 |
commit | f1145b8a5e8630e0180d35ff1ced2e934b872f01 (patch) | |
tree | c99e7b7a5f6e1ab3839777dbd8095f83a1c94f20 /includes | |
parent | e107dff1ce417df779733667ad81d60755582779 (diff) |
implemented team/angeltype coordinators
Diffstat (limited to 'includes')
-rw-r--r-- | includes/controller/angeltypes_controller.php | 12 | ||||
-rw-r--r-- | includes/controller/user_angeltypes_controller.php | 200 | ||||
-rw-r--r-- | includes/model/AngelType_model.php | 3 | ||||
-rw-r--r-- | includes/model/UserAngelTypes_model.php | 37 | ||||
-rw-r--r-- | includes/model/User_model.php | 26 | ||||
-rw-r--r-- | includes/view/AngelTypes_view.php | 52 | ||||
-rw-r--r-- | includes/view/UserAngelTypes_view.php | 30 |
7 files changed, 299 insertions, 61 deletions
diff --git a/includes/controller/angeltypes_controller.php b/includes/controller/angeltypes_controller.php index bcd7696e..6b94545b 100644 --- a/includes/controller/angeltypes_controller.php +++ b/includes/controller/angeltypes_controller.php @@ -174,7 +174,7 @@ function angeltype_controller() { return array( sprintf(_("Team %s"), $angeltype['name']), - AngelType_view($angeltype, $members, $user_angeltype, in_array('admin_user_angeltypes', $privileges), in_array('admin_angel_types', $privileges)) + AngelType_view($angeltype, $members, $user_angeltype, in_array('admin_user_angeltypes', $privileges) || $user_angeltype['coordinator'], in_array('admin_angel_types', $privileges)) ); } @@ -201,18 +201,10 @@ function angeltypes_list_controller() { $actions[] = '<a class="delete" href="' . page_link_to('angeltypes') . '&action=delete&angeltype_id=' . $angeltype['id'] . '">' . _("delete") . '</a>'; } - $angeltype['membership'] = ""; + $angeltype['membership'] = AngelType_render_membership($angeltype); if ($angeltype['user_angeltype_id'] != null) { - if ($angeltype['restricted']) { - if ($angeltype['confirm_user_id'] == null) - $angeltype['membership'] = '<img src="pic/icons/lock.png" alt="' . _("Unconfirmed") . '" title="' . _("Unconfirmed") . '"> ' . _("Unconfirmed"); - else - $angeltype['membership'] = '<img src="pic/icons/tick.png" alt="' . _("Member") . '" title="' . _("Member") . '"> ' . _("Member"); - } else - $angeltype['membership'] = '<img src="pic/icons/tick.png" alt="' . _("Member") . '" title="' . _("Member") . '"> ' . _("Member"); $actions[] = '<a class="cancel" href="' . page_link_to('user_angeltypes') . '&action=delete&user_angeltype_id=' . $angeltype['user_angeltype_id'] . '">' . _("leave") . '</a>'; } else { - $angeltype['membership'] = '<img src="pic/icons/cross.png" alt="" title="">'; $actions[] = '<a class="add" href="' . page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'] . '">' . _("join") . '</a>'; } diff --git a/includes/controller/user_angeltypes_controller.php b/includes/controller/user_angeltypes_controller.php index b955fe6d..095b35f5 100644 --- a/includes/controller/user_angeltypes_controller.php +++ b/includes/controller/user_angeltypes_controller.php @@ -6,11 +6,6 @@ function user_angeltypes_delete_all_controller() { global $user, $privileges; - if (! in_array('admin_user_angeltypes', $privileges)) { - error(_("You are not allowed to delete all users for this angeltype.")); - redirect(page_link_to('angeltypes')); - } - if (! isset($_REQUEST['angeltype_id'])) { error(_("Angeltype doesn't exist.")); redirect(page_link_to('angeltypes')); @@ -24,6 +19,11 @@ function user_angeltypes_delete_all_controller() { redirect(page_link_to('angeltypes')); } + if (! User_is_AngelType_coordinator($user, $angeltype)) { + error(_("You are not allowed to delete all users for this angeltype.")); + redirect(page_link_to('angeltypes')); + } + if (isset($_REQUEST['confirmed'])) { $result = UserAngelTypes_delete_all($angeltype['id']); if ($result === false) @@ -46,11 +46,6 @@ function user_angeltypes_delete_all_controller() { function user_angeltypes_confirm_all_controller() { global $user, $privileges; - if (! in_array('admin_user_angeltypes', $privileges)) { - error(_("You are not allowed to confirm all users for this angeltype.")); - redirect(page_link_to('angeltypes')); - } - if (! isset($_REQUEST['angeltype_id'])) { error(_("Angeltype doesn't exist.")); redirect(page_link_to('angeltypes')); @@ -64,6 +59,19 @@ function user_angeltypes_confirm_all_controller() { redirect(page_link_to('angeltypes')); } + $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); + if ($user_angeltype === false) + engelsystem_error("Unable to load user angeltype."); + if ($user_angeltype == null) { + error(_("User angeltype doesn't exist.")); + redirect(page_link_to('angeltypes')); + } + + if (! in_array('admin_user_angeltypes', $privileges) && ! $user_angeltype['coordinator']) { + error(_("You are not allowed to confirm all users for this angeltype.")); + redirect(page_link_to('angeltypes')); + } + if (isset($_REQUEST['confirmed'])) { $result = UserAngelTypes_confirm_all($angeltype['id'], $user); if ($result === false) @@ -86,11 +94,6 @@ function user_angeltypes_confirm_all_controller() { function user_angeltype_confirm_controller() { global $user, $privileges; - if (! in_array('admin_user_angeltypes', $privileges)) { - error(_("You are not allowed to confirm this users angeltype.")); - redirect(page_link_to('angeltypes')); - } - if (! isset($_REQUEST['user_angeltype_id'])) { error(_("User angeltype doesn't exist.")); redirect(page_link_to('angeltypes')); @@ -112,6 +115,11 @@ function user_angeltype_confirm_controller() { redirect(page_link_to('angeltypes')); } + if (! User_is_AngelType_coordinator($user, $angeltype)) { + error(_("You are not allowed to confirm this users angeltype.")); + redirect(page_link_to('angeltypes')); + } + $user_source = User($user_angeltype['user_id']); if ($user_source === false) engelsystem_error("Unable to load user."); @@ -171,7 +179,7 @@ function user_angeltype_delete_controller() { redirect(page_link_to('angeltypes')); } - if ($user['UID'] != $user_angeltype['user_id'] && ! in_array('admin_user_angeltypes', $privileges)) { + if ($user['UID'] != $user_angeltype['user_id'] && ! User_is_AngelType_coordinator($user, $angeltype)) { error(_("You are not allowed to delete this users angeltype.")); redirect(page_link_to('angeltypes')); } @@ -198,7 +206,65 @@ function user_angeltype_delete_controller() { * Update an UserAngelType. */ function user_angeltype_update_controller() { - + global $user, $privileges; + + if (! in_array('admin_angel_types', $privileges)) { + error(_("You are not allowed to set coordinator rights.")); + redirect(page_link_to('angeltypes')); + } + + if (! isset($_REQUEST['user_angeltype_id'])) { + error(_("User angeltype doesn't exist.")); + redirect(page_link_to('angeltypes')); + } + + if (isset($_REQUEST['coordinator']) && preg_match("/^[01]$/", $_REQUEST['coordinator'])) + $coordinator = $_REQUEST['coordinator'] == "1"; + else { + error(_("No coordinator update given.")); + redirect(page_link_to('angeltypes')); + } + + $user_angeltype = UserAngelType($_REQUEST['user_angeltype_id']); + if ($user_angeltype === false) + engelsystem_error("Unable to load user angeltype."); + if ($user_angeltype == null) { + error(_("User angeltype doesn't exist.")); + redirect(page_link_to('angeltypes')); + } + + $angeltype = AngelType($user_angeltype['angeltype_id']); + if ($angeltype === false) + engelsystem_error("Unable to load angeltype."); + if ($angeltype == null) { + error(_("Angeltype doesn't exist.")); + redirect(page_link_to('angeltypes')); + } + + $user_source = User($user_angeltype['user_id']); + if ($user_source === false) + engelsystem_error("Unable to load user."); + if ($user_source == null) { + error(_("User doesn't exist.")); + redirect(page_link_to('angeltypes')); + } + + if (isset($_REQUEST['confirmed'])) { + $result = UserAngelType_update($user_angeltype['id'], $coordinator); + if ($result === false) + engelsystem_error("Unable to update coordinator rights."); + + $success_message = sprintf($coordinator ? _("Added coordinator rights for %s to %s.") : _("Removed coordinator rights for %s from %s."), $angeltype['name'], User_Nick_render($user_source)); + engelsystem_log($success_message); + success($success_message); + + redirect(page_link_to('angeltypes') . '&action=view&angeltype_id=' . $angeltype['id']); + } + + return array( + $coordinator ? _("Add coordinator rights") : _("Remove coordinator rights"), + UserAngelType_update_view($user_angeltype, $user, $angeltype, $coordinator) + ); } /** @@ -220,38 +286,84 @@ function user_angeltype_add_controller() { redirect(page_link_to('angeltypes')); } - $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); - if ($user_angeltype === false) - engelsystem_error("Unable to load user angeltype."); - if ($user_angeltype != null) { - error(sprintf(_("User is already an %s."), $angeltype['name'])); - redirect(page_link_to('angeltypes')); - } - - if (isset($_REQUEST['confirmed'])) { - $user_angeltype_id = UserAngelType_create($user, $angeltype); - if ($user_angeltype_id === false) - engelsystem_error("Unable to create user angeltype."); + if (User_is_AngelType_coordinator($user, $angeltype)) { + // Allow to add any user + $user_id = $user['UID']; - $success_message = sprintf(_("User %s joined %s."), User_Nick_render($user), $angeltype['name']); - engelsystem_log($success_message); - success($success_message); + $users_source = Users_by_angeltype_inverted($angeltype); + if ($users_source === false) + engelsystem_error("Unable to load users."); - if (in_array('admin_user_angeltypes', $privileges)) { - $result = UserAngelType_confirm($user_angeltype_id, $user); - if ($result === false) - engelsystem_error("Unable to confirm user angeltype."); - $success_message = sprintf(_("User %s confirmed as %s."), User_Nick_render($user), $angeltype['name']); - engelsystem_log($success_message); + if (isset($_REQUEST['submit'])) { + $ok = true; + + if (isset($_REQUEST['user_id']) && in_array($_REQUEST['user_id'], array_map(function ($user) { + return $user['UID']; + }, $users_source))) + $user_id = $_REQUEST['user_id']; + else { + $ok = false; + error(_("Please select a user.")); + } + + if ($ok) { + foreach ($users_source as $user_source) + if ($user_source['UID'] == $user_id) { + $user_angeltype_id = UserAngelType_create($user_source, $angeltype); + if ($user_angeltype_id === false) + engelsystem_error("Unable to create user angeltype."); + + engelsystem_log(sprintf("User %s added to %s.", User_Nick_render($user_source), $angeltype['name'])); + success(sprintf(_("User %s added to %s."), User_Nick_render($user_source), $angeltype['name'])); + + $result = UserAngelType_confirm($user_angeltype_id, $user_source); + if ($result === false) + engelsystem_error("Unable to confirm user angeltype."); + engelsystem_log(sprintf("User %s confirmed as %s.", User_Nick_render($user), $angeltype['name'])); + + redirect(page_link_to('angeltypes') . '&action=view&angeltype_id=' . $angeltype['id']); + } + } } - redirect(page_link_to('angeltypes') . '&action=view&angeltype_id=' . $angeltype['id']); + return array( + _("Add user to angeltype"), + UserAngelType_add_view($angeltype, $users_source, $user_id) + ); + } else { + // Allow only me + $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); + if ($user_angeltype === false) + engelsystem_error("Unable to load user angeltype."); + if ($user_angeltype != null) { + error(sprintf(_("You are already a %s."), $angeltype['name'])); + redirect(page_link_to('angeltypes')); + } + + if (isset($_REQUEST['confirmed'])) { + $user_angeltype_id = UserAngelType_create($user, $angeltype); + if ($user_angeltype_id === false) + engelsystem_error("Unable to create user angeltype."); + + $success_message = sprintf(_("You joined %s."), $angeltype['name']); + engelsystem_log(sprintf("User %s joined %s.", User_Nick_render($user), $angeltype['name'])); + success($success_message); + + if (in_array('admin_user_angeltypes', $privileges)) { + $result = UserAngelType_confirm($user_angeltype_id, $user); + if ($result === false) + engelsystem_error("Unable to confirm user angeltype."); + engelsystem_log(sprintf("User %s confirmed as %s.", User_Nick_render($user), $angeltype['name'])); + } + + redirect(page_link_to('angeltypes') . '&action=view&angeltype_id=' . $angeltype['id']); + } + + return array( + sprintf(_("Become a %s"), $angeltype['name']), + UserAngelType_join_view($user, $angeltype) + ); } - - return array( - _("Add user to angeltype"), - UserAngelType_add_view($user, $angeltype) - ); } /** diff --git a/includes/model/AngelType_model.php b/includes/model/AngelType_model.php index 832a3236..df5e6ae1 100644 --- a/includes/model/AngelType_model.php +++ b/includes/model/AngelType_model.php @@ -92,7 +92,8 @@ function AngelTypes_with_user($user) { return sql_select(" SELECT `AngelTypes`.*, `UserAngelTypes`.`id` as `user_angeltype_id`, - `UserAngelTypes`.`confirm_user_id` + `UserAngelTypes`.`confirm_user_id`, + `UserAngelTypes`.`coordinator` FROM `AngelTypes` LEFT JOIN `UserAngelTypes` ON `AngelTypes`.`id`=`UserAngelTypes`.`angeltype_id` AND `UserAngelTypes`.`user_id`=" . $user['UID'] . " diff --git a/includes/model/UserAngelTypes_model.php b/includes/model/UserAngelTypes_model.php index 4987eddd..b9b44032 100644 --- a/includes/model/UserAngelTypes_model.php +++ b/includes/model/UserAngelTypes_model.php @@ -1,9 +1,42 @@ <?php +/** + * User angeltypes model + */ + +/** + * Returns true if user is angeltype coordinator or has privilege admin_user_angeltypes. + * + * @param User $user + * @param AngelType $angeltype + */ +function User_is_AngelType_coordinator($user, $angeltype) { + return (sql_num_query(" + SELECT `id` + FROM `UserAngelTypes` + WHERE `user_id`=" . sql_escape($user['UID']) . " + AND `angeltype_id`=" . sql_escape($angeltype['id']) . " + AND `coordinator`=TRUE + LIMIT 1") > 0) || in_array('admin_user_angeltypes', privileges_for_user($user['UID'])); +} + +/** + * Add or remove coordinator rights. + * + * @param int $user_angeltype_id + * @param bool $coordinator + */ +function UserAngelType_update($user_angeltype_id, $coordinator) { + return sql_query(" + UPDATE `UserAngelTypes` + SET `coordinator`=" . ($coordinator ? 'TRUE' : 'FALSE') . " + WHERE `id`=" . sql_escape($user_angeltype_id) . " + LIMIT 1"); +} /** * Delete all unconfirmed UserAngelTypes for given Angeltype. - * - * @param int $angeltype_id + * + * @param int $angeltype_id */ function UserAngelTypes_delete_all($angeltype_id) { return sql_query(" diff --git a/includes/model/User_model.php b/includes/model/User_model.php index d369ea3e..c8c48e6c 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -1,12 +1,34 @@ <?php +/** + * User model + */ + +/** + * Returns all users that are not member of given angeltype. + * + * @param Angeltype $angeltype + */ +function Users_by_angeltype_inverted($angeltype) { + return sql_select(" + SELECT `User`.* + FROM `User` + LEFT JOIN `UserAngelTypes` ON (`User`.`UID`=`UserAngelTypes`.`user_id` AND `angeltype_id`=" . sql_escape($angeltype['id']) . ") + WHERE `UserAngelTypes`.`id` IS NULL + ORDER BY `Nick`"); +} /** * Returns all members of given angeltype. - * @param Angeltype $angeltype + * + * @param Angeltype $angeltype */ function Users_by_angeltype($angeltype) { return sql_select(" - SELECT `User`.*, `UserAngelTypes`.`id` as `user_angeltype_id`, `UserAngelTypes`.`confirm_user_id` + SELECT + `User`.*, + `UserAngelTypes`.`id` as `user_angeltype_id`, + `UserAngelTypes`.`confirm_user_id`, + `UserAngelTypes`.`coordinator` FROM `User` JOIN `UserAngelTypes` ON `User`.`UID`=`UserAngelTypes`.`user_id` WHERE `UserAngelTypes`.`angeltype_id`=" . sql_escape($angeltype['id']) . " diff --git a/includes/view/AngelTypes_view.php b/includes/view/AngelTypes_view.php index d71bbbfd..c9293903 100644 --- a/includes/view/AngelTypes_view.php +++ b/includes/view/AngelTypes_view.php @@ -1,4 +1,34 @@ <?php +/** + * AngelTypes + */ + +/** + * Render angeltype membership state + * + * @param UserAngelType $user_angeltype + * UserAngelType and AngelType + * @return string + */ +function AngelType_render_membership($user_angeltype) { + $membership = ""; + if ($user_angeltype['user_angeltype_id'] != null) { + if ($user_angeltype['restricted']) { + if ($user_angeltype['confirm_user_id'] == null) + $membership = '<img src="pic/icons/lock.png" alt="' . _("Unconfirmed") . '" title="' . _("Unconfirmed") . '"> ' . _("Unconfirmed"); + elseif ($user_angeltype['coordinator']) + $membership = '<img src="pic/icons/tick.png" alt="' . _("Coordinator") . '" title="' . _("Coordinator") . '"> ' . _("Coordinator"); + else + $membership = '<img src="pic/icons/tick.png" alt="' . _("Member") . '" title="' . _("Member") . '"> ' . _("Member"); + } elseif ($user_angeltype['coordinator']) + $membership = '<img src="pic/icons/tick.png" alt="' . _("Coordinator") . '" title="' . _("Coordinator") . '"> ' . _("Coordinator"); + else + $membership = '<img src="pic/icons/tick.png" alt="' . _("Member") . '" title="' . _("Member") . '"> ' . _("Member"); + } else { + $membership = '<img src="pic/icons/cross.png" alt="" title="">'; + } + return $membership; +} function AngelType_delete_view($angeltype) { return page(array( @@ -56,7 +86,7 @@ function AngelType_view($angeltype, $members, $user_angeltype, $admin_user_angel // Team-Coordinators list missing - $page[] = '<h3>' . _("Members") . '</h3>'; + $coordinators = array(); $members_confirmed = array(); $members_unconfirmed = array(); foreach ($members as $member) { @@ -67,14 +97,34 @@ function AngelType_view($angeltype, $members, $user_angeltype, $admin_user_angel '<a href="' . page_link_to('user_angeltypes') . '&action=delete&user_angeltype_id=' . $member['user_angeltype_id'] . '" class="cancel">' . _("deny") . '</a>' )); $members_unconfirmed[] = $member; + } elseif ($member['coordinator']) { + if ($admin_angeltypes) + $member['actions'] = '<a href="' . page_link_to('user_angeltypes') . '&action=update&user_angeltype_id=' . $member['user_angeltype_id'] . '&coordinator=0" class="cancel">' . _("Remove coordinator rights") . '</a>'; + else + $member['actions'] = ''; + $coordinators[] = $member; } else { if ($admin_user_angeltypes) $member['actions'] = join(" ", array( + $admin_angeltypes ? '<a href="' . page_link_to('user_angeltypes') . '&action=update&user_angeltype_id=' . $member['user_angeltype_id'] . '&coordinator=1" class="add">' . _("Add coordinator rights") . '</a>' : '', '<a href="' . page_link_to('user_angeltypes') . '&action=delete&user_angeltype_id=' . $member['user_angeltype_id'] . '" class="cancel">' . _("remove") . '</a>' )); $members_confirmed[] = $member; } } + if (count($coordinators) > 0) { + $page[] = '<h3>' . _("Coordinators") . '</h3>'; + $page[] = table(array( + 'Nick' => _("Nick"), + 'DECT' => _("DECT"), + 'actions' => "" + ), $coordinators); + } + $page[] = '<h3>' . _("Members") . '</h3>'; + if ($admin_user_angeltypes) + $page[] = buttons(array( + button(page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'], _("Add"), 'add') + )); $page[] = table(array( 'Nick' => _("Nick"), 'DECT' => _("DECT"), diff --git a/includes/view/UserAngelTypes_view.php b/includes/view/UserAngelTypes_view.php index ed825c04..68bb1ec9 100644 --- a/includes/view/UserAngelTypes_view.php +++ b/includes/view/UserAngelTypes_view.php @@ -1,4 +1,14 @@ <?php +function UserAngelType_update_view($user_angeltype, $user, $angeltype, $coordinator) { + return page(array( + msg(), + info(sprintf($coordinator ? _("Do you really want to add coordinator rights for %s to %s?") : _("Do you really want to remove coordinator rights for %s from %s?"), $angeltype['name'], User_Nick_render($user)), true), + buttons(array( + button(page_link_to('angeltypes') . '&action=view&angeltype_id=' . $angeltype['id'], _("cancel"), 'cancel'), + button(page_link_to('user_angeltypes') . '&action=update&user_angeltype_id=' . $user_angeltype['id'] . '&coordinator=' . ($coordinator ? '1' : '0') . '&confirmed', _("yes"), 'ok') + )) + )); +} function UserAngelTypes_delete_all_view($angeltype) { return page(array( @@ -44,7 +54,25 @@ function UserAngelType_delete_view($user_angeltype, $user, $angeltype) { )); } -function UserAngelType_add_view($user, $angeltype) { +function UserAngelType_add_view($angeltype, $users_source, $user_id) { + $users = array(); + foreach ($users_source as $user_source) + $users[$user_source['UID']] = User_Nick_render($user_source); + + return page(array( + msg(), + buttons(array( + button(page_link_to('angeltypes') . '&action=view&angeltype_id=' . $angeltype['id'], _("back"), 'back') + )), + form(array( + form_info(_("Angeltype"), $angeltype['name']), + form_select('user_id', _("User"), $users, $user_id), + form_submit('submit', _("Add")) + )) + )); +} + +function UserAngelType_join_view($user, $angeltype) { return page(array( msg(), info(sprintf(_("Do you really want to add %s to %s?"), User_Nick_render($user), $angeltype['name']), true), |