/** * Admin user "perform with checked" action to assign user to or remove users from groups * * @param $input * @return array * @throws Exception * @throws Services_Exception * @throws Services_Exception_BadRequest * @throws Services_Exception_Denied */ function action_manage_groups($input) { Services_Exception_Denied::checkGlobal('admin_users'); $check = Services_Exception_BadRequest::checkAccess(); //first pass - show confirm popup if (!empty($check['ticket'])) { $selected = $input->asArray('checked'); if (count($selected) > 0) { //provide redirect if js is not enabled $referer = Services_Utilities_Controller::noJsPath(); //remove from group icon clicked for a specific user if (isset($input['groupremove'])) { $items = $input->asArray('groupremove'); return ['FORWARD' => ['controller' => 'access', 'action' => 'confirm', 'title' => tra('Please confirm removal from group'), 'confirmAction' => $input->action->word(), 'confirmController' => 'user', 'customMsg' => tr('Are you sure you want to remove user %0 from the following group:', $selected[0]), 'items' => $items, 'extra' => ['add_remove' => 'remove', 'user' => $selected[0], 'referer' => $referer], 'ticket' => $check['ticket'], 'modal' => '1']]; //selected users to be added or removed from selected groups groups } else { $all_groups = $this->lib->list_all_groups(); $countgrps = count($all_groups) < 21 ? count($all_groups) : 20; $users = $input->asArray('checked'); if (count($users) == 1) { $customMsg = tra('For this user:'******'tiki')->get_user_groups($users[0]); } else { $customMsg = tra('For these selected users:'); $userGroups = ''; } return ['title' => tra('Change group assignments for selected users'), 'confirmAction' => $input->action->word(), 'confirmController' => 'user', 'customMsg' => $customMsg, 'all_groups' => $all_groups, 'countgrps' => $countgrps, 'items' => $users, 'extra' => ['referer' => $referer], 'ticket' => $check['ticket'], 'modal' => '1', 'confirm' => 'y', 'userGroups' => str_replace(['\'', '&'], ['%39;', '%26'], json_encode($userGroups))]; } } else { throw new Services_Exception(tra('No users were selected. Please select one or more users.'), 409); } //after confirm submit - perform action and return success feedback } elseif ($check === true && $_SERVER['REQUEST_METHOD'] === 'POST') { $extra = json_decode($input['extra'], true); //selected users added or removed from selected groups if (isset($input['checked_groups'])) { $groups = $input->asArray('checked_groups'); $users = json_decode($input['items'], true); $add_remove = $input->add_remove->word(); //single user removed from a particular group } else { $groups = json_decode($input['items'], true); $users[] = $extra['user']; $add_remove = $extra['add_remove']; } if (!empty($users) && !empty($groups)) { global $user; $logslib = TikiLib::lib('logs'); $userGroups = $this->lib->get_user_groups_inclusion($user); $permname = 'group_' . $add_remove . '_member'; $groupperm = Perms::get()->{$permname}; $userperm = Perms::get()->group_join; foreach ($users as $assign_user) { foreach ($groups as $group) { if ($groupperm || array_key_exists($group, $userGroups) && $userperm) { if ($add_remove === 'add') { $res = $this->lib->assign_user_to_group($assign_user, $group); if ($res) { $logmsg = sprintf(tra('%s %s assigned to %s %s.'), tra('user'), $assign_user, tra('group'), $group); $logslib->add_log('adminusers', $logmsg, $user); } else { throw new Services_Exception(tra('An error occurred. The group assignment failed'), 400); } } elseif ($add_remove === 'remove') { $this->lib->remove_user_from_group($assign_user, $group); $logmsg = sprintf(tra('%s %s removed from %s %s.'), tra('user'), $assign_user, tra('group'), $group); $logslib->add_log('adminusers', $logmsg, $user); } } else { throw new Services_Exception_Denied(); } } } //return to page //if javascript is not enabled if (!empty($extra['referer'])) { $this->access->redirect($extra['referer'], tra('Selected user(s) group assignment(s) changed'), null, 'feedback'); } if (count($users) === 1) { $msg = tra('The following user:'******'Has'; } else { $msg = tra('The following users:'); $helper = 'Have'; } $verb = $add_remove == 'add' ? 'added to' : 'removed from'; $grpcnt = count($groups) === 1 ? 'group' : 'groups'; $toMsg = tr('%0 been %1 the following %2:', tra($helper), tra($verb), tra($grpcnt)); return ['extra' => 'post', 'feedback' => ['ajaxtype' => 'feedback', 'ajaxheading' => tra('Success'), 'ajaxitems' => $users, 'ajaxmsg' => $msg, 'ajaxtoMsg' => $toMsg, 'ajaxtoList' => $groups, 'modal' => '1']]; } else { throw new Services_Exception(tra('No groups were selected. Please select one or more groups.'), 409); } } }