From 938e45bc69a54ac640c7827caf1784bb56781d01 Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Wed, 28 Feb 2024 21:10:18 +0200 Subject: [PATCH] Refactor get groupSubscribers --- Controller/GrouperGroupsController.php | 13 +++---------- Lib/GrouperApiAccess.php | 20 ++++++-------------- Lib/enum.php | 1 + View/GrouperGroups/index.ctp | 1 + webroot/js/members.js | 6 +++++- 5 files changed, 16 insertions(+), 25 deletions(-) diff --git a/Controller/GrouperGroupsController.php b/Controller/GrouperGroupsController.php index 3baeed7..79d3515 100644 --- a/Controller/GrouperGroupsController.php +++ b/Controller/GrouperGroupsController.php @@ -149,11 +149,12 @@ public function groupOwner(): void * Show all members of a group * Called from all pages via AJAX call * + * @throws Exception */ public function groupSubscribers(): void { $groupName = urldecode($this->request->query['groupname']); - $subscribers = 0; + $subscribers = []; // //Need to see if coming from AdHoc or from a WG (Working Group) // $groupNameFormatted = strpos($groupName, ':') === false ? 'ref:incommon-collab:' . $groupName . ':users' @@ -165,15 +166,7 @@ public function groupSubscribers(): void $this->CoGrouperLiteWidget->getConfig()); } 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')); - } - - if(count($subscribers) < 1){ - $this->restResponse(HttpStatusCodesEnum::HTTP_NOT_FOUND, ErrorsEnum::Error); - } elseif (count($subscribers) == 1 - && $subscribers[0]['sourceId'] === 'NoAccess') { - $this->restResponse(HttpStatusCodesEnum::HTTP_FORBIDDEN, ErrorsEnum::NoAccess); + throw $e; } $this->set(compact('subscribers')); diff --git a/Lib/GrouperApiAccess.php b/Lib/GrouperApiAccess.php index bf5c9e8..abf1726 100644 --- a/Lib/GrouperApiAccess.php +++ b/Lib/GrouperApiAccess.php @@ -60,7 +60,7 @@ public function __construct(array $cfg) || empty($cfg['CoGrouperLiteWidget']['conn_pass']) || empty($cfg['CoGrouperLiteWidget']['conn_ver']) ) { - throw new GrouperLiteWidgetException('No GrouperLite instance captured'); + throw new \http\Exception\RuntimeException('No GrouperLite instance captured'); } $this->http->setServiceUrl($cfg['CoGrouperLiteWidget']['conn_url'], $cfg['CoGrouperLiteWidget']['conn_ver']); @@ -78,8 +78,7 @@ public function __construct(array $cfg) * @param string $addUserId * * @return bool Requests success or not - * @throws GrouperLiteWidgetException - * @throws JsonException + * @throws GrouperLiteWidgetException|JsonException */ public function addGroupMember(string $actAsUserId, string $groupName, @@ -423,7 +422,7 @@ public function getGrouperUserMemberships(string $userId, string $groupType): ar [GrouperGroupTypeEnum::OPTINS, GrouperGroupTypeEnum::OPTOUTS], true); - //Build request logic + // Build request logic $data = []; $data['WsRestGetMembershipsRequest']['fieldName'] = $groupType; $data['WsRestGetMembershipsRequest']['wsSubjectLookups'][0]['subjectId'] = $isOptinsOrOptouts ? GrouperConfigEnums::ALL : $userId; @@ -482,16 +481,9 @@ public function getGroupMembers(string $actorUserId, string $groupName): array throw $e; } - // Parse out relevant records to send front end - if(isset($results['WsGetMembersResults']['results'][0]['resultMetadata']['resultCode']) - && $results['WsGetMembersResults']['results'][0]['resultMetadata']['resultCode'] === GrouperResultCodesEnum::GROUP_NOT_FOUND) { - return [ - [ - 'sourceId' => 'NoAccess', - 'name' => '', - 'id' => '' - ] - ]; + if(isset($results['error']) && $results['error']) { + $cakeExceptionClass = $results['cakeException']; + throw new $cakeExceptionClass($results['message']); } return $results['WsGetMembersResults']['results'][0]['wsSubjects'] ?? []; diff --git a/Lib/enum.php b/Lib/enum.php index 815b144..963dae1 100644 --- a/Lib/enum.php +++ b/Lib/enum.php @@ -27,6 +27,7 @@ class GrouperResultCodesEnum { const IS_NOT_MEMBER = 'IS_NOT_MEMBER'; const NO_SUCH_OBJECT = 'NO_SUCH_OBJECT'; const PROBLEM_WITH_ASSIGNMENT = 'PROBLEM_WITH_ASSIGNMENT'; + const PROBLEM_GETTING_MEMBERS = 'PROBLEM_GETTING_MEMBERS'; const SUBJECT_NOT_FOUND = 'SUBJECT_NOT_FOUND'; const SUCCESS = 'SUCCESS'; const SUCCESS_INSERTED = 'SUCCESS_INSERTED'; diff --git a/View/GrouperGroups/index.ctp b/View/GrouperGroups/index.ctp index d3da24d..b05e647 100644 --- a/View/GrouperGroups/index.ctp +++ b/View/GrouperGroups/index.ctp @@ -86,6 +86,7 @@ addSubscriberSuccess: "", removeSubscriberError: "", removeSubscriberSuccess: "", + getSubscriberError: "", searchTag: "", peoplePickerPlaceHolder: "", noaccess: "", diff --git a/webroot/js/members.js b/webroot/js/members.js index 4357086..2fa0a09 100644 --- a/webroot/js/members.js +++ b/webroot/js/members.js @@ -70,13 +70,17 @@ export default { const resp = await fetch(`${this.api.group}?groupname=${encodeURIComponent(name)}`, { headers: { "Accept": "application/json", - // 'Content-Type': 'application/x-www-form-urlencoded', }, method: "GET" }); if (resp.ok) { this.subscribers = await resp.json(); } else { + generateFlash( + `${this.txt.getSubscriberError.replace('this', `"${name}"`)}`, + 'error'); + let errorResponse = await resp.json(); + generateFlash(`${errorResponse.message}`, 'error'); this.error = resp; } this.loading = false;