Skip to content

Commit

Permalink
Improve code readability. Remove non-intuitive code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Feb 22, 2024
1 parent 8505de5 commit a953284
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 135 deletions.
123 changes: 19 additions & 104 deletions Controller/GrouperGroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,11 @@ public function groupSubscribers(): void
//Need to see if coming from AdHoc or from a WG (Working Group)
$groupNameFormatted = strpos($groupName, ':') === false ? 'ref:incommon-collab:' . $groupName . ':users'
: $groupName;
//Set initial
$scope = [
'groupName' => $groupNameFormatted
];

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

} catch (Exception $e) {
Expand Down Expand Up @@ -219,109 +215,28 @@ public function addSubscriber(): void
}

/**
* The majority of this code is copied from the find() function in the CoPeopleController that was added by
* Arlen. Eventually this will become a component, so for now just tweaking the code needed for the Grouper plugin
* to work.
* Perform a "keyword" search for CO People, sort of like the CO Dashboard
* cross controller search, but intended specifically for "people finder"
* search while you type API calls.
*
* @since COmanage Registry v3.3.0
*/
public function findSubscriber() {
$findUserId = urldecode($this->request->query['term']);
$co = urldecode($this->request->query['co']);

$mode = urldecode($this->request->query['mode']);

// jquery Autocomplete sends the search as url?term=foo
$coPersonIds = array();
if(!empty($findUserId)) {
// Leverage model specific keyword search

// Note EmailAddress and Identifier don't support substring search
foreach(array('Name', 'EmailAddress', 'Identifier') as $m) {
$hits = $this->CoPerson->$m->search($co, $findUserId, 25);
public function findSubscriber(): void
{
$this->autoRender = false;
$this->request->allowMethod('ajax');
$this->layout = 'ajax';

foreach($hits as $hit) {
if ($hit['CoPerson']['status'] == 'A') {
$coPersonIds[] = $hit['CoPerson']['id'];
}
}
// $coPersonIds = array_merge($coPersonIds, Hash::extract($hits, '{n}.CoPerson.id'));
}
// What search mode should we use?
if(empty($this->request->query['mode'])) {
$this->Api->restResultHeader(HttpStatusCodesEnum::HTTP_BAD_REQUEST, 'Mode Not Specified');
return;
}

$coPersonIds = array_unique($coPersonIds);

// Look up additional information to provide hints as to which person is which.
// We only do this when there are relatively small numbers of results to
// avoid making a bunch of database queries early in the search.

$matches = array();

if(count($coPersonIds) > 100) {
// We don't return large sets to avoid slow performance

$this->response->type('json');
$this->response->statusCode(401);
$this->response->body(json_encode(array('status' => 'ERROR', 'message' => 'Too Many Results')));
$this->response->send();
$matches = '';
} else {
$people = $this->CoPerson->filterPicker($co, $coPersonIds, $mode);
$pickerEmailType = $this->Co->CoSetting->getPersonPickerEmailType($co);
$pickerIdentifierType = $this->Co->CoSetting->getPersonPickerIdentifierType($co);
$pickerDisplayTypes = $this->Co->CoSetting->getPersonPickerDisplayTypes($co);

foreach($people as $p) {
$label = generateCn($p['Name'][0]);
$idArr = $p['Identifier'];
$emailArr = $p['EmailAddress'];
$email = '';
$emailLabel = '';
$id = '';
$idLabel = '';

// Iterate over the email array
if(!empty($emailArr) && !empty($pickerEmailType)) {
if(!empty($pickerDisplayTypes)) {
$emailLabel = _txt('fd.extended_type.generic.label', array(_txt('fd.email_address.mail'), $pickerEmailType));
}
else {
$emailLabel = _txt('fd.email_address.mail') . ': ';
}
foreach($emailArr as $e) {
if($e['type'] == $pickerEmailType) {
$email = $e['mail'];
break;
}
}
}

// Set the identifier for display (and limit it to 30 characters max)
if(!empty($idArr[0]['identifier']) && !empty($pickerIdentifierType)) {
if(!empty($pickerDisplayTypes)) {
$idLabel = _txt('fd.extended_type.generic.label', array(_txt('fd.identifier.identifier'), $pickerIdentifierType));
}
else {
$idLabel = _txt('fd.identifier.identifier') . ': ';
}
foreach($idArr as $i) {
if($i['type'] == $pickerIdentifierType) {
//$id = mb_strimwidth($i['identifier'], 0, 30, '...');
$id = $i['identifier'];
break;
}
}
}

$matches[] = array(
'value' => $p['CoPerson']['id'],
'label' => $label,
'email' => $email,
'emailLabel' => $emailLabel,
'identifier' => $id,
'identifierLabel' => $idLabel
);
}
}
$matches = $this->GrouperGroup->findForPicker($this->cur_co['Co']['id'],
$this->request->query['mode'],
$this->request->query['term'] ?? null);

$this->set(compact('matches'));
$this->set('_serialize', 'matches');
Expand Down
20 changes: 12 additions & 8 deletions Lib/GrouperApiAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,21 +372,22 @@ public function getGrouperGroupInfo(array $queryData)
* Note: Params added at end make sure that the groups returned can only be viewed by the member logged into
* Grouper Lite
*
* @param string $userId
* @param string $actorUserId
* @param string $userId
*
* @return array Membership records that User is a member of in Grouper
*
* @throws GrouperLiteWidgetException
*/
public function getGrouperMemberOfGroups(string $userId)
public function getUserGrouperGroupMemberships(string $actorUserId, string $userId): array
{
if(empty($userId)) {
return [];
}

$actionEndpoint = "/subjects/{$userId}/groups?"
. 'wsLiteObjectType=WsRestGetGroupsLiteRequest'
. "&actAsSubjectId={$userId}";
. "&actAsSubjectId={$actorUserId}";
try {
$results = $this->http->sendRequest('GET', $actionEndpoint);
} catch (Exception $e) {
Expand All @@ -400,20 +401,23 @@ public function getGrouperMemberOfGroups(string $userId)
/**
* Get members associated to a specific Grouper Group
*
* @param array $queryData Array of conditions for querying
* @param string $actorUserId
* @param string $groupName
*
* @return array Listing of Members belonging to Grouper Group
* @throws GrouperLiteWidgetException|JsonException
* @throws GrouperLiteWidgetException
* @throws JsonException
*/
public function getMembersInGroup(array $queryData) {
public function getGrouperGroupMembers(string $actorUserId, string $groupName): array
{
//Build request logic
$usersToShow = [
'WsRestGetMembersRequest' => [
'actAsSubjectLookup' => [
'subjectId' => $queryData['userId']
'subjectId' => $actorUserId
],
'wsGroupLookups' => [
['groupName' => $queryData['groupName']]
['groupName' => $groupName]
],
'subjectAttributeNames' => ['name']
]
Expand Down
2 changes: 2 additions & 0 deletions Lib/GrouperHTTPWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ public function setUser(string $user): void
* @throws GrouperLiteWidgetException If issue with Grouper WS connection
*
* TODO: Handle unauthorized request-responses
* TODO: Handle SUBJECT_NOT_FOUND,
* TODO: Handle GROUP_NOT_FOUND,
*/
public function sendRequest(string $method, string $endPoint, string $body = ''): array {
$uri = "{$this->_serviceUrl}{$endPoint}";
Expand Down
4 changes: 4 additions & 0 deletions Lib/enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ class GrouperSpecialGroups {

/** Group whose members cannot see Grouper button, even if admin */
const GROUPER_VISIBLE_GROUP = 'app:comanage:LiteUI:grouperVisible';

/* Stem for email groups, only email groups using this stem are viewable */
const GROUPER_EMAIL_STEM = 'app:sympa';
}

class GrouperResultCodesEnum {
const IS_MEMBER = 'IS_MEMBER';
const SUCCESS = 'SUCCESS';
const GROUP_NOT_FOUND = 'GROUP_NOT_FOUND';
const SUBJECT_NOT_FOUND = 'SUBJECT_NOT_FOUND';
const NO_SUCH_OBJECT = 'NO_SUCH_OBJECT';
const SUCCESS_NO_INFO = 'SUCCESS_NO_INFO';
const EXECUTE_FAILED = 'EXECUTE_FAILED';
Expand Down
Loading

0 comments on commit a953284

Please sign in to comment.