Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ protected function getUserData(string $userId, bool $includeScopes = false): ?ar

// Get groups data
$userAccount = $this->accountManager->getAccount($targetUserObject);
$groups = $this->groupManager->getUserGroups($targetUserObject);
$gids = [];
foreach ($groups as $group) {
$gids[] = $group->getGID();
}
$gids = $this->groupManager->getUserGroupIds($targetUserObject);

if ($isAdmin || $isDelegatedAdmin) {
try {
Expand Down Expand Up @@ -279,10 +275,13 @@ protected function findGroupsWithDisplayname(array $userDetails): array {
$groupIds = array_unique($groupIds);
sort($groupIds);

return array_map(function ($groupId) {
$displayname = $this->groupDisplayNameCache->getDisplayName($groupId) ?? $groupId;
return ['id' => $groupId, 'displayname' => $displayname];
}, $groupIds);
$info = [];
foreach ($this->groupDisplayNameCache->getDisplayNames($groupIds) as $groupId => $displayName) {
if ($displayName !== null) {
$info[] = ['id' => $groupId, 'displayname' => $displayName];
}
}
return $info;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions apps/provisioning_api/lib/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,8 +1080,7 @@ public function editUserMultiField(
}

if ($groups !== null) {
$currentGroups = $this->groupManager->getUserGroups($targetUser);
$currentGroupIds = array_map(fn (IGroup $g) => $g->getGID(), $currentGroups);
$currentGroupIds = $this->groupManager->getUserGroupIds($targetUser);
foreach (array_diff($currentGroupIds, $groups) as $gid) {
$this->groupManager->get($gid)?->removeUser($targetUser);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,8 @@ public function testGetGroupUsersDetails(): void {
->with($gid)
->willReturn($group);
$this->groupManager->expects($this->any())
->method('getUserGroups')
->willReturn([$group]);
->method('getUserGroupIds')
->willReturn(['ncg1']);

/** @var MockObject */
$this->subAdminManager->expects($this->any())
Expand Down Expand Up @@ -538,8 +538,8 @@ public function testGetGroupUsersDetailsEncoded(): void {
->with($gid)
->willReturn($group);
$this->groupManager->expects($this->any())
->method('getUserGroups')
->willReturn([$group]);
->method('getUserGroupIds')
->willReturn(['Department A/B C/D']);

/** @var MockObject */
$this->subAdminManager->expects($this->any())
Expand Down
15 changes: 3 additions & 12 deletions apps/provisioning_api/tests/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1196,8 +1196,8 @@ public function testGetUserDataAsAdmin(): void {
->willReturn(true);
$this->groupManager
->expects($this->any())
->method('getUserGroups')
->willReturn([$group0, $group1, $group2]);
->method('getUserGroupIds')
->willReturn(['group0', 'group1', 'group2']);
$this->groupManager
->expects($this->once())
->method('getSubAdmin')
Expand All @@ -1206,15 +1206,6 @@ public function testGetUserDataAsAdmin(): void {
->expects($this->once())
->method('getSubAdminsGroups')
->willReturn([$group3]);
$group0->expects($this->exactly(1))
->method('getGID')
->willReturn('group0');
$group1->expects($this->exactly(1))
->method('getGID')
->willReturn('group1');
$group2->expects($this->exactly(1))
->method('getGID')
->willReturn('group2');
$group3->expects($this->once())
->method('getGID')
->willReturn('group3');
Expand Down Expand Up @@ -2759,7 +2750,7 @@ public function testUpdateUserGroupDiff(): void {
$newGroup = $this->createMock(IGroup::class);
$newGroup->method('getGID')->willReturn('newgroup');

$this->groupManager->method('getUserGroups')->willReturn([$oldGroup]);
$this->groupManager->method('getUserGroupIds')->willReturn(['oldgroup']);
$this->groupManager->method('groupExists')->willReturn(true);
$this->groupManager->method('get')->willReturnMap([
['newgroup', $newGroup],
Expand Down
38 changes: 38 additions & 0 deletions lib/private/Group/DisplayNameCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,44 @@ public function getDisplayName(string $groupId): ?string {
return $displayName;
}

/**
* @param list<string> $groupIds
* @return array<string, ?string>
*/
public function getDisplayNames(array $groupIds): array {
$result = [];
$missing = [];
foreach ($groupIds as $groupId) {
if (isset($this->cache[$groupId])) {
$result[$groupId] = $this->cache[$groupId];
} else {
$displayName = $this->memCache->get($groupId);
if ($displayName) {
$this->cache[$groupId] = $displayName;
$result[$groupId] = $displayName;
} else {
$missing[] = $groupId;
}
}
}

/** @var Manager $groupManager */
$groupManager = $this->groupManager;
$groups = $groupManager->getGroupsObjects($missing);
$stillMissingGroups = array_diff($missing, array_keys($groups));
foreach ($groups as $groupId => $group) {
$this->cache[$groupId] = $group->getDisplayName();
$this->memCache->set($groupId, $displayName, 60 * 10); // 10 minutes
$result[$groupId] = $group->getDisplayName();
}

foreach ($stillMissingGroups as $groupId) {
$result[$groupId] = null;
}

return $result;
}

public function clear(): void {
$this->cache = new CappedMemoryCache();
$this->memCache->clear();
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Group/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ protected function getGroupObject($gid, $displayName = null) {
* @param array<string, string> $displayNames Array containing already know display name for a groupId
* @return array<string, IGroup>
*/
protected function getGroupsObjects(array $gids, array $displayNames = []): array {
public function getGroupsObjects(array $gids, array $displayNames = []): array {
$backends = [];
$groups = [];
foreach ($gids as $gid) {
Expand Down
Loading