/** * Sets the Members of a group */ public function SetMembers() { $db =& $this->db; $response = new ResponseManager(); $groupObject = new UserGroup($db); $groupId = Kit::GetParam('GroupID', _REQUEST, _INT); $users = Kit::GetParam('UserID', _POST, _ARRAY, array()); // We will receive a list of users from the UI which are in the "assign column" at the time the form is // submitted. // We want to go through and unlink any users that are NOT in that list, but that the current user has access // to edit. // We want to add any users that are in that list (but aren't already assigned) // All users that this session has access to if (!($allUsers = $this->user->userList())) { trigger_error(__('Error getting all users'), E_USER_ERROR); } // Convert to an array of ID's for convenience $allUserIds = array_map(function ($array) { return $array['userid']; }, $allUsers); // Users in group $usersAssigned = UserData::entries(null, array('groupIds' => array($groupId))); Debug::Audit('All userIds we want to assign: ' . var_export($users, true)); Debug::Audit('All userIds we have access to: ' . var_export($allUserIds, true)); foreach ($usersAssigned as $user) { /* @var Userdata $user */ // Did this session have permission to do anything to this user? // If not, move on if (!in_array($user->userId, $allUserIds)) { continue; } Debug::Audit('Logged in user has permission to make changes to this assigned user ' . $user->userId); // Is this user in the provided list of users? if (in_array($user->userId, $users)) { // This user is already assigned, so we remove it from the $users array Debug::Audit('This user is already assigned ' . $user->userId); if (($key = array_search($user->userId, $users)) !== false) { unset($users[$key]); } } else { Debug::Audit('This user is assigned, but not in the list of assignments ' . $user->userId); // It isn't therefore needs to be removed if (!$groupObject->Unlink($groupId, $user->userId)) { trigger_error($groupObject->GetErrorMessage(), E_USER_ERROR); } } } Debug::Audit('All userIds we want to assign after sorting: ' . var_export($users, true)); // Add any users that are still missing after tha assignment process foreach ($users as $userId) { Debug::Audit('User was missing, linking them: ' . $userId); // Add any that are missing if (!$groupObject->Link($groupId, $userId)) { trigger_error($groupObject->GetErrorMessage(), E_USER_ERROR); } } $response->SetFormSubmitResponse(__('Group membership set'), false); $response->Respond(); }