Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit 18b6aed

Browse files
committed
feat: Adapter - getUsersGroupsOnSp, getGroupsWhereMemberIsActive
1 parent efc0f8f commit 18b6aed

File tree

3 files changed

+111
-11
lines changed

3 files changed

+111
-11
lines changed

lib/Adapter.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ abstract public function getVoById($id);
8484
*/
8585
abstract public function getMemberGroups($user, $vo);
8686

87+
/**
88+
* @param User $user perun user
89+
* @param Vo $vo vo we are working with
90+
*
91+
* @return Group[] groups from vo where user is valid.
92+
*/
93+
abstract public function getGroupsWhereMemberIsActive($user, $vo);
94+
8795
/**
8896
* @param string $spEntityId entity id of the sp
8997
*
@@ -170,7 +178,15 @@ abstract public function getFacilityByClientId($clientId, $clientIdAttr);
170178
*
171179
* @return Group[] from vo which are assigned to all facilities with spEntityId for this userId
172180
*/
173-
abstract public function getUsersGroupsOnFacility($spEntityId, $userId);
181+
abstract public function getUsersGroupsOnSp($spEntityId, $userId);
182+
183+
/**
184+
* @param Facility $facility entity id of the sp
185+
* @param int $userId
186+
*
187+
* @return Group[] from vo which are assigned to all facilities with spEntityId for this userId
188+
*/
189+
abstract public function getUsersGroupsOnFacility($facility, $userId);
174190

175191
/**
176192
* @param <String, String> map $attribute

lib/AdapterLdap.php

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,43 @@ public function getMemberGroups($user, $vo)
152152
return $groups;
153153
}
154154

155+
public function getGroupsWhereMemberIsActive($user, $vo)
156+
{
157+
$userId = $user->getId();
158+
$userWithMembership = $this->connector->searchForEntity(
159+
'perunUserId=' . $userId . ',ou=People,' . $this->ldapBase,
160+
'(objectClass=perunUser)',
161+
['perunUserId', 'memberOf']
162+
);
163+
164+
$groups = [];
165+
foreach ($userWithMembership['memberOf'] as $groupDn) {
166+
$voId = explode('=', explode(',', $groupDn)[1], 2)[1];
167+
if ($voId !== $vo->getId()) {
168+
continue;
169+
}
170+
171+
$group = $this->connector->searchForEntity(
172+
$groupDn,
173+
'(objectClass=perunGroup)',
174+
['perunGroupId', 'cn', 'perunUniqueGroupName', 'perunVoId', 'uuid', 'description']
175+
);
176+
array_push(
177+
$groups,
178+
new Group(
179+
$group['perunGroupId'][0],
180+
$group['perunVoId'][0],
181+
$group['uuid'][0],
182+
$group['cn'][0],
183+
$group['perunUniqueGroupName'][0],
184+
$group['description'][0] ?? ''
185+
)
186+
);
187+
}
188+
189+
return $groups;
190+
}
191+
155192
public function getSpGroups(string $spEntityId): array
156193
{
157194
$facility = $this->getFacilityByEntityId($spEntityId);
@@ -424,14 +461,17 @@ public function setUserExtSourceAttributes($userExtSourceId, $attributes)
424461
$this->fallbackAdapter->setUserExtSourceAttributes($userExtSourceId, $attributes);
425462
}
426463

427-
public function getUsersGroupsOnFacility($spEntityId, $userId)
464+
public function getUsersGroupsOnSp($spEntityId, $userId)
428465
{
429466
$facility = $this->getFacilityByEntityId($spEntityId);
467+
return self::getUsersGroupsOnFacility($facility, $userId);
468+
}
430469

470+
public function getUsersGroupsOnFacility($facility, $userId)
471+
{
431472
if (null === $facility) {
432473
return [];
433474
}
434-
435475
$id = $facility->getId();
436476

437477
$resources = $this->connector->searchForEntities(
@@ -442,7 +482,7 @@ public function getUsersGroupsOnFacility($spEntityId, $userId)
442482
Logger::debug('Resources - ' . json_encode($resources));
443483

444484
if (null === $resources) {
445-
throw new Exception('Service with spEntityId: ' . $spEntityId . ' hasn\'t assigned any resource.');
485+
throw new Exception('Service with ID: ' . $id . ' hasn\'t assigned any resource.');
446486
}
447487
$resourcesString = '(|';
448488
foreach ($resources as $resource) {
@@ -470,10 +510,7 @@ public function getUsersGroupsOnFacility($spEntityId, $userId)
470510
)
471511
);
472512
}
473-
$resultGroups = $this->removeDuplicateEntities($resultGroups);
474-
Logger::debug('Groups - ' . json_encode($resultGroups));
475-
476-
return $resultGroups;
513+
return $this->removeDuplicateEntities($resultGroups);
477514
}
478515

479516
public function getMemberStatusByUserAndVo($user, $vo)

lib/AdapterRpc.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,48 @@ public function getMemberGroups($user, $vo)
154154
return $convertedGroups;
155155
}
156156

157+
public function getGroupsWhereMemberIsActive($user, $vo)
158+
{
159+
try {
160+
$member = $this->connector->get('membersManager', 'getMemberByUser', [
161+
'vo' => $vo->getId(),
162+
'user' => $user->getId(),
163+
]);
164+
165+
$memberGroups = $this->connector->get('groupsManager', 'getGroupsWhereMemberIsActive', [
166+
'member' => $member['id'],
167+
]);
168+
} catch (PerunException $e) {
169+
return [];
170+
}
171+
172+
$convertedGroups = [];
173+
foreach ($memberGroups as $group) {
174+
try {
175+
$attr = $this->connector->get('attributesManager', 'getAttribute', [
176+
'group' => $group['id'],
177+
'attributeName' => 'urn:perun:group:attribute-def:virt:voShortName',
178+
]);
179+
$uniqueName = $attr['value'] . ':' . $group['name'];
180+
array_push(
181+
$convertedGroups,
182+
new Group(
183+
$group['id'],
184+
$group['voId'],
185+
$group['uuid'],
186+
$group['name'],
187+
$uniqueName,
188+
$group['description']
189+
)
190+
);
191+
} catch (PerunException $e) {
192+
continue;
193+
}
194+
}
195+
196+
return $convertedGroups;
197+
}
198+
157199
public function getSpGroups(string $spEntityId): array
158200
{
159201
$facility = $this->getFacilityByEntityId($spEntityId);
@@ -338,13 +380,16 @@ public function getFacilityAttribute($facility, $attrName)
338380
return $perunAttr['value'];
339381
}
340382

341-
public function getUsersGroupsOnFacility($spEntityId, $userId)
383+
public function getUsersGroupsOnSp($spEntityId, $userId)
342384
{
343385
$facility = $this->getFacilityByEntityId($spEntityId);
344-
$groups = [];
386+
return self::getUsersGroupsOnFacility($facility, $userId);
387+
}
345388

389+
public function getUsersGroupsOnFacility($facility, $userId)
390+
{
346391
if (null === $facility) {
347-
return $groups;
392+
return [];
348393
}
349394

350395
$usersGroupsOnFacility = $this->connector->get(
@@ -357,6 +402,8 @@ public function getUsersGroupsOnFacility($spEntityId, $userId)
357402
]
358403
);
359404

405+
$groups = [];
406+
360407
foreach ($usersGroupsOnFacility as $usersGroupOnFacility) {
361408
if (isset($usersGroupOnFacility['attributes'][0]['friendlyName']) &&
362409
'voShortName' === $usersGroupOnFacility['attributes'][0]['friendlyName']) {

0 commit comments

Comments
 (0)