From 0e7f6317957ea51e02e7c6deabb7f004c941966b Mon Sep 17 00:00:00 2001 From: Axel Stohn Date: Sun, 13 Mar 2022 10:16:21 -0700 Subject: [PATCH] Changes for add/delete members from group --- Controller/GrouperGroupsController.php | 79 +++++++++++++++++++++ Lib/GrouperApiAccess.php | 76 +++++++++++++++++++- Model/GrouperGroup.php | 58 +++++++++++++++ View/Elements/Components/subscriberList.ctp | 6 +- 4 files changed, 213 insertions(+), 6 deletions(-) diff --git a/Controller/GrouperGroupsController.php b/Controller/GrouperGroupsController.php index 4f353d2..eccc6e6 100644 --- a/Controller/GrouperGroupsController.php +++ b/Controller/GrouperGroupsController.php @@ -148,6 +148,83 @@ public function groupSubscribers() { $this->set('_serialize', 'subscribers'); } + /** + * Add a new member to a group + * Called from all pages via AJAX call + * + */ + public function addSubscriber() { + $groupName = urldecode($this->request->query['group']); + $addUserId = urldecode($this->request->query['userId']); + + if ($this->request->is('ajax')) { + $ajax = true; + } + + //Need to see if coming from AdHoc or from a WG (Working Group) + if (strpos($groupName, ':') === false ) { + $groupNameFormatted = 'ref:incommon-collab:' . $groupName . ':users'; + } else { + $groupNameFormatted = $groupName; + } + + //Set initial + $scope = [ + 'groupName' => $groupNameFormatted, + 'addUserId' => $addUserId + ]; + + try { + $subscribers = $this->GrouperGroup->addMemberToGroup($scope, $this->userId); + + } catch (Exception $e) { + CakeLog::write('error', __METHOD__ . ': ' . var_export($e->getMessage(), true)); + + $this->Flash->set(_txt('pl.grouperlite.message.flash.group-detail-members-failed'), array('key' => 'error')); + } + + $this->set(compact('subscribers')); + $this->set('_serialize', 'subscribers'); + } + + /** + * Remove a member from a group + * Called from all pages via AJAX call + * + */ + public function removeSubscriber() { + $groupName = urldecode($this->request->query['group']); + $remUserId = urldecode($this->request->query['userId']); + + if ($this->request->is('ajax')) { + $ajax = true; + } + + //Need to see if coming from AdHoc or from a WG (Working Group) + if (strpos($groupName, ':') === false ) { + $groupNameFormatted = 'ref:incommon-collab:' . $groupName . ':users'; + } else { + $groupNameFormatted = $groupName; + } + + //Set initial + $scope = [ + 'groupName' => $groupNameFormatted, + 'remUserId' => $remUserId + ]; + + try { + $subscribers = $this->GrouperGroup->removeMemberToGroup($scope, $this->userId); + + } catch (Exception $e) { + CakeLog::write('error', __METHOD__ . ': ' . var_export($e->getMessage(), true)); + + $this->Flash->set(_txt('pl.grouperlite.message.flash.group-detail-members-failed'), array('key' => 'error')); + } + + $this->set(compact('subscribers')); + $this->set('_serialize', 'subscribers'); + } /** * Listing of all Grouper Groups owned/admin by User Or search those Grouper Groups @@ -538,6 +615,8 @@ function isAuthorized() { $p['groupowner'] = true; $p['groupmember'] = true; $p['groupSubscribers'] = true; + $p['addSubscriber'] = true; + $p['removeSubscriber'] = true; $p['groupoptin'] = true; $p['emaillistsoptin'] = true; $p['emaillistsmember'] = true; diff --git a/Lib/GrouperApiAccess.php b/Lib/GrouperApiAccess.php index 86cab1a..d52c26c 100644 --- a/Lib/GrouperApiAccess.php +++ b/Lib/GrouperApiAccess.php @@ -84,9 +84,8 @@ public function getGrouperMemberOfGroups(array $queryData) { //Build request logic $userId = $queryData['userId']; - $connectionUrl = "{$this->config['fullUrl']}/subjects/$userId/groups"; - // $connectionUrl = "{$this->config['fullUrl']}/subjects/$userId/groups?"; - // $connectionUrl .= "wsLiteObjectType=WsRestGetGroupsLiteRequest&actAsSubjectSourceId=ldap&actAsSubjectId=$userId"; + $connectionUrl = "{$this->config['fullUrl']}/subjects/$userId/groups?"; + $connectionUrl .= "wsLiteObjectType=WsRestGetGroupsLiteRequest&actAsSubjectSourceId=ldap&actAsSubjectId=$userId"; try { $results = $this->http->sendRequest('GET', $connectionUrl); @@ -297,6 +296,77 @@ public function getMembersInGroup(array $queryData) { return array(); } + /** + * Add a member to a specific Grouper Group + * + * @param array $queryData Array of conditions for querying + * @return array Listing of Members belonging to Grouper Group + * @throws GrouperLiteException + */ + public function addMemberToGroup(array $queryData) { + + try { + $groupName = $queryData['groupName']; + + //Build request logic + $usersToShow = array( + "WsRestAddMemberRequest" => array( + "subjectLookups" => array( + array("subjectId" => $queryData['addUserId']), + ), + "replaceAllExisting" => "F", + "actAsSubjectLookup" => array( + "subjectId" => $queryData['userId'] + ) + ) + ); + + $this->http->setHeader(array('Content-Type' => 'application/json', 'Accept' => 'application/json')); + $connectionUrl = "{$this->config['fullUrl']}/groups/$groupName/members"; + + $results = $this->http->sendRequest('PUT', $connectionUrl, json_encode($usersToShow)); + + // Parse out relevant records to send front end + if (isset($results['WsAddMemberResults']['results'][0]['resultMetadata']) && $results['WsAddMemberResults']['results'][0]['resultMetadata'] != NULL) { + return $results['WsAddMemberResults']['results'][0]['resultMetadata']['resultCode']; + } + } catch (Exception $e) { + CakeLog::write('error', __METHOD__ . ': An error occurred'); + throw $e; + } + + return array(); + } + + /** + * Remove a member from a specific Grouper Group + * + * @param array $queryData Array of conditions for querying + * @return array Listing of Members belonging to Grouper Group + * @throws GrouperLiteException + */ + public function removeMemberToGroup(array $queryData) { + + try { + $groupName = $queryData['groupName']; + $remUserId = $queryData['remUserId']; + + $this->http->setHeader(array('Content-Type' => 'application/json', 'Accept' => 'application/json')); + $connectionUrl = "{$this->config['fullUrl']}/groups/$groupName/members/$remUserId"; + $results = $this->http->sendRequest('PUT', $connectionUrl); + + // Parse out relevant records to send front end + if (isset($results['WsGetMembersResults']['results'][0]['wsSubjects']) && $results['WsGetMembersResults']['results'][0]['wsSubjects'] != NULL) { + return $results['WsGetMembersResults']['results'][0]['wsSubjects']; + } + } catch (Exception $e) { + CakeLog::write('error', __METHOD__ . ': An error occurred'); + throw $e; + } + + return array(); + } + /** * ======================== NOT BEING USED ======================== * diff --git a/Model/GrouperGroup.php b/Model/GrouperGroup.php index 4709064..3b32fbd 100644 --- a/Model/GrouperGroup.php +++ b/Model/GrouperGroup.php @@ -344,6 +344,64 @@ public function membersInGroup(array $conditions, string $userId) { } } + /** + * Add a member to a specific Grouper Group + * + * @param array $conditions Listing of conditions for display of records + * @param string $userId Id of User + * @return array Listing of members in requested Grouper Group + * @throws GrouperLiteException Captured in Controller + * + */ + public function addMemberToGroup(array $conditions, string $userId) { + $this->initApi(); + + $conditions['userId'] = $userId; + + try { + $groupMembers = $this->grouperAPI->addMemberToGroup($conditions); + + if ($groupMembers != 'SUCCESS'){ + return []; + } + + return $groupMembers; + + } catch (Exception $e) { + CakeLog::write('error', __METHOD__ . ': An error occurred'); + throw $e; + } + } + + /** + * Remove a member from a specific Grouper Group + * + * @param array $conditions Listing of conditions for display of records + * @param string $userId Id of User + * @return array Listing of members in requested Grouper Group + * @throws GrouperLiteException Captured in Controller + * + */ + public function removeMemberToGroup(array $conditions, string $userId) { + $this->initApi(); + + $conditions['userId'] = $userId; + + try { + $groupMembers = $this->grouperAPI->removeMemberToGroup($conditions); + + if (count($groupMembers) < 1){ + return $groupMembers; + } + + return $groupMembers['resultCode']; + + } catch (Exception $e) { + CakeLog::write('error', __METHOD__ . ': An error occurred'); + throw $e; + } + } + /** * ======================== NOT BEING USED ======================== * diff --git a/View/Elements/Components/subscriberList.ctp b/View/Elements/Components/subscriberList.ctp index d15b0f0..8caa819 100644 --- a/View/Elements/Components/subscriberList.ctp +++ b/View/Elements/Components/subscriberList.ctp @@ -63,7 +63,7 @@ ) ); ?>'; - var removeUrl = 'Html->url( + var addUrl = 'Html->url( array( 'plugin' => "grouper_lite", 'controller' => 'grouper_groups', @@ -143,7 +143,7 @@ var user = $(ev.target).data('user'); $.ajax({ method: 'DELETE', - url: removeUrl + '?userId=' + user, + url: removeUrl + '?group=' + group + '&userId=' + user, dataType: 'json', success: function(data) { load(); @@ -162,7 +162,7 @@ function onAddUser(user, group, field) { $.ajax({ method: 'POST', - url: removeUrl + '?group=' + group + '&userId=' + user, + url: addUrl + '?group=' + group + '&userId=' + user, dataType: 'json', success: function(data) { load();