Skip to content

Commit

Permalink
Rename and cleanup GrouperAPI methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Feb 22, 2024
1 parent a953284 commit ddfccf7
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 150 deletions.
22 changes: 7 additions & 15 deletions Controller/GrouperGroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,14 @@ public function groupSubscribers(): void
$groupName = urldecode($this->request->query['groupname']);
$subscribers = 0;

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

try {
$subscribers = $this->GrouperGroup->getGrouperGroupMembers($this->userId,
$groupNameFormatted,
$groupName,
$this->CoGrouperLiteWidget->getConfig());
CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($subscribers, true));

} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': ' . var_export($e->getMessage(), true));

Expand Down Expand Up @@ -194,15 +192,14 @@ public function addSubscriber(): void
$resultAdd = 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 {
$resultAdd = $this->GrouperGroup->addGrouperGroupMember($this->userId,
$groupNameFormatted,
$groupName,
$addUserId,
$this->CoGrouperLiteWidget->getConfig());
CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($resultAdd, true));
} catch (Exception $e) {
// $this->restResponse(HttpStatusCodesEnum::HTTP_UNAUTHORIZED, ErrorsEnum::NotAdded);
CakeLog::write('error', __METHOD__ . ': ' . var_export($e->getMessage(), true));
Expand Down Expand Up @@ -246,8 +243,6 @@ public function findSubscriber(): void
* Remove a member from a group
* Called from all pages via AJAX call
*
* TODO: We should probably save extra metadata in a data attribute and then pass that attribute to the request and not
* the Human Readable name
* TODO: We need to appropriately handle Unathenticated call. We have to bubble up the response and do something.
*/
public function removeSubscriber(): void
Expand All @@ -265,8 +260,6 @@ public function removeSubscriber(): void
$groupNameFormatted,
$remUserId,
$this->CoGrouperLiteWidget->getConfig());

CakeLog::write('debug', __METHOD__ . '::response: ' . var_export($resultRemove, true));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': ' . var_export($e->getMessage(), true));
}
Expand Down Expand Up @@ -304,7 +297,6 @@ public function groupOwnerApi() {
$func = $scope['method'];
$groupowners = $this->GrouperGroup->$func($scope,
$this->CoGrouperLiteWidget->getConfig());
CakeLog::write('debug', __METHOD__ . "::response: " . var_export($groupowners, true));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . "::{$errorHint}: " . var_export($e->getMessage(), true));
$this->restResponse(HttpStatusCodesEnum::HTTP_INTERNAL_SERVER_ERROR, ErrorsEnum::Exception);
Expand Down
179 changes: 85 additions & 94 deletions Lib/GrouperApiAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,36 +206,28 @@ public function createGroupWithTemplate(array $queryData): array
*
* Create or Update a Group where User is Admin/Owner
*
* @param array $queryData Array of conditions and data adding new Grouper Group
* @param string $actAsUserId
* @param string $groupName
* @param string $stem
* @param string $groupDescription
*
* @return bool True if added or updated successful
* @throws GrouperLiteWidgetException|JsonException
* @throws GrouperLiteWidgetException
* @throws JsonException
*/
public function createUpdateGroup(array $queryData)
public function createUpdateGroup(string $actAsUserId, string $groupName, string $stem, string $groupDescription ): bool
{

$groupName = htmlentities($queryData['name']);
$stemName = htmlentities($queryData['stem']);
$userId = $queryData['userId'];
$groupDescription = htmlentities($queryData['description']);

//Group name may be in "friendly" format, so need to do some small conversions before saving.
$newGroupName = ucfirst(strtolower($groupName));
$newGroupName = str_replace(' ', '', $newGroupName);

$newGroupToSave = "$stemName:$newGroupName";

//Build request logic
$groupToSave = [
'WsRestGroupSaveRequest' => [
'actAsSubjectLookup' => [
'subjectId' => $userId
'subjectId' => $actAsUserId
],
'wsGroupToSaves' => [
[
'wsGroup' => [
'description' => $groupDescription,
'name' => $newGroupToSave,
'name' => "{$stem}:{$groupName}",
'displayExtension' => $groupName
],
'wsGroupLookup' => [
Expand Down Expand Up @@ -322,14 +314,14 @@ public function deleteGroupWithTemplate(array $queryData)
*
* Grouper Group information plus a listing of attributes in Grouper for that given Group
*
* @param array $queryData Array of conditions for querying
* @param string $groupName
*
* @return array Record of Grouper attributes for given GroupName
* @throws GrouperLiteWidgetException|JsonException
* @throws GrouperLiteWidgetException
* @throws JsonException
*/
public function getGrouperGroupInfo(array $queryData)
public function getGrouperGroupInfo(string $groupName)
{
$groupName = $queryData['groupName'];
$groupInfo = [];

//Build request logic
Expand Down Expand Up @@ -436,7 +428,7 @@ public function getGrouperGroupMembers(string $actorUserId, string $groupName):

// Parse out relevant records to send front end
if(isset($results['WsGetMembersResults']['results'][0]['resultMetadata']['resultCode'])
&& $results['WsGetMembersResults']['results'][0]['resultMetadata']['resultCode'] == 'GROUP_NOT_FOUND') {
&& $results['WsGetMembersResults']['results'][0]['resultMetadata']['resultCode'] === GrouperResultCodesEnum::GROUP_NOT_FOUND) {
return [
[
'sourceId' => 'NoAccess',
Expand All @@ -449,21 +441,77 @@ public function getGrouperGroupMembers(string $actorUserId, string $groupName):
return $results['WsGetMembersResults']['results'][0]['wsSubjects'] ?? [];
}

/**
* Used for requests made to Membership endpoint in Grouper WS
*
* @param string $userId
* @param string $groupType
*
* @return array Group records associated to calling method
* @throws GrouperLiteWidgetException
* @see getOwnedStems()
* @see getOptinGroups()
* @see getOptOutGroups()
* @see getOwnedGroups()
*/
private function getGrouperUserMemberships(string $userId, string $groupType): array
{
if (in_array($groupType, [
GrouperGroupTypeEnum::OPTINS,
GrouperGroupTypeEnum::OPTOUTS], true)
) {
$subjectId = 'GrouperAll';
} elseif (in_array($groupType, [
GrouperGroupTypeEnum::ADMIN,
GrouperGroupTypeEnum::UPDATE,
GrouperGroupTypeEnum::STEM_ADMIN], true)
) {
$subjectId = $userId;
} else {
CakeLog::write('error', __METHOD__ . ": Option of {$groupType} is not supported");
throw new GrouperLiteWidgetException("Option of {$groupType} is not supported");
}

//Build request logic
$data = [];
$data['WsRestGetMembershipsRequest']['fieldName'] = $groupType;
$data['WsRestGetMembershipsRequest']['wsSubjectLookups'][0]['subjectId'] = $subjectId;

if ($groupType == GrouperGroupTypeEnum::OPTINS
|| $groupType == GrouperGroupTypeEnum::OPTOUTS) {
// Build request logic, 2 subjectId's, second is for when user in "Secret" Optin/Optout Group
$data['WsRestGetMembershipsRequest']['wsSubjectLookups'][1]['subjectId'] = $userId;
}

$actionEndpoint = '/memberships';

try {
$results = $this->http->sendRequest('POST',
$actionEndpoint,
json_encode($data));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
throw $e;
}

return $results;
}

/**
* Gets all available Optin/OptOut groups in Grouper
*
* Returns Optin/OptOut groups that can be joined/left
*
* @param array $queryData Array of conditions for querying
* @param string $userId
* @param string $groupType
*
* @return array Optin groups from Grouper
* @throws GrouperLiteWidgetException
*
*/
public function getOptionalGroups(array $queryData)
public function getOptionalGroups(string $userId, string $groupType): array
{

try {
$results = $this->useMembershipUrl($queryData);
$results = $this->getGrouperUserMemberships($userId, $groupType);
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
throw $e;
Expand All @@ -474,21 +522,16 @@ public function getOptionalGroups(array $queryData)
/**
* Gets all groups in Grouper where user is an admin/owner or has update privs
*
* @param array $queryData Array of conditions for querying
* @return array Array of groups from Grouper
* @throws GrouperLiteWidgetException
* @param string $userId
*
* @return array Array of groups from Grouper
* @throws Exception
*/
public function getOwnedGroups(array $queryData)
public function getOwnedGroups(string $userId): array
{
try {
$queryData['groupType'] = 'admin';
$resultsAdmin = $this->useMembershipUrl($queryData);
CakeLog::write('debug', __METHOD__ . '::response admins: ' . var_export($resultsAdmin, true));

$queryData['groupType'] = 'update';
$resultsUpdate = $this->useMembershipUrl($queryData);
CakeLog::write('debug', __METHOD__ . '::response updates: ' . var_export($resultsUpdate, true));
$resultsAdmin = $this->getGrouperUserMemberships($userId, GrouperGroupTypeEnum::ADMIN);
$resultsUpdate = $this->getGrouperUserMemberships($userId, GrouperGroupTypeEnum::UPDATE);
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
throw $e;
Expand All @@ -503,16 +546,15 @@ public function getOwnedGroups(array $queryData)
*
* Gets all Stems/Folders where User is admin/owner
*
* @param array $queryData Array of conditions for querying
* @param string $userId
*
* @return array Array of Stems/Folders from Grouper
* @throws GrouperLiteWidgetException
*/
public function getOwnedStems(array $queryData)
public function getOwnedStems(string $userId): array
{
$queryData['groupType'] = 'stemAdmin';

try {
$results = $this->useMembershipUrl($queryData);
$results = $this->getGrouperUserMemberships($userId, GrouperGroupTypeEnum::ADMIN);
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
throw $e;
Expand Down Expand Up @@ -647,55 +689,4 @@ public function urlGrouperEncode(string $url): string
$url = urlencode($url);
return str_replace(':', '%3A', $url);
}

/**
* Used for requests made to Membership endpoint in Grouper WS
*
* @param array $queryData Array of conditions for querying
* @return array Group records associated to calling method
* @throws GrouperLiteWidgetException
*
* @see getOwnedStems()
* @see getOptinGroups()
* @see getOptOutGroups()
* @see getOwnedGroups()
*/
private function useMembershipUrl(array $queryData)
{
$groupType = $queryData['groupType'];
$userId = $queryData['userId'];

if ($groupType == 'optins' || $groupType == 'optouts') {
$subjectId = 'GrouperAll';
} elseif ($groupType == 'admin' || $groupType == 'update' || $groupType == 'stemAdmin') {
$subjectId = $userId;
} else {
CakeLog::write('error', __METHOD__ . ": Option of {$groupType} is not supported");
throw new GrouperLiteWidgetException("Option of {$groupType} is not supported");
}

//Build request logic
$groupsToShow = [];
$groupsToShow['WsRestGetMembershipsRequest']['fieldName'] = $groupType;
$groupsToShow['WsRestGetMembershipsRequest']['wsSubjectLookups'][0]['subjectId'] = $subjectId;

if ($groupType == 'optins'
|| $groupType == 'optouts') {
// Build request logic, 2 subjectId's, second is for when user in "Secret" Optin/Optout Group
$groupsToShow['WsRestGetMembershipsRequest']['wsSubjectLookups'][1]['subjectId'] = $userId;
}

$actionEndpoint = "/memberships";

try {
$results = $this->http->sendRequest('POST',
$actionEndpoint,
json_encode($groupsToShow));
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
throw $e;
}

return $results;
}
}
8 changes: 8 additions & 0 deletions Lib/enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ class GrouperResultCodesEnum {
const NO_SUCH_OBJECT = 'NO_SUCH_OBJECT';
const SUCCESS_NO_INFO = 'SUCCESS_NO_INFO';
const EXECUTE_FAILED = 'EXECUTE_FAILED';
}

class GrouperGroupTypeEnum {
const OPTINS = 'optins';
const OPTOUTS = 'optouts';
const ADMIN = 'admin';
const UPDATE = 'update';
const STEM_ADMIN = 'stemAdmin';
}
Loading

0 comments on commit ddfccf7

Please sign in to comment.