From ebd58519c0fee92629c77139eba28e3793fac845 Mon Sep 17 00:00:00 2001 From: axman Date: Fri, 29 Jan 2021 12:04:56 -0700 Subject: [PATCH] Exceptions not complete, will do in another ticket --- Controller/GrouperGroupsController.php | 15 +- Lib/GrouperApiAccess.php | 329 +++++++++++++++++++------ Lib/GrouperHTTPWrapper.php | 119 +++++++-- Lib/GrouperLiteException.php | 51 ++++ Model/CoGrouperLite.php | 25 ++ Model/GrouperAttribute.php | 52 +++- Model/GrouperGroup.php | 138 ++++++++--- Model/GrouperLite.php | 26 +- Model/GrouperLiteAppModel.php | 25 ++ Model/GrouperUser.php | 23 -- 10 files changed, 644 insertions(+), 159 deletions(-) create mode 100644 Lib/GrouperLiteException.php delete mode 100644 Model/GrouperUser.php diff --git a/Controller/GrouperGroupsController.php b/Controller/GrouperGroupsController.php index f5aaebd..6eff67e 100644 --- a/Controller/GrouperGroupsController.php +++ b/Controller/GrouperGroupsController.php @@ -99,7 +99,20 @@ public function groupOptin() { $this->set('groupergroupoptin', $this->GrouperGroup->getSearchedGroups($this->userId, $searchCriteria, 'groupOptin')); $this->set('searchcriteria', $searchCriteria); } else { - $this->set('groupergroupoptin', $this->GrouperGroup->optinGroups($this->userId)); + try { + $optin = $this->GrouperGroup->optinGroups($this->userId); + if (isset($optin['errorMessage'])){ + $this->Flash->error($optin['errorMessage']); + $this->set('groupergroupoptin', array()); + } else { + $this->set('groupergroupoptin', $optin); + } + + } catch (Exception $e) { + CakeLog::write('error', 'GrouperLite Controller: Seeing is believing'); + $this->Flash->error($e->getMessage()); + $this->set('groupergroupoptin', array()); + } } } diff --git a/Lib/GrouperApiAccess.php b/Lib/GrouperApiAccess.php index cb7bcd2..c603f01 100644 --- a/Lib/GrouperApiAccess.php +++ b/Lib/GrouperApiAccess.php @@ -1,53 +1,89 @@ http = new GrouperHTTPWrapper(); if (!CakeSession::check('Plugin.Grouper.id')) { - //TODO throw error of some sort + CakeLog::write('error', 'GrouperLite API: No Widget record in Session'); + throw new GrouperLiteException("No GrouperLite instance captured"); } $connUrl = CakeSession::read('Plugin.Grouper.url'); $connVer = CakeSession::read('Plugin.Grouper.version'); - $this->config['fullUrl'] = $connUrl. $this->_urlServlet . $connVer; + $this->config['fullUrl'] = $connUrl . $this->_urlServlet . $connVer; $this->http->setUser(CakeSession::read('Plugin.Grouper.user')); $this->http->setPassword(CakeSession::read('Plugin.Grouper.pass')); - - //$connections = ConnectionManager::enumConnectionObjects(); - //var_dump($connections); - //$this->config = $connections['grouperapi']; - //$connUrl = Configure::read('GrouperLite.connUrl'); - //$connVer = Configure::read('GrouperLite.connVer'); - - //$this->config['fullUrl'] = $connUrl. $this->_urlServlet . $connVer; - - //$this->http->setUser(Configure::read('GrouperLite.connUser')); - //$this->http->setPassword(Configure::read('GrouperLite.connPass')); - } - public function getGrouperUser($queryData) { - $userId = $queryData['conditions']['userId']; + /** + * NOT BEING USED + * Get User information from Grouper Web Service + * + * @param array $queryData Array of conditions for querying + * @return array + * @throws Exception + */ + public function getGrouperUser(array $queryData) { + $userId = $queryData['conditions']['userId']; $this->http->setHeader(array('Content-Type' => 'application/json', 'Accept' => 'application/json')); + + // Create attributes want returned from call to Grouper WS $formArray = array( 'WsRestGetSubjectsRequest' => array( 'subjectAttributeNames' => array( @@ -63,32 +99,63 @@ public function getGrouperUser($queryData) { ) ); $formData = json_encode($formArray); - $connectionUrl = $this->config['fullUrl'] . '/subjects'; - $results = $this->http->sendRequest('GET', $connectionUrl, $formData); - if (isset($results['WsGetSubjectsResults']['wsSubjects'][0]['id']) && $results['WsGetSubjectsResults']['wsSubjects'][0]['id'] != NULL) { - return $results['WsGetSubjectsResults']['wsSubjects'][0]['id']; + try { + $results = $this->http->sendRequest('GET', $connectionUrl, $formData); + + // Parse out relevant records to send front end + if (isset($results['WsGetSubjectsResults']['wsSubjects'][0]['id']) && $results['WsGetSubjectsResults']['wsSubjects'][0]['id'] != NULL) { + return $results['WsGetSubjectsResults']['wsSubjects'][0]['id']; + } + + } catch (Exception $e) { + CakeLog::write('error', + 'GrouperLite API - getGrouperUser: An issue surfaced of ' . var_export($e->getMessage(), true)); + throw $e; } + return array(); } + /** + * Get Groups that User is a member of from Grouper. + * + * @param array $queryData Array of conditions for querying + * @return array Membership records that User is a member of in Grouper + * @throws GrouperLiteException + */ + public function getGrouperMemberOfGroups(array $queryData) { - public function getGrouperMemberOfGroups($queryData) { + //Build request logic $userId = $queryData['conditions']['userId']; - $connectionUrl = "{$this->config['fullUrl']}/subjects/{$userId}/groups"; - $results = $this->http->sendRequest('GET', $connectionUrl); + try { + $results = $this->http->sendRequest('GET', $connectionUrl); - if (isset($results['WsGetGroupsLiteResult']['wsGroups']) && $results['WsGetGroupsLiteResult']['wsGroups'] != NULL) { - return $results['WsGetGroupsLiteResult']['wsGroups']; + // Parse out relevant records to send front end + if (isset($results['WsGetGroupsLiteResult']['wsGroups']) && $results['WsGetGroupsLiteResult']['wsGroups'] != NULL) { + return $results['WsGetGroupsLiteResult']['wsGroups']; + } + } catch (Exception $e) { + CakeLog::write('error', + 'GrouperLite API - getGrouperMemberOfGroups: An issue surfaced of ' . var_export($e->getMessage(), true)); + throw $e; } return array(); } - public function grouperGroupLeaveOrJoin($queryData) { + /** + * For Optin groups, calls Grouper WS to join or leave a group + * + * @param array $queryData Array of conditions for querying + * @return bool True if join or leave successful, False if not + * @throws GrouperLiteException + */ + public function grouperGroupLeaveOrJoin(array $queryData) { + $groupName = $queryData['conditions']['groupName']; $userId = $queryData['conditions']['userId']; $groupLeaveOrJoin = $queryData['conditions']['LeaveJoin']; @@ -99,10 +166,12 @@ public function grouperGroupLeaveOrJoin($queryData) { } elseif ($groupLeaveOrJoin == "Join") { $memberRequest = "WsRestAddMemberRequest"; } else { - $memberRequest = ''; - //TODO Log and throw error + CakeLog::write('error', + "GrouperLite API - grouperGroupLeaveOrJoin: Option of $groupLeaveOrJoin is not supported"); + throw new GrouperLiteException("Receved option of $groupLeaveOrJoin which is not supported"); } + //Build request logic $groupToLeave = array( $memberRequest => array( "actAsSubjectLookup" => array( @@ -114,57 +183,115 @@ public function grouperGroupLeaveOrJoin($queryData) { ) ) ); - $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($groupToLeave)); + try { + $results = $this->http->sendRequest('PUT', $connectionUrl, json_encode($groupToLeave)); - if (isset($results['WsAddMemberResults']['wsGroupAssigned']) && $results['WsAddMemberResults']['wsGroupAssigned'] != NULL) { - $groupAssigned = $results['WsAddMemberResults']['wsGroupAssigned']['name']; - if ($groupAssigned == urldecode($groupName)) { - return true; + if (isset($results['WsAddMemberResults']['wsGroupAssigned']) && $results['WsAddMemberResults']['wsGroupAssigned'] != NULL) { + $groupAssigned = $results['WsAddMemberResults']['wsGroupAssigned']['name']; + if ($groupAssigned == urldecode($groupName)) { + return true; + } } + } catch (Exception $e) { + CakeLog::write('error', + 'GrouperLite API - grouperGroupLeaveOrJoin: An issue surfaced of ' . var_export($e->getMessage(), true)); + throw $e; } return false; } - public function getOptinGroups($queryData) { + /** + * Gets all available Optin groups in Grouper + * Checks to see which Optin groups the user is already a member of + * Returns Optin groups that can be joined and ones that the user is already joined + * + * @param array $queryData Array of conditions for querying + * @return array Optin groups from Grouper + * @throws GrouperLiteException + * + */ + public function getOptinGroups(array $queryData) { $queryData['conditions']['groupType'] = 'Optins'; - $results = $this->useMembershipUrl($queryData); - if (isset($results['WsGetMembershipsResults']['wsGroups']) && $results['WsGetMembershipsResults']['wsGroups'] != NULL) { - return $results['WsGetMembershipsResults']['wsGroups']; - } + try { + $results = $this->useMembershipUrl($queryData); + if (isset($results['WsGetMembershipsResults']['wsGroups']) && $results['WsGetMembershipsResults']['wsGroups'] != NULL) { + return $results['WsGetMembershipsResults']['wsGroups']; + } + } catch (Exception $e) { + CakeLog::write('error', + 'GrouperLite API - getOptinGroups: An issue surfaced of ' . var_export($e->getMessage(), true)); + throw $e; + } return array(); } - public function getOwnerGroups($queryData) { + /** + * Gets all groups in Grouper where user is an admin/owner + * + * @param array $queryData Array of conditions for querying + * @return array Array of groups from Grouper + * @throws GrouperLiteException + * + */ + public function getOwnerGroups(array $queryData) { $queryData['conditions']['groupType'] = 'Owner'; - $results = $this->useMembershipUrl($queryData); - if (isset($results['WsGetMembershipsResults']['wsGroups']) && $results['WsGetMembershipsResults']['wsGroups'] != NULL) { - return $results['WsGetMembershipsResults']['wsGroups']; - } + try { + $results = $this->useMembershipUrl($queryData); + if (isset($results['WsGetMembershipsResults']['wsGroups']) && $results['WsGetMembershipsResults']['wsGroups'] != NULL) { + return $results['WsGetMembershipsResults']['wsGroups']; + } + } catch (Exception $e) { + CakeLog::write('error', + 'GrouperLite API - getOwnerGroups: An issue surfaced of ' . var_export($e->getMessage(), true)); + throw $e; + } return array(); } - public function getOwnerStems($queryData) { + /** + * Gets all Stems/Folders where User is admin/owner + * + * @param array $queryData Array of conditions for querying + * @return array Array of Stems/Folders from Grouper + * @throws GrouperLiteException + */ + public function getOwnerStems(array $queryData) { $queryData['conditions']['groupType'] = 'StemOwner'; - $results = $this->useMembershipUrl($queryData); - if (isset($results['WsGetMembershipsResults']['wsStems']) && $results['WsGetMembershipsResults']['wsStems'] != NULL) { - return $results['WsGetMembershipsResults']['wsStems']; - } + try { + $results = $this->useMembershipUrl($queryData); + if (isset($results['WsGetMembershipsResults']['wsStems']) && $results['WsGetMembershipsResults']['wsStems'] != NULL) { + return $results['WsGetMembershipsResults']['wsStems']; + } + } catch (Exception $e) { + CakeLog::write('error', + 'GrouperLite API - getOwnerStems: An issue surfaced of ' . var_export($e->getMessage(), true)); + throw $e; + } return array(); } - private function useMembershipUrl($queryData) { + /** + * Used for requests made to Membership endpoint in Grouper WS + * + * @see getOptinGroups() + * @see getOwnerGroups() + * @see getOwnerStems() + * + * @param array $queryData Array of conditions for querying + * @return array Group records associated to calling method + * @throws GrouperLiteException + */ + private function useMembershipUrl(array $queryData) { $groupType = $queryData['conditions']['groupType']; $userId = $queryData['conditions']['userId']; @@ -178,10 +305,12 @@ private function useMembershipUrl($queryData) { $fieldName = "stemAdmin"; $subjectId = $userId; } else { - return array(); - //TODO - Throw error + CakeLog::write('error', + "GrouperLite API - useMembershipUrl: Option of $groupType is not supported"); + throw new GrouperLiteException("Option of $groupType is not supported"); } + //Build request logic $groupsToShow = array( "WsRestGetMembershipsRequest" => array( "fieldName" => $fieldName, @@ -190,34 +319,58 @@ private function useMembershipUrl($queryData) { ) ) ); - $this->http->setHeader(array('Content-Type' => 'application/json', 'Accept' => 'application/json')); - $connectionUrl = "{$this->config['fullUrl']}/memberships"; - return $this->http->sendRequest('POST', $connectionUrl, json_encode($groupsToShow)); + try { + return $this->http->sendRequest('POST', $connectionUrl, json_encode($groupsToShow)); + } catch (Exception $e) { + CakeLog::write('error', + 'GrouperLite API - useMembershipUrl: An issue surfaced of ' . var_export($e->getMessage(), true)); + throw $e; + } } - - public function getGrouperGroupDescription($queryData) { + /** + * Returns a Distinct Grouper Group with its associated attributes and values + * + * @param array $queryData Array of conditions for querying + * @return array Array of attributes for a Group + * @throws GrouperLiteException + */ + public function getGrouperGroupDescription(array $queryData) { $groupName = $queryData['conditions']['groupName']; + //Build request logic $groupName = urlencode($groupName); - $connectionUrl = "{$this->config['fullUrl']}/groups/{$groupName}/memberships"; - $results = $this->http->sendRequest('GET', $connectionUrl); + try { + $results = $this->http->sendRequest('GET', $connectionUrl); - if (isset($results['WsGetMembershipsResults']['wsGroups']) && $results['WsGetMembershipsResults']['wsGroups'] != NULL) { - return $results['WsGetMembershipsResults']['wsGroups']; + if (isset($results['WsGetMembershipsResults']['wsGroups']) && $results['WsGetMembershipsResults']['wsGroups'] != NULL) { + return $results['WsGetMembershipsResults']['wsGroups']; + } + } catch (Exception $e) { + CakeLog::write('error', + 'GrouperLite API - getGrouperGroupDescription: An issue surfaced of ' . var_export($e->getMessage(), true)); + throw $e; } - return array(); } - public function createUpdateGroup($queryData) { + /** + * NOTE: This will probably changes once templates become available to use + * + * Create or Update a Group where User is Admin/Owner + * + * @param array $queryData Array of conditions for querying + * @return bool True if added or updated successful + * @throws GrouperLiteException + */ + public function createUpdateGroup(array $queryData) { //TODO - Need to add checking for missing ':' - //Currently only supporting create group, but can update a group with this call! + //Currently only supporting create group, need to test if update a group will work! $groupName = $queryData['conditions']['groupName']; $stemName = $queryData['conditions']['stemName']; @@ -226,6 +379,7 @@ public function createUpdateGroup($queryData) { $newGroupToSave = $stemName . $groupName; + //Build request logic $groupToSave = array( "WsRestGroupSaveRequest" => array( "actAsSubjectLookup" => array( @@ -244,25 +398,37 @@ public function createUpdateGroup($queryData) { ) ) ); - $this->http->setHeader(array('Content-Type' => 'application/json', 'Accept' => 'application/json')); - $connectionUrl = "{$this->config['fullUrl']}/groups"; - $results = $this->http->sendRequest('POST', $connectionUrl, json_encode($groupToSave)); + try { + $results = $this->http->sendRequest('POST', $connectionUrl, json_encode($groupToSave)); - if (isset($results['WsGroupSaveResults']['results']['resultMetadata']['resultCode'])) { - if (stripos($results['WsGroupSaveResults']['results']['resultMetadata']['resultCode'], "Success", 0) !== false) { - return true; + if (isset($results['WsGroupSaveResults']['results']['resultMetadata']['resultCode'])) { + if (stripos($results['WsGroupSaveResults']['results']['resultMetadata']['resultCode'], "Success", 0) !== false) { + return true; + } } + } catch (Exception $e) { + CakeLog::write('error', + 'GrouperLite API - createUpdateGroup: An issue surfaced of ' . var_export($e->getMessage(), true)); + throw $e; } return false; } - public function getGroupAttributes($queryData) { + /** + * Listing of attributes in Grouper for a given Group Name + * + * @param array $queryData Array of conditions for querying + * @return array Record of Grouper attributes for given GroupName + * @throws GrouperLiteException + */ + public function getGroupAttributes(array $queryData) { // Need to pass in the full stem path, so thing like sandbox:app:sympa are good! $groupName = $queryData['conditions']['groupName']; + //Build request logic $stemToFind = array( "WsRestGetAttributeAssignmentsRequest" => array( "attributeAssignType" => "group", @@ -274,14 +440,19 @@ public function getGroupAttributes($queryData) { ) ) ); - $this->http->setHeader(array('Content-Type' => 'application/json', 'Accept' => 'application/json')); $connectionUrl = "{$this->config['fullUrl']}/attributeAssignments"; - $results = $this->http->sendRequest('POST', $connectionUrl, json_encode($stemToFind)); + try { + $results = $this->http->sendRequest('POST', $connectionUrl, json_encode($stemToFind)); - if (isset($results['WsGetAttributeAssignmentsResults']['wsAttributeAssigns']) && $results['WsGetAttributeAssignmentsResults']['wsAttributeAssigns'] != NULL) { - return $results['WsGetAttributeAssignmentsResults']['wsAttributeAssigns']; + if (isset($results['WsGetAttributeAssignmentsResults']['wsAttributeAssigns']) && $results['WsGetAttributeAssignmentsResults']['wsAttributeAssigns'] != NULL) { + return $results['WsGetAttributeAssignmentsResults']['wsAttributeAssigns']; + } + } catch (Exception $e) { + CakeLog::write('error', + 'GrouperLite API - getGroupAttributes: An issue surfaced of ' . var_export($e->getMessage(), true)); + throw $e; } return array(); } diff --git a/Lib/GrouperHTTPWrapper.php b/Lib/GrouperHTTPWrapper.php index 9872a74..9c9abb9 100644 --- a/Lib/GrouperHTTPWrapper.php +++ b/Lib/GrouperHTTPWrapper.php @@ -1,37 +1,111 @@ config['ssl_verify_peer'] = false; } - public function setSSLVerify($val) { + /** + * Setter for SSL Verify Peer + * + * @param boolean $val + */ + public function setSSLVerify(bool $val) { $this->config['ssl_verify_peer'] = $val; } - public function setUser($user) { + /** + * Setter - User account for accessing Grouper Web Services + * + * @param string $user + */ + public function setUser(string $user) { + $this->_user = $user; } - public function setPassword($password) { + /** + * Setter - Password for accessing Grouper Web Services + * + * @param string $password + */ + public function setPassword(string $password) { + $this->_password = $password; } - public function setHeader($headerSetting) { + /** + * Setter - Header settings needed to call Grouper Web Services + * + * @param array $headerSetting + */ + public function setHeader(array $headerSetting) { $this->_request['header'] = $headerSetting; } - public function sendRequest($method, $uri, $body = '') { + /** + * Send request to Grouper Web Services for Grouper Groups Information + * + * @param string $method HTTP RESTful method (example: GET, POST, PUT, etc.) + * @param string $uri Grouper RESTful endpoint with request string, if applicable + * @param string $body JSON formatted request, if applicable + * @return array array of records | array with error message + * @throws GrouperLiteException If issue with Grouper WS connection + */ + public function sendRequest(string $method, string $uri, string $body = ''): array { + $this->_request['method'] = $method; $this->_request['uri'] = $this->_parseUri($uri); $this->_request['uri']['user'] = $this->_user; @@ -40,19 +114,23 @@ public function sendRequest($method, $uri, $body = '') { try { $results = $this->request($this->_request); - } catch(Throwable $e) { - CakeLog::write('error', 'Grouper API: An error occurred: ' . print_r($e->getMessage(), true)); - $results = array( - 'code' => 500, - 'errorMessage' => 'An error occurred in reaching the Grouper API, please check the logs and your settings.' - ); - return array(); + } catch (Exception $e) { + CakeLog::write('error', + 'GrouperLite API: An error occurred: ' . var_export($e->getMessage(), true)); + throw new GrouperLiteException('An error occurred talking to Grouper WS'); } - return $this->_verifyResults($results); } - private function _verifyResults($apiResults) { + /** + * Verify data from Grouper Web Service came back successfully, if not log error and send to front end. + * + * @param HttpSocketResponse $apiResults Results from Grouper WS + * @return array array of records | array with error message + * @throws GrouperLiteException If issue with Grouper WS data returned + */ + private function _verifyResults(HttpSocketResponse $apiResults): array { + $resBody = array(); if ($apiResults->isOk()) { $resBody = json_decode($apiResults->body(), true); @@ -62,12 +140,15 @@ private function _verifyResults($apiResults) { if ($apiSuccess != 'SUCCESS') { CakeLog::write('error', - 'Grouper API: Result Code was not SUCCESS, it was: ' . print_r($apiSuccess, true)); - $resBody = array(); + 'GrouperLite: Result Code was not SUCCESS, it was: ' . var_export($apiSuccess, true)); + CakeLog::write('error', + 'GrouperLite: Error message: ' . var_export($apiResults->body(), true)); + throw new GrouperLiteException('Result from Grouper WS was' . var_export($apiSuccess, true)); } } else { CakeLog::write('error', - 'Grouper API Error: ' . print_r($apiResults->body(), true)); + 'GrouperLite Verify: Error message = ' . var_export($apiResults->body(), true)); + throw new GrouperLiteException('An error occurred while talking to Grouper WS'); } return $resBody; } diff --git a/Lib/GrouperLiteException.php b/Lib/GrouperLiteException.php new file mode 100644 index 0000000..6e4621b --- /dev/null +++ b/Lib/GrouperLiteException.php @@ -0,0 +1,51 @@ +code}]: {$this->message}\n"; + } +} \ No newline at end of file diff --git a/Model/CoGrouperLite.php b/Model/CoGrouperLite.php index f276be6..154c0e6 100644 --- a/Model/CoGrouperLite.php +++ b/Model/CoGrouperLite.php @@ -1,4 +1,29 @@ grouperAPI = new GrouperApiAccess(); + /** + * Used to instantiate API class + */ + private function initApi() { + if ($this->grouperAPI == null) { + $this->grouperAPI = new GrouperApiAccess(); + } } - public function groupAttributes($groupName) { + /** + * Pull attributes for a Group in Grouper + * + * @param string $groupName Name of Group + * @return array Attributes in Grouper for this Group + * @throws GrouperLiteException + */ + public function groupAttributes(string $groupName) { $args = array(); $args['conditions']['groupName'] = $groupName; diff --git a/Model/GrouperGroup.php b/Model/GrouperGroup.php index c34a3e0..bdb7e63 100644 --- a/Model/GrouperGroup.php +++ b/Model/GrouperGroup.php @@ -1,29 +1,68 @@ grouperAPI = new GrouperApiAccess(); - + /** + * Used to instantiate API class + * + * Tried to use the constructor for GrouperGroup to set this but it was triggering the GrouperAPIAccess class + * to instantiate before the Controller was able populate the connection settings from the DB into the Session, + * so error was being thrown. + * Now will call this before each function call to verify set. + */ + private function initApi() { + if ($this->grouperAPI == null) { + $this->grouperAPI = new GrouperApiAccess(); + } } + private function memberOfGroups($userId) { + $this->initApi(); + $args = array(); $args['conditions']['userId'] = $userId; @@ -32,8 +71,9 @@ private function memberOfGroups($userId) { public function filteredMemberOfGroups($userId) { - //Need to filter out the optin joined groups + $this->initApi(); + //Need to filter out the optin joined groups $memberOfGroups = $this->memberOfGroups($userId); $args = array(); @@ -52,6 +92,8 @@ public function filteredMemberOfGroups($userId) { } public function groupDescriptions($groupName) { + $this->initApi(); + $args = array(); $args['conditions']['groupName'] = $groupName; @@ -67,6 +109,8 @@ public function groupDescriptions($groupName) { } public function leaveGroup($userId, $groupName) { + $this->initApi(); + $args = array(); $args['conditions']['LeaveJoin'] = 'Leave'; $args['conditions']['userId'] = $userId; @@ -76,6 +120,8 @@ public function leaveGroup($userId, $groupName) { } public function joinGroup($userId, $groupName) { + $this->initApi(); + $args = array(); $args['conditions']['LeaveJoin'] = 'Join'; $args['conditions']['userId'] = $userId; @@ -85,23 +131,38 @@ public function joinGroup($userId, $groupName) { } public function ownerGroups($userId) { + $this->initApi(); + $args = array(); $args['conditions']['userId'] = $userId; $ownGroups = $this->grouperAPI->getOwnerGroups($args); - $ownerGroups = $this->getFriendlyName($ownGroups); return $ownerGroups; } - public function optinGroups($userId) { + /** + * Get all Groups with Optin attribute set + * Will Match up with Groups User is a member of to determine which Optin groups the user is + * already a member of. + * + * @param string $userId Id of user + * @return array Listing of Optin groups available in Grouper + * @throws GrouperLiteException Captured in Controller + */ + public function optinGroups(string $userId) { + $this->initApi(); + $args = array(); $args['conditions']['userId'] = $userId; $joinOrLeave = $this->grouperAPI->getOptinGroups($args); $userGroups = $this->memberOfGroups($userId); + if(isset($joinOrLeave['errorMessage'])){ + return $joinOrLeave; + } //See if Optin group match any of the groups belong to already. foreach ($joinOrLeave as &$groupsJoL) { @@ -117,11 +178,12 @@ public function optinGroups($userId) { } } $groupsToOptin = $this->getFriendlyName($joinOrLeave); - return $groupsToOptin; } public function createGroup($userId, $groupName, $stemName, $groupDesc) { + $this->initApi(); + $args = array(); $args['conditions']['groupType'] = 'Optins'; $args['conditions']['userId'] = $userId; @@ -133,30 +195,14 @@ public function createGroup($userId, $groupName, $stemName, $groupDesc) { } public function getOwnerStems($userId) { + $this->initApi(); + $args = array(); $args['conditions']['userId'] = $userId; return $this->grouperAPI->getOwnerStems($args); } - private function getFriendlyName($groups) { - - foreach ($groups as &$group) { - $group['friendlyName'] = $group['displayName']; - $attributes = $this->grouperAPI->getGroupAttributes(array('conditions' => array('groupName' => $group['displayName']))); - foreach ($attributes as $attribute) { - if ($attribute['attributeDefNameName'] == $this->friendly) { - if (isset($attribute['wsAttributeAssignValues']['valueSystem'])) { - $group['friendlyName'] = $attribute['wsAttributeAssignValues']['valueSystem']; - } - break; - } - } - } - - return $groups; - } - /** * Search for Groups/Lists related to Search term. * Will import all records the user can see and then do search here rather than call Grouper WS Search @@ -169,6 +215,8 @@ private function getFriendlyName($groups) { * @return array */ public function getSearchedGroups($userId, $searchCriteria, $page) { + $this->initApi(); + $args = array(); $args['conditions']['userId'] = $userId; @@ -194,4 +242,30 @@ public function getSearchedGroups($userId, $searchCriteria, $page) { return $returnResults; } + /** + * Determine if result set contains friendly name, if so add as a new attribute in result set + * + * @param array $groups Current result set of Groups + * @return array Same result set with added param for friendly name + * @throws GrouperLiteException + * + */ + private function getFriendlyName(array $groups) { + $this->initApi(); + + foreach ($groups as &$group) { + $group['friendlyName'] = $group['displayName']; + $attributes = $this->grouperAPI->getGroupAttributes(array('conditions' => array('groupName' => $group['displayName']))); + foreach ($attributes as $attribute) { + if ($attribute['attributeDefNameName'] == $this->friendly) { + if (isset($attribute['wsAttributeAssignValues']['valueSystem'])) { + $group['friendlyName'] = $attribute['wsAttributeAssignValues']['valueSystem']; + } + break; + } + } + } + return $groups; + } + } diff --git a/Model/GrouperLite.php b/Model/GrouperLite.php index ef785ea..9c2520b 100644 --- a/Model/GrouperLite.php +++ b/Model/GrouperLite.php @@ -1,5 +1,29 @@ grouperAPI = new GrouperApiAccess(); - } - - public function getUser($userId) - { - $args = array(); - $args['conditions']['userId'] = $userId; - return $this->grouperAPI->getGrouperUser($args); - } - -}