Skip to content

Commit

Permalink
Simplify add/remove from group backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Feb 28, 2024
1 parent a522187 commit 758b2f5
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 147 deletions.
52 changes: 26 additions & 26 deletions Controller/GrouperGroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ public function groupSubscribers(): void
// : $groupName;

try {
$subscribers = $this->GrouperGroup->getGrouperGroupMembers($this->userId,
$groupName,
$this->CoGrouperLiteWidget->getConfig());
$subscribers = $this->GrouperGroup->getGroupMembers($this->userId,
$groupName,
$this->CoGrouperLiteWidget->getConfig());
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': ' . var_export($e->getMessage(), true));

Expand All @@ -184,31 +184,31 @@ public function groupSubscribers(): void
* Add a new member to a group
* Called from all pages via AJAX call
*
* @throws JsonException
* @throws Exception
*/
public function addSubscriber(): void
{
$groupName = urldecode($this->request->query['group']);
$addUserId = urldecode($this->request->query['userId']);
$resultAdd = false;

//Need to see if coming from AdHoc or from a WG (Working Group)
// Need to see if coming from AdHoc or from a WG (Working Group)
// todo: Investigate further
// XXX groupJoin is not using this formatted syntax???
// $groupNameFormatted = strpos($groupName, ':') === false ? 'ref:incommon-collab:' . $groupName . ':users'
// : $groupName;

try {
$resultAdd = $this->GrouperGroup->addGrouperGroupMember($this->userId,
$groupName,
$addUserId,
$this->CoGrouperLiteWidget->getConfig());
$this->GrouperGroup->addGroupMember($this->userId,
$groupName,
$addUserId,
$this->CoGrouperLiteWidget->getConfig());
} catch (Exception $e) {
// $this->restResponse(HttpStatusCodesEnum::HTTP_UNAUTHORIZED, ErrorsEnum::NotAdded);
CakeLog::write('error', __METHOD__ . ': ' . var_export($e->getMessage(), true));
$this->restResponse(HttpStatusCodesEnum::HTTP_NOT_FOUND, ErrorsEnum::Exception);
throw $e;
}

$this->set(compact($resultAdd ? GrouperResultCodesEnum::SUCCESS : ''));
$this->set('_serialize', 'resultAdd');

$this->restResponse(HttpStatusCodesEnum::HTTP_OK);
}

/**
Expand Down Expand Up @@ -255,10 +255,10 @@ public function removeSubscriber(): void
: $groupName;

try {
$resultRemove = $this->GrouperGroup->removeGrouperGroupMember($this->userId,
$groupNameFormatted,
$remUserId,
$this->CoGrouperLiteWidget->getConfig());
$resultRemove = $this->GrouperGroup->removeGroupMember($this->userId,
$groupNameFormatted,
$remUserId,
$this->CoGrouperLiteWidget->getConfig());
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': ' . var_export($e->getMessage(), true));
}
Expand Down Expand Up @@ -432,22 +432,22 @@ public function groupCreateTemplate()
*/
public function joinGroup(): void
{
// 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']);
$resultAdd = false;

try {
// Add myself
$resultAdd = $this->GrouperGroup->joinGroup($this->userId,
$name,
$this->CoGrouperLiteWidget->getConfig());
$this->GrouperGroup->addGroupMember($this->userId,
$name,
$this->userId,
$this->CoGrouperLiteWidget->getConfig());
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': ' . var_export($e->getMessage(), true));
// $this->restResponse(HttpStatusCodesEnum::HTTP_UNAUTHORIZED, ErrorsEnum::NotAdded);
$this->restResponse(HttpStatusCodesEnum::HTTP_NOT_FOUND, ErrorsEnum::Exception);
throw $e;
}

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

/**
Expand Down
32 changes: 16 additions & 16 deletions Lib/GrouperApiAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@
*/
class GrouperApiAccess
{
// Array for storing settings needed in call to Grouper
private $config;

// Instance of Grouper HTTP Wrapper class
private $http;
private GrouperHTTPWrapper $http;

// String used to build out Grouper WS URL
private $_urlServlet = '/grouper-ws/servicesRest/';
private string $_urlServlet = '/grouper-ws/servicesRest/';

/**
* GrouperApiAccess constructor.
Expand Down Expand Up @@ -84,9 +81,9 @@ public function __construct(array $cfg)
* @throws GrouperLiteWidgetException
* @throws JsonException
*/
public function addGrouperGroupMember(string $actAsUserId,
string $groupName,
string $addUserId): bool
public function addGroupMember(string $actAsUserId,
string $groupName,
string $addUserId): bool
{
if(empty($actAsUserId)
|| empty($groupName)
Expand Down Expand Up @@ -121,6 +118,11 @@ public function addGrouperGroupMember(string $actAsUserId,
throw $e;
}

if(isset($results['error']) && $results['error']) {
$cakeExceptionClass = $results['cakeException'];
throw new $cakeExceptionClass($results['message']);
}

return isset($results['WsAddMemberResults']['wsGroupAssigned'])
&& $results['WsAddMemberResults']['wsGroupAssigned']['name'] === urldecode($groupName);
}
Expand Down Expand Up @@ -262,7 +264,7 @@ public function createUpdateGroup(string $actAsUserId, string $groupName, string
* @throws GrouperLiteWidgetException|JsonException
*
*/
public function deleteGroupWithTemplate(array $queryData)
public function deleteGroupWithTemplate(array $queryData): bool
{
$workingGroupExt = $queryData['workingGroupExt'];
$userId = $queryData['userId'];
Expand Down Expand Up @@ -320,7 +322,7 @@ public function deleteGroupWithTemplate(array $queryData)
* @throws GrouperLiteWidgetException
* @throws JsonException
*/
public function getGrouperGroupInfo(string $groupName)
public function getGroupInfo(string $groupName): array
{
$groupInfo = [];

Expand Down Expand Up @@ -371,7 +373,7 @@ public function getGrouperGroupInfo(string $groupName)
*
* @throws GrouperLiteWidgetException
*/
public function getUserGrouperGroupMemberships(string $actorUserId, string $userId): array
public function getUserGroupMemberships(string $actorUserId, string $userId): array
{
if(empty($userId)) {
return [];
Expand Down Expand Up @@ -400,7 +402,7 @@ public function getUserGrouperGroupMemberships(string $actorUserId, string $user
* @throws GrouperLiteWidgetException
* @throws JsonException
*/
public function getGrouperGroupMembers(string $actorUserId, string $groupName): array
public function getGroupMembers(string $actorUserId, string $groupName): array
{
//Build request logic
$usersToShow = [
Expand Down Expand Up @@ -572,7 +574,7 @@ public function getOwnedStems(string $userId): array
*
* @throws GrouperLiteWidgetException
*/
public function isMemberOfGrouperGroup(string $groupName, string $userId): bool
public function isMemberOfGroup(string $groupName, string $userId): bool
{
if(empty($userId) || empty($groupName)) {
return [];
Expand Down Expand Up @@ -636,9 +638,7 @@ public function removeDuplicates(array $arrOne, array $arrTwo)
* @throws GrouperLiteWidgetException
* @throws JsonException
*/
public function removeGrouperGroupMember(string $actAsUserId,
string $groupName,
string $removeUserId): bool
public function removeGroupMember(string $actAsUserId, string $groupName, string $removeUserId): bool
{
if(empty($actAsUserId)
|| empty($groupName)
Expand Down
41 changes: 32 additions & 9 deletions Lib/GrouperHTTPWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,23 +190,46 @@ public function sendRequest(string $method, string $endPoint, string $body = '')
$apiResults = $this->request($this->_request);
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred: ' . var_export($e->getMessage(), true));
throw new GrouperLiteWidgetException('An error occurred talking to Grouper WS');
throw new RuntimeException(_txt('er.notfound-b', array(
_txt('pl.grouperlite.crumb.root'))
));
}

// Call may return non-200, which may be okay depending on call
if (!$apiResults->isOk()) {
CakeLog::write('error', __METHOD__ . ': Grouper WS returned non-200 of ' . var_export($apiResults->body(), true));
$successHeader = $apiResults->getHeader('X-Grouper-success');
if (empty($successHeader)) {
throw new RuntimeException('Web service did not even respond!');
}

$payload = json_decode($apiResults->body(), true);

CakeLog::write('debug', __METHOD__ . '::payload: ' . var_export($payload, true));

$successHeader = $apiResults->getHeader('X-Grouper-success');
if (empty($successHeader)) {
throw new RuntimeException('Web service did not even respond!');
$code = $apiResults->code;
$reasonPhrase = $apiResults->reasonPhrase;

// The request returned with Failed status
if($successHeader == 'F' && !empty($payload)) {
CakeLog::write('debug', __METHOD__ . '::body ' . var_export($apiResults->body(), true));
CakeLog::write('debug', __METHOD__ . '::headers: ' . var_export($apiResults->headers, true));

// I need to pop the element since the key is request specific.
// For example, it is WsAddMemberResults for an Add request
$error_payload = array_pop($payload);
$constant_error = 'GrouperCodesToExceptionClassEnum::' . $error_payload['resultMetadata']['resultCode'];
$cakeException = defined($constant_error) ? constant($constant_error) : 'BadRequestException';
if($code == HttpStatusCodesEnum::HTTP_UNAUTHORIZED) {
$cakeException = 'UnauthorizedException';
}

return [
'error' => true,
'grouperCode' => $error_payload['resultMetadata']['resultCode'],
'message' => $error_payload['resultMetadata']['resultMessage'],
'reasonPhrase' => $reasonPhrase,
'cakeException' => $cakeException
];
}

CakeLog::write('debug', __METHOD__ . '::payload: ' . var_export($payload, true));

return $payload;
}
}
32 changes: 21 additions & 11 deletions Lib/enum.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

class ErrorsEnum {
const Exception = array('status' => 'ERROR', 'message' => 'EXCEPTION');
const NotDeleted = array('status' => 'ERROR', 'message' => 'NOT DELETED');
const NotAdded = array('status' => 'ERROR', 'message' => 'NOT ADDED');
const Error = array('status' => 'ERROR', 'message' => 'ERROR');
const NoAccess = array('status' => 'ERROR', 'message' => 'NO ACCESS');
const Error = ['status' => 'ERROR', 'message' => 'ERROR'];
const Exception = ['status' => 'ERROR', 'message' => 'EXCEPTION'];
const NoAccess = ['status' => 'ERROR', 'message' => 'NO ACCESS'];
const NotAdded = ['status' => 'ERROR', 'message' => 'NOT ADDED'];
const NotDeleted = ['status' => 'ERROR', 'message' => 'NOT DELETED'];
}

class GrouperSpecialGroups {
Expand All @@ -20,19 +20,29 @@ class GrouperSpecialGroups {
}

class GrouperResultCodesEnum {
const IS_MEMBER = 'IS_MEMBER';
const SUCCESS = 'SUCCESS';
const EXECUTE_FAILED = 'EXECUTE_FAILED';
const GROUP_NOT_FOUND = 'GROUP_NOT_FOUND';
const SUBJECT_NOT_FOUND = 'SUBJECT_NOT_FOUND';
const IS_MEMBER = 'IS_MEMBER';
const NO_SUCH_OBJECT = 'NO_SUCH_OBJECT';
const PROBLEM_WITH_ASSIGNMENT = 'PROBLEM_WITH_ASSIGNMENT';
const SUBJECT_NOT_FOUND = 'SUBJECT_NOT_FOUND';
const SUCCESS = 'SUCCESS';
const SUCCESS_NO_INFO = 'SUCCESS_NO_INFO';
const EXECUTE_FAILED = 'EXECUTE_FAILED';
}

class GrouperCodesToExceptionClassEnum {
const EXECUTE_FAILED = 'BadRequestException';
const GROUP_NOT_FOUND = 'NotFoundException';
const NO_SUCH_OBJECT = 'NotFoundException';
const PROBLEM_WITH_ASSIGNMENT = 'BadRequestException';
const SUBJECT_NOT_FOUND = 'NotFoundException';
const SUCCESS_NO_INFO = 'MethodNotAllowedException';
}

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

0 comments on commit 758b2f5

Please sign in to comment.