Skip to content

Commit 2bc949d

Browse files
authored
Merge pull request #33581 from nextcloud/share-lazy-user
use LazyUser in DefaultShareProvider
2 parents 9872467 + de6bb33 commit 2bc949d

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

lib/private/Share20/DefaultShareProvider.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use OC\Share20\Exception\BackendError;
1212
use OC\Share20\Exception\InvalidShare;
1313
use OC\Share20\Exception\ProviderException;
14+
use OC\User\LazyUser;
1415
use OCP\AppFramework\Utility\ITimeFactory;
1516
use OCP\DB\QueryBuilder\IQueryBuilder;
1617
use OCP\Defaults;
@@ -909,8 +910,8 @@ public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
909910
}
910911
$cursor->closeCursor();
911912
} elseif ($shareType === IShare::TYPE_GROUP) {
912-
$user = $this->userManager->get($userId);
913-
$allGroups = ($user instanceof IUser) ? $this->groupManager->getUserGroupIds($user) : [];
913+
$user = new LazyUser($userId, $this->userManager);
914+
$allGroups = $this->groupManager->getUserGroupIds($user);
914915

915916
/** @var Share[] $shares2 */
916917
$shares2 = [];
@@ -1045,9 +1046,9 @@ private function createShare($data) {
10451046

10461047
if ($share->getShareType() === IShare::TYPE_USER) {
10471048
$share->setSharedWith($data['share_with']);
1048-
$user = $this->userManager->get($data['share_with']);
1049-
if ($user !== null) {
1050-
$share->setSharedWithDisplayName($user->getDisplayName());
1049+
$displayName = $this->userManager->getDisplayName($data['share_with']);
1050+
if ($displayName !== null) {
1051+
$share->setSharedWithDisplayName($displayName);
10511052
}
10521053
} elseif ($share->getShareType() === IShare::TYPE_GROUP) {
10531054
$share->setSharedWith($data['share_with']);

tests/lib/Share20/DefaultShareProviderTest.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,31 @@ class DefaultShareProviderTest extends \Test\TestCase {
3838
/** @var IDBConnection */
3939
protected $dbConn;
4040

41-
/** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */
41+
/** @var IUserManager | MockObject */
4242
protected $userManager;
4343

44-
/** @var IGroupManager | \PHPUnit\Framework\MockObject\MockObject */
44+
/** @var IGroupManager | MockObject */
4545
protected $groupManager;
4646

47-
/** @var IRootFolder | \PHPUnit\Framework\MockObject\MockObject */
47+
/** @var IRootFolder | MockObject */
4848
protected $rootFolder;
4949

5050
/** @var DefaultShareProvider */
5151
protected $provider;
5252

53-
/** @var \PHPUnit\Framework\MockObject\MockObject|IMailer */
53+
/** @var MockObject|IMailer */
5454
protected $mailer;
5555

5656
/** @var IFactory|MockObject */
5757
protected $l10nFactory;
5858

59-
/** @var \PHPUnit\Framework\MockObject\MockObject|IL10N */
59+
/** @var MockObject|IL10N */
6060
protected $l10n;
6161

62-
/** @var \PHPUnit\Framework\MockObject\MockObject|Defaults */
62+
/** @var MockObject|Defaults */
6363
protected $defaults;
6464

65-
/** @var \PHPUnit\Framework\MockObject\MockObject|IURLGenerator */
65+
/** @var MockObject|IURLGenerator */
6666
protected $urlGenerator;
6767

6868
/** @var ITimeFactory|MockObject */
@@ -1035,7 +1035,9 @@ public function testGetSharedWithGroup($storageStringId, $fileName1, $fileName2)
10351035
['shareOwner', $owner],
10361036
['sharedBy', $initiator],
10371037
]);
1038-
$this->groupManager->method('getUserGroupIds')->with($user)->willReturn($groups);
1038+
$this->groupManager
1039+
->method('getUserGroupIds')
1040+
->willReturnCallback(fn (IUser $user) => ($user->getUID() === 'sharedWith' ? $groups : []));
10391041

10401042
$file = $this->createMock(File::class);
10411043
$this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf();
@@ -1123,7 +1125,9 @@ public function testGetSharedWithGroupUserModified($storageStringId, $fileName1,
11231125
['shareOwner', $owner],
11241126
['sharedBy', $initiator],
11251127
]);
1126-
$this->groupManager->method('getUserGroupIds')->with($user)->willReturn($groups);
1128+
$this->groupManager
1129+
->method('getUserGroupIds')
1130+
->willReturnCallback(fn (IUser $user) => ($user->getUID() === 'user' ? $groups : []));
11271131

11281132
$file = $this->createMock(File::class);
11291133
$this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf();
@@ -1206,7 +1210,9 @@ public function testGetSharedWithGroupWithNode($storageStringId, $fileName1, $fi
12061210
['user1', $user1],
12071211
]);
12081212

1209-
$this->groupManager->method('getUserGroupIds')->with($user0)->willReturn(['group0']);
1213+
$this->groupManager
1214+
->method('getUserGroupIds')
1215+
->willReturnCallback(fn (IUser $user) => ($user->getUID() === 'user0' ? ['group0'] : []));
12101216

12111217
$node = $this->createMock(Folder::class);
12121218
$node->method('getId')->willReturn($fileId2);
@@ -1283,7 +1289,9 @@ public function testGetSharedWithWithDeletedFile($shareType, $trashed) {
12831289
['shareOwner', $owner],
12841290
['sharedBy', $initiator],
12851291
]);
1286-
$this->groupManager->method('getUserGroupIds')->with($user)->willReturn($groups);
1292+
$this->groupManager
1293+
->method('getUserGroupIds')
1294+
->willReturnCallback(fn (IUser $user) => ($user->getUID() === 'sharedWith' ? $groups : []));
12871295

12881296
$share = $this->provider->getSharedWith('sharedWith', $shareType, null, 1, 0);
12891297
$this->assertCount(0, $share);

0 commit comments

Comments
 (0)