Skip to content

Commit c3cc765

Browse files
Merge pull request #61934 from nextcloud/backport/61916/stable32
[stable32] feat: Fetch groups in batch in getUserGroups
2 parents 5741102 + a04f97f commit c3cc765

2 files changed

Lines changed: 18 additions & 24 deletions

File tree

lib/private/Group/Manager.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,8 @@ public function getUserGroups(?IUser $user = null) {
301301
* @return \OC\Group\Group[]
302302
*/
303303
public function getUserIdGroups(string $uid): array {
304-
$groups = [];
305-
306-
foreach ($this->getUserIdGroupIds($uid) as $groupId) {
307-
$aGroup = $this->get($groupId);
308-
if ($aGroup instanceof IGroup) {
309-
$groups[$groupId] = $aGroup;
310-
} else {
311-
$this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']);
312-
}
313-
}
314-
315-
return $groups;
304+
$groupIds = $this->getUserIdGroupIds($uid);
305+
return $this->getGroupsObjects($groupIds);
316306
}
317307

318308
/**

tests/lib/Group/ManagerTest.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,9 @@ public function testGetUserGroups(): void {
427427
->with('user1')
428428
->willReturn(['group1']);
429429
$backend->expects($this->any())
430-
->method('groupExists')
430+
->method('getGroupDetails')
431431
->with('group1')
432-
->willReturn(true);
432+
->willReturn(['displayName' => 'group1']);
433433

434434
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
435435
$manager->addBackend($backend);
@@ -475,9 +475,9 @@ public function testGetUserGroupsWithDeletedGroup(): void {
475475
->with('user1')
476476
->willReturn(['group1']);
477477
$backend->expects($this->any())
478-
->method('groupExists')
479-
->with('group1')
480-
->willReturn(false);
478+
->method('getGroupsDetails')
479+
->with(['group1'])
480+
->willReturn(['group1' => []]);
481481

482482
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
483483
$manager->addBackend($backend);
@@ -559,8 +559,8 @@ public function testGetUserGroupsMultipleBackends(): void {
559559
->with('user1')
560560
->willReturn(['group1']);
561561
$backend1->expects($this->any())
562-
->method('groupExists')
563-
->willReturn(true);
562+
->method('getGroupDetails')
563+
->willReturnCallback(fn ($gid) => $gid === 'group1' ? ['displayName' => 'group1'] : []);
564564

565565
/**
566566
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2
@@ -570,9 +570,9 @@ public function testGetUserGroupsMultipleBackends(): void {
570570
->method('getUserGroups')
571571
->with('user1')
572572
->willReturn(['group1', 'group2']);
573-
$backend1->expects($this->any())
574-
->method('groupExists')
575-
->willReturn(true);
573+
$backend2->expects($this->any())
574+
->method('getGroupDetails')
575+
->willReturnCallback(fn ($gid) => ['displayName' => $gid]);
576576

577577
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
578578
$manager->addBackend($backend1);
@@ -869,6 +869,10 @@ public function testGetUserGroupsWithAddUser(): void {
869869
->method('groupExists')
870870
->with('group1')
871871
->willReturn(true);
872+
$backend->expects($this->any())
873+
->method('getGroupDetails')
874+
->with('group1')
875+
->willReturn(['displayName' => 'group1']);
872876

873877
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress);
874878
$manager->addBackend($backend);
@@ -903,9 +907,9 @@ public function testGetUserGroupsWithRemoveUser(): void {
903907
return $expectedGroups;
904908
});
905909
$backend->expects($this->any())
906-
->method('groupExists')
910+
->method('getGroupDetails')
907911
->with('group1')
908-
->willReturn(true);
912+
->willReturn(['displayName' => 'group1']);
909913
$backend->expects($this->once())
910914
->method('inGroup')
911915
->willReturn(true);

0 commit comments

Comments
 (0)