Skip to content

Code cleanup #11

Merged
merged 5 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 71 additions & 61 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 @@ -149,31 +149,24 @@ 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'
// : $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));

$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'));
Expand All @@ -184,31 +177,38 @@ 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
{
$this->layout = null;
$this->autoRender = false;

$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());
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) {
// $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_CREATED);
}

/**
Expand Down Expand Up @@ -243,32 +243,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->removeGrouperGroupMember($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 +292,7 @@ public function groupOwnerApi() {
$scope['searchpage'] = 'ownerGroups';
$errorHint = 'Search';
} else {
$scope['method'] = 'ownerGroups';
$scope['method'] = 'getOwnedGroups';
$errorHint = '';
}
try {
Expand Down Expand Up @@ -429,25 +432,32 @@ public function groupCreateTemplate()
/**
* Process to join a group displayed on the "Optin" page
*
* @throws Exception
*/
public function joinGroup(): void
{
$name = urldecode($this->request->query['GroupName']);
$resultAdd = false;
$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
$groupName = urldecode($this->request->query['GroupName']);

try {
// Add myself
$resultAdd = $this->GrouperGroup->joinGroup($this->userId,
$name,
$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));
// $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_CREATED);
}

/**
Expand All @@ -456,25 +466,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
Loading