Skip to content

Commit

Permalink
Merge pull request #33581 from nextcloud/share-lazy-user
Browse files Browse the repository at this point in the history
use LazyUser in DefaultShareProvider
  • Loading branch information
blizzz authored Jun 17, 2024
2 parents 9872467 + de6bb33 commit 2bc949d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
11 changes: 6 additions & 5 deletions lib/private/Share20/DefaultShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OC\Share20\Exception\BackendError;
use OC\Share20\Exception\InvalidShare;
use OC\Share20\Exception\ProviderException;
use OC\User\LazyUser;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Defaults;
Expand Down Expand Up @@ -909,8 +910,8 @@ public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
}
$cursor->closeCursor();
} elseif ($shareType === IShare::TYPE_GROUP) {
$user = $this->userManager->get($userId);
$allGroups = ($user instanceof IUser) ? $this->groupManager->getUserGroupIds($user) : [];
$user = new LazyUser($userId, $this->userManager);
$allGroups = $this->groupManager->getUserGroupIds($user);

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

if ($share->getShareType() === IShare::TYPE_USER) {
$share->setSharedWith($data['share_with']);
$user = $this->userManager->get($data['share_with']);
if ($user !== null) {
$share->setSharedWithDisplayName($user->getDisplayName());
$displayName = $this->userManager->getDisplayName($data['share_with']);
if ($displayName !== null) {
$share->setSharedWithDisplayName($displayName);
}
} elseif ($share->getShareType() === IShare::TYPE_GROUP) {
$share->setSharedWith($data['share_with']);
Expand Down
30 changes: 19 additions & 11 deletions tests/lib/Share20/DefaultShareProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,31 @@ class DefaultShareProviderTest extends \Test\TestCase {
/** @var IDBConnection */
protected $dbConn;

/** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */
/** @var IUserManager | MockObject */
protected $userManager;

/** @var IGroupManager | \PHPUnit\Framework\MockObject\MockObject */
/** @var IGroupManager | MockObject */
protected $groupManager;

/** @var IRootFolder | \PHPUnit\Framework\MockObject\MockObject */
/** @var IRootFolder | MockObject */
protected $rootFolder;

/** @var DefaultShareProvider */
protected $provider;

/** @var \PHPUnit\Framework\MockObject\MockObject|IMailer */
/** @var MockObject|IMailer */
protected $mailer;

/** @var IFactory|MockObject */
protected $l10nFactory;

/** @var \PHPUnit\Framework\MockObject\MockObject|IL10N */
/** @var MockObject|IL10N */
protected $l10n;

/** @var \PHPUnit\Framework\MockObject\MockObject|Defaults */
/** @var MockObject|Defaults */
protected $defaults;

/** @var \PHPUnit\Framework\MockObject\MockObject|IURLGenerator */
/** @var MockObject|IURLGenerator */
protected $urlGenerator;

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

$file = $this->createMock(File::class);
$this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf();
Expand Down Expand Up @@ -1123,7 +1125,9 @@ public function testGetSharedWithGroupUserModified($storageStringId, $fileName1,
['shareOwner', $owner],
['sharedBy', $initiator],
]);
$this->groupManager->method('getUserGroupIds')->with($user)->willReturn($groups);
$this->groupManager
->method('getUserGroupIds')
->willReturnCallback(fn (IUser $user) => ($user->getUID() === 'user' ? $groups : []));

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

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

$node = $this->createMock(Folder::class);
$node->method('getId')->willReturn($fileId2);
Expand Down Expand Up @@ -1283,7 +1289,9 @@ public function testGetSharedWithWithDeletedFile($shareType, $trashed) {
['shareOwner', $owner],
['sharedBy', $initiator],
]);
$this->groupManager->method('getUserGroupIds')->with($user)->willReturn($groups);
$this->groupManager
->method('getUserGroupIds')
->willReturnCallback(fn (IUser $user) => ($user->getUID() === 'sharedWith' ? $groups : []));

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

0 comments on commit 2bc949d

Please sign in to comment.