Skip to content

Commit

Permalink
Changes for add/delete members from group
Browse files Browse the repository at this point in the history
  • Loading branch information
Axel Stohn authored and Axel Stohn committed Mar 13, 2022
1 parent 8e23cff commit 0e7f631
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 6 deletions.
79 changes: 79 additions & 0 deletions Controller/GrouperGroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,83 @@ public function groupSubscribers() {
$this->set('_serialize', 'subscribers');
}

/**
* Add a new member to a group
* Called from all pages via AJAX call
*
*/
public function addSubscriber() {
$groupName = urldecode($this->request->query['group']);
$addUserId = urldecode($this->request->query['userId']);

if ($this->request->is('ajax')) {
$ajax = true;
}

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

//Set initial
$scope = [
'groupName' => $groupNameFormatted,
'addUserId' => $addUserId
];

try {
$subscribers = $this->GrouperGroup->addMemberToGroup($scope, $this->userId);

} 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'));
}

$this->set(compact('subscribers'));
$this->set('_serialize', 'subscribers');
}

/**
* Remove a member from a group
* Called from all pages via AJAX call
*
*/
public function removeSubscriber() {
$groupName = urldecode($this->request->query['group']);
$remUserId = urldecode($this->request->query['userId']);

if ($this->request->is('ajax')) {
$ajax = true;
}

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

//Set initial
$scope = [
'groupName' => $groupNameFormatted,
'remUserId' => $remUserId
];

try {
$subscribers = $this->GrouperGroup->removeMemberToGroup($scope, $this->userId);

} 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'));
}

$this->set(compact('subscribers'));
$this->set('_serialize', 'subscribers');
}

/**
* Listing of all Grouper Groups owned/admin by User Or search those Grouper Groups
Expand Down Expand Up @@ -538,6 +615,8 @@ function isAuthorized() {
$p['groupowner'] = true;
$p['groupmember'] = true;
$p['groupSubscribers'] = true;
$p['addSubscriber'] = true;
$p['removeSubscriber'] = true;
$p['groupoptin'] = true;
$p['emaillistsoptin'] = true;
$p['emaillistsmember'] = true;
Expand Down
76 changes: 73 additions & 3 deletions Lib/GrouperApiAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ public function getGrouperMemberOfGroups(array $queryData) {

//Build request logic
$userId = $queryData['userId'];
$connectionUrl = "{$this->config['fullUrl']}/subjects/$userId/groups";
// $connectionUrl = "{$this->config['fullUrl']}/subjects/$userId/groups?";
// $connectionUrl .= "wsLiteObjectType=WsRestGetGroupsLiteRequest&actAsSubjectSourceId=ldap&actAsSubjectId=$userId";
$connectionUrl = "{$this->config['fullUrl']}/subjects/$userId/groups?";
$connectionUrl .= "wsLiteObjectType=WsRestGetGroupsLiteRequest&actAsSubjectSourceId=ldap&actAsSubjectId=$userId";

try {
$results = $this->http->sendRequest('GET', $connectionUrl);
Expand Down Expand Up @@ -297,6 +296,77 @@ public function getMembersInGroup(array $queryData) {
return array();
}

/**
* Add a member to a specific Grouper Group
*
* @param array $queryData Array of conditions for querying
* @return array Listing of Members belonging to Grouper Group
* @throws GrouperLiteException
*/
public function addMemberToGroup(array $queryData) {

try {
$groupName = $queryData['groupName'];

//Build request logic
$usersToShow = array(
"WsRestAddMemberRequest" => array(
"subjectLookups" => array(
array("subjectId" => $queryData['addUserId']),
),
"replaceAllExisting" => "F",
"actAsSubjectLookup" => array(
"subjectId" => $queryData['userId']
)
)
);

$this->http->setHeader(array('Content-Type' => 'application/json', 'Accept' => 'application/json'));
$connectionUrl = "{$this->config['fullUrl']}/groups/$groupName/members";

$results = $this->http->sendRequest('PUT', $connectionUrl, json_encode($usersToShow));

// Parse out relevant records to send front end
if (isset($results['WsAddMemberResults']['results'][0]['resultMetadata']) && $results['WsAddMemberResults']['results'][0]['resultMetadata'] != NULL) {
return $results['WsAddMemberResults']['results'][0]['resultMetadata']['resultCode'];
}
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
throw $e;
}

return array();
}

/**
* Remove a member from a specific Grouper Group
*
* @param array $queryData Array of conditions for querying
* @return array Listing of Members belonging to Grouper Group
* @throws GrouperLiteException
*/
public function removeMemberToGroup(array $queryData) {

try {
$groupName = $queryData['groupName'];
$remUserId = $queryData['remUserId'];

$this->http->setHeader(array('Content-Type' => 'application/json', 'Accept' => 'application/json'));
$connectionUrl = "{$this->config['fullUrl']}/groups/$groupName/members/$remUserId";
$results = $this->http->sendRequest('PUT', $connectionUrl);

// Parse out relevant records to send front end
if (isset($results['WsGetMembersResults']['results'][0]['wsSubjects']) && $results['WsGetMembersResults']['results'][0]['wsSubjects'] != NULL) {
return $results['WsGetMembersResults']['results'][0]['wsSubjects'];
}
} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
throw $e;
}

return array();
}

/**
* ======================== NOT BEING USED ========================
*
Expand Down
58 changes: 58 additions & 0 deletions Model/GrouperGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,64 @@ public function membersInGroup(array $conditions, string $userId) {
}
}

/**
* Add a member to a specific Grouper Group
*
* @param array $conditions Listing of conditions for display of records
* @param string $userId Id of User
* @return array Listing of members in requested Grouper Group
* @throws GrouperLiteException Captured in Controller
*
*/
public function addMemberToGroup(array $conditions, string $userId) {
$this->initApi();

$conditions['userId'] = $userId;

try {
$groupMembers = $this->grouperAPI->addMemberToGroup($conditions);

if ($groupMembers != 'SUCCESS'){
return [];
}

return $groupMembers;

} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
throw $e;
}
}

/**
* Remove a member from a specific Grouper Group
*
* @param array $conditions Listing of conditions for display of records
* @param string $userId Id of User
* @return array Listing of members in requested Grouper Group
* @throws GrouperLiteException Captured in Controller
*
*/
public function removeMemberToGroup(array $conditions, string $userId) {
$this->initApi();

$conditions['userId'] = $userId;

try {
$groupMembers = $this->grouperAPI->removeMemberToGroup($conditions);

if (count($groupMembers) < 1){
return $groupMembers;
}

return $groupMembers['resultCode'];

} catch (Exception $e) {
CakeLog::write('error', __METHOD__ . ': An error occurred');
throw $e;
}
}

/**
* ======================== NOT BEING USED ========================
*
Expand Down
6 changes: 3 additions & 3 deletions View/Elements/Components/subscriberList.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
)
); ?>';

var removeUrl = '<?php print $this->Html->url(
var addUrl = '<?php print $this->Html->url(
array(
'plugin' => "grouper_lite",
'controller' => 'grouper_groups',
Expand Down Expand Up @@ -143,7 +143,7 @@
var user = $(ev.target).data('user');
$.ajax({
method: 'DELETE',
url: removeUrl + '?userId=' + user,
url: removeUrl + '?group=' + group + '&userId=' + user,
dataType: 'json',
success: function(data) {
load();
Expand All @@ -162,7 +162,7 @@
function onAddUser(user, group, field) {
$.ajax({
method: 'POST',
url: removeUrl + '?group=' + group + '&userId=' + user,
url: addUrl + '?group=' + group + '&userId=' + user,
dataType: 'json',
success: function(data) {
load();
Expand Down

0 comments on commit 0e7f631

Please sign in to comment.