Skip to content

Commit

Permalink
simplify add/remove to/from group
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Feb 28, 2024
1 parent 758b2f5 commit 076c938
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 159 deletions.
91 changes: 54 additions & 37 deletions Controller/GrouperGroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function beforeRender() {
$this->set('vv_config', $cfg);

$this->set('title', _txt('pl.grouperlite.title.groupmember'));
$this->set('vv_is_user_owner', $this->GrouperGroup->isUserOwner($this->userId ?? '', $cfg) );
$this->set('vv_is_user_owner', $this->GrouperGroup->isUserGroupOwner($this->userId ?? '', $cfg) );
// $this->set('vv_is_template_user', $this->GrouperGroup->isTemplateUser($this->userId ?? '', $cfg) );
// $this->set('vv_is_grouper_visible', $this->GrouperGroup->isGrouperVisible($this->userId ?? '', $cfg));
$this->set('vv_coid', $this->cur_co['Co']['id']);
Expand Down Expand Up @@ -189,6 +189,9 @@ public function groupSubscribers(): void
*/
public function addSubscriber(): void
{
$this->layout = null;
$this->autoRender = false;

$groupName = urldecode($this->request->query['group']);
$addUserId = urldecode($this->request->query['userId']);

Expand All @@ -199,10 +202,14 @@ public function addSubscriber(): void
// : $groupName;

try {
$this->GrouperGroup->addGroupMember($this->userId,
$groupName,
$addUserId,
$this->CoGrouperLiteWidget->getConfig());
if(!$this->GrouperGroup->addGroupMember($this->userId,
$groupName,
$addUserId,
$this->CoGrouperLiteWidget->getConfig())) {
// The Request returned unsuccessful, but we have not more infomration. In this case we will just return
// forbidden since we do not actually now what happened
$this->restResponse(HttpStatusCodesEnum::HTTP_FORBIDDEN);
}
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': ' . var_export($e->getMessage(), true));
throw $e;
Expand Down Expand Up @@ -243,32 +250,35 @@ public function findSubscriber(): void
* Called from all pages via AJAX call
*
* TODO: We need to appropriately handle Unathenticated call. We have to bubble up the response and do something.
* @throws JsonException
*/
public function removeSubscriber(): void
{
$this->layout = null;
$this->autoRender = false;

$groupName = urldecode($this->request->query['group']);
$remUserId = urldecode($this->request->query['userId']);
$resultRemove = false;

//Need to see if coming from AdHoc or from a WG (Working Group)
$groupNameFormatted = (strpos($groupName, ':') === false) ? 'ref:incommon-collab:' . $groupName . ':users'
: $groupName;
// $groupNameFormatted = (strpos($groupName, ':') === false) ? 'ref:incommon-collab:' . $groupName . ':users'
// : $groupName;

try {
$resultRemove = $this->GrouperGroup->removeGroupMember($this->userId,
$groupNameFormatted,
$remUserId,
$this->CoGrouperLiteWidget->getConfig());
if(!$this->GrouperGroup->removeGroupMember($this->userId,
$groupName,
$remUserId,
$this->CoGrouperLiteWidget->getConfig())) {
// The Request returned unsuccessful, but we have not more infomration. In this case we will just return
// forbidden since we do not actually now what happened
$this->restResponse(HttpStatusCodesEnum::HTTP_FORBIDDEN);
}
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': ' . var_export($e->getMessage(), true));
throw $e;
}

if (!$resultRemove) {
$this->restResponse(HttpStatusCodesEnum::HTTP_NOT_FOUND, ErrorsEnum::Error);
}

$this->set(compact($resultRemove ? GrouperResultCodesEnum::SUCCESS : ''));
$this->set('_serialize', 'resultRemove');
$this->restResponse(HttpStatusCodesEnum::HTTP_OK);
}

/**
Expand All @@ -289,7 +299,7 @@ public function groupOwnerApi() {
$scope['searchpage'] = 'ownerGroups';
$errorHint = 'Search';
} else {
$scope['method'] = 'ownerGroups';
$scope['method'] = 'getOwnedGroups';
$errorHint = '';
}
try {
Expand Down Expand Up @@ -429,19 +439,26 @@ public function groupCreateTemplate()
/**
* Process to join a group displayed on the "Optin" page
*
* @throws Exception
*/
public function joinGroup(): void
{
$this->layout = null;
$this->autoRender = false;
// todo: add Subscriber and joinGroup should accept the same query parameters. Currently the join Group
// accepts a GroupName, while the addSubscriber accepts a group parameter
$name = urldecode($this->request->query['GroupName']);
$groupName = urldecode($this->request->query['GroupName']);

try {
// Add myself
$this->GrouperGroup->addGroupMember($this->userId,
$name,
$this->userId,
$this->CoGrouperLiteWidget->getConfig());
if(!$this->GrouperGroup->addGroupMember($this->userId,
$groupName,
$this->userId,
$this->CoGrouperLiteWidget->getConfig())) {
// The Request returned unsuccessful, but we have not more infomration. In this case we will just return
// forbidden since we do not actually now what happened
$this->restResponse(HttpStatusCodesEnum::HTTP_FORBIDDEN);
}
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': ' . var_export($e->getMessage(), true));
throw $e;
Expand All @@ -456,25 +473,25 @@ public function joinGroup(): void
*/
public function leaveGroup(): void
{
$name = urldecode($this->request->query['GroupName']);
$resultRemove = false;
$this->layout = null;
$this->autoRender = false;
$groupName = urldecode($this->request->query['GroupName']);

try {
$resultRemove = $this->GrouperGroup->leaveGroup($this->userId,
$name,
$this->CoGrouperLiteWidget->getConfig());
if(!$this->GrouperGroup->removeGroupMember($this->userId,
$groupName,
$this->userId,
$this->CoGrouperLiteWidget->getConfig())) {
// The Request returned unsuccessful, but we have not more infomration. In this case we will just return
// forbidden since we do not actually now what happened
$this->restResponse(HttpStatusCodesEnum::HTTP_FORBIDDEN);
}
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': ' . var_export($e->getMessage(), true));
throw $e;
}

// $this->restResponse(HttpStatusCodesEnum::HTTP_UNAUTHORIZED, ErrorsEnum::NotDeleted);

if (!$resultRemove) {
$this->restResponse(HttpStatusCodesEnum::HTTP_NOT_FOUND, ErrorsEnum::Error);
}

$this->set(compact($resultRemove ? GrouperResultCodesEnum::SUCCESS : ''));
$this->set('_serialize', 'resultRemove');
$this->restResponse(HttpStatusCodesEnum::HTTP_OK);
}

/**
Expand Down
157 changes: 54 additions & 103 deletions Lib/GrouperApiAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,57 +392,6 @@ public function getUserGroupMemberships(string $actorUserId, string $userId): ar
return $results['WsGetGroupsLiteResult']['wsGroups'] ?? [];
}

/**
* Get members associated to a specific Grouper Group
*
* @param string $actorUserId
* @param string $groupName
*
* @return array Listing of Members belonging to Grouper Group
* @throws GrouperLiteWidgetException
* @throws JsonException
*/
public function getGroupMembers(string $actorUserId, string $groupName): array
{
//Build request logic
$usersToShow = [
'WsRestGetMembersRequest' => [
'actAsSubjectLookup' => [
'subjectId' => $actorUserId
],
'wsGroupLookups' => [
['groupName' => $groupName]
],
'subjectAttributeNames' => ['name']
]
];

$actionEndpoint = "/groups";

try {
$results = $this->http->sendRequest('POST',
$actionEndpoint,
json_encode($usersToShow, JSON_THROW_ON_ERROR));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
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' => ''
]
];
}

return $results['WsGetMembersResults']['results'][0]['wsSubjects'] ?? [];
}

/**
* Used for requests made to Membership endpoint in Grouper WS
*
Expand All @@ -456,7 +405,7 @@ public function getGroupMembers(string $actorUserId, string $groupName): array
* @see getOptOutGroups()
* @see getOwnedGroups()
*/
private function getGrouperUserMemberships(string $userId, string $groupType): array
public function getGrouperUserMemberships(string $userId, string $groupType): array
{
if (in_array($groupType, [
GrouperGroupTypeEnum::OPTINS,
Expand Down Expand Up @@ -500,47 +449,76 @@ private function getGrouperUserMemberships(string $userId, string $groupType): a
}

/**
* Gets all available Optin/OptOut groups in Grouper
*
* Returns Optin/OptOut groups that can be joined/left
* Get members associated to a specific Grouper Group
*
* @param string $userId
* @param string $groupType
* @param string $actorUserId
* @param string $groupName
*
* @return array Optin groups from Grouper
* @return array Listing of Members belonging to Grouper Group
* @throws GrouperLiteWidgetException
* @throws JsonException
*/
public function getOptionalGroups(string $userId, string $groupType): array
public function getGroupMembers(string $actorUserId, string $groupName): array
{
//Build request logic
$usersToShow = [
'WsRestGetMembersRequest' => [
'actAsSubjectLookup' => [
'subjectId' => $actorUserId
],
'wsGroupLookups' => [
['groupName' => $groupName]
],
'subjectAttributeNames' => ['name']
]
];

$actionEndpoint = "/groups";

try {
$results = $this->getGrouperUserMemberships($userId, $groupType);
$results = $this->http->sendRequest('POST',
$actionEndpoint,
json_encode($usersToShow, JSON_THROW_ON_ERROR));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
throw $e;
}
return $results['WsGetMembershipsResults']['wsGroups'] ?? [];

// 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' => ''
]
];
}

return $results['WsGetMembersResults']['results'][0]['wsSubjects'] ?? [];
}

/**
* Gets all groups in Grouper where user is an admin/owner or has update privs
* Gets all available Optin/OptOut groups in Grouper
*
* Returns Optin/OptOut groups that can be joined/left
*
* @param string $userId
* @param string $groupType
*
* @return array Array of groups from Grouper
* @throws Exception
* @return array Optin groups from Grouper
* @throws GrouperLiteWidgetException
*/
public function getOwnedGroups(string $userId): array
public function getOptionalGroups(string $userId, string $groupType): array
{
try {
$resultsAdmin = $this->getGrouperUserMemberships($userId, GrouperGroupTypeEnum::ADMIN);
$resultsUpdate = $this->getGrouperUserMemberships($userId, GrouperGroupTypeEnum::UPDATE);
$results = $this->getGrouperUserMemberships($userId, $groupType);
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
throw $e;
}

return $this->removeDuplicates($resultsAdmin['WsGetMembershipsResults']['wsGroups'] ?? [],
$resultsUpdate['WsGetMembershipsResults']['wsGroups'] ?? []);
return $results['WsGetMembershipsResults']['wsGroups'] ?? [];
}

/**
Expand Down Expand Up @@ -582,7 +560,7 @@ public function isMemberOfGroup(string $groupName, string $userId): bool

$groupNameEncoded = $this->urlGrouperEncode($groupName);

$actionEndpoint = "/groups"
$actionEndpoint = '/groups'
. "/{$groupNameEncoded}/members/{$userId}";
try {
$results = $this->http->sendRequest('GET', $actionEndpoint);
Expand All @@ -595,37 +573,6 @@ public function isMemberOfGroup(string $groupName, string $userId): bool
&& $results['WsHasMemberLiteResul']['resultMetadata']['resultCode'] === GrouperResultCodesEnum::IS_MEMBER;
}

/**
* Removes duplicates where the user is the owner and the updater of the group. Just one line instead of two.
*
* @param array $arrOne
* @param array $arrTwo
* @return array
*/
public function removeDuplicates(array $arrOne, array $arrTwo)
{
//Determine which array is bigger and use as base
$countOne = count($arrOne);
$countTwo = count($arrTwo);
if ($countOne >= $countTwo) {
$arrL = $arrOne;
$arrS = $arrTwo;
} else {
$arrL = $arrTwo;
$arrS = $arrOne;
}

foreach ($arrL as $large) {
foreach ($arrS as $key => $val) {
if ($large['uuid'] == $val['uuid']) {
unset($arrS[$key]);
}
}
}

return array_merge_recursive($arrL, $arrS);
}

/**
* Remove a member from a specific Grouper Group
*
Expand Down Expand Up @@ -672,7 +619,11 @@ public function removeGroupMember(string $actAsUserId, string $groupName, string
throw $e;
}

// Parse out relevant records to send front end
if(isset($results['error']) && $results['error']) {
$cakeExceptionClass = $results['cakeException'];
throw new $cakeExceptionClass($results['message']);
}

return isset($results['WsDeleteMemberResults']['resultMetadata']['resultCode'])
&& $results['WsDeleteMemberResults']['resultMetadata']['resultCode'] === GrouperResultCodesEnum::SUCCESS;
}
Expand Down
Loading

0 comments on commit 076c938

Please sign in to comment.