Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #11 from Ioannis/Code_cleanup
Code cleanup
aaschenbrener committed Feb 28, 2024
2 parents a522187 + 938e45b commit b33433d
Showing 8 changed files with 302 additions and 315 deletions.
132 changes: 71 additions & 61 deletions Controller/GrouperGroupsController.php
@@ -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']);
@@ -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'));
@@ -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);
}

/**
@@ -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);
}

/**
@@ -289,7 +292,7 @@ public function groupOwnerApi() {
$scope['searchpage'] = 'ownerGroups';
$errorHint = 'Search';
} else {
$scope['method'] = 'ownerGroups';
$scope['method'] = 'getOwnedGroups';
$errorHint = '';
}
try {
@@ -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);
}

/**
@@ -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);
}

/**

0 comments on commit b33433d

Please sign in to comment.