diff --git a/lib/Model/Mount.php b/lib/Model/Mount.php index 75d9ce447..25c1c35d2 100644 --- a/lib/Model/Mount.php +++ b/lib/Model/Mount.php @@ -19,6 +19,7 @@ use OCA\Files_Sharing\External\Manager as ExternalShareManager; use OCP\Federation\ICloudIdManager; use OCP\Http\Client\IClientService; +use OCP\IUser; /** * Class Mount @@ -43,6 +44,7 @@ class Mount extends ManagedModel implements IDeserializable, IQueryRow, JsonSeri private ICloudIdManager $cloudIdManager; private IClientService $httpClientService; private ExternalShareManager $externalShareManager; + private IUser $recipient; private string $remote = ''; private int $remoteShareId = 0; @@ -316,6 +318,16 @@ public function getExternalShareManager(): ExternalShareManager { return $this->externalShareManager; } + public function setRecipient(IUser $recipient): self { + $this->recipient = $recipient; + + return $this; + } + + public function getRecipient(): IUser { + return $this->recipient; + } + public function setRemote(string $remote): void { $this->remote = $remote; } @@ -346,6 +358,7 @@ public function toMount(): array { 'mountpoint' => $this->getMountPoint(false), 'HttpClientService' => $this->getHttpClientService(), 'manager' => $this->getExternalShareManager(), + 'recipient' => $this->getRecipient(), 'cloudId' => $this->getCloudIdManager()->getCloudId( $member->getUserId(), $member->getRemoteInstance()->getRoot() diff --git a/lib/MountManager/CircleMountProvider.php b/lib/MountManager/CircleMountProvider.php index 3964129b3..28e52d1eb 100644 --- a/lib/MountManager/CircleMountProvider.php +++ b/lib/MountManager/CircleMountProvider.php @@ -72,7 +72,7 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader): array { foreach ($items as $item) { try { $this->fixDuplicateFile($user->getUID(), $item); - $mounts[] = $this->generateCircleMount($item, $loader); + $mounts[] = $this->generateCircleMount($item, $loader, $user); } catch (\Exception $e) { $this->logger->warning('issue with teams\' mounts', ['exception' => $e]); } @@ -86,7 +86,7 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader): array { * @throws InitiatorNotFoundException * @throws MountPointConstructionException */ - public function generateCircleMount(Mount $mount, IStorageFactory $storageFactory): CircleMount { + public function generateCircleMount(Mount $mount, IStorageFactory $storageFactory, IUser $user): CircleMount { $initiator = $mount->getInitiator(); // TODO: right now, limited to Local Nextcloud User @@ -97,7 +97,8 @@ public function generateCircleMount(Mount $mount, IStorageFactory $storageFactor $mount->setCloudIdManager($this->cloudIdManager) ->setHttpClientService($this->clientService) - ->setExternalShareManager($this->externalShareManager); + ->setExternalShareManager($this->externalShareManager) + ->setRecipient($user); return new CircleMount( $mount, diff --git a/tests/unit/lib/MountManager/CircleMountProviderTest.php b/tests/unit/lib/MountManager/CircleMountProviderTest.php new file mode 100644 index 000000000..1045a712b --- /dev/null +++ b/tests/unit/lib/MountManager/CircleMountProviderTest.php @@ -0,0 +1,146 @@ +rootFolder = $this->createMock(IRootFolder::class); + $this->mountRequest = $this->createMock(MountRequest::class); + $this->federatedUserService = $this->createMock(FederatedUserService::class); + $configService = $this->createMock(ConfigService::class); + $configService->method('isLocalInstance') + ->willReturn(true); + $this->user = $this->createMock(IUser::class); + $this->user->method('getUID') + ->willReturn('alice'); + + $this->provider = new CircleMountProvider( + $this->createMock(IClientService::class), + $this->rootFolder, + Server::get(ICloudIdManager::class), + $this->mountRequest, + $this->createMock(MountPointRequest::class), + $this->federatedUserService, + $configService, + $this->createMock(LoggerInterface::class), + $this->createMock(ExternalShareManager::class), + ); + } + + private function createTeamMount(): Mount { + $localUser = new FederatedUser(); + $localUser->setUserId('alice') + ->setUserType(Member::TYPE_USER) + ->setInstance(''); + $initiator = new Member(); + $initiator->setUserId('alice') + ->setInheritedBy($localUser); + + $remoteInstance = new RemoteInstance(); + $remoteInstance->setRoot('https://remote.example'); + $owner = new Member(); + $owner->setUserId('bob') + ->setRemoteInstance($remoteInstance); + + $mount = new Mount(); + $mount->setMountId('mount-id') + ->setOriginalMountPoint('/team-share') + ->setToken('token') + ->setOwner($owner) + ->setInitiator($initiator); + + return $mount; + } + + private function mockFreeMountPoint(): void { + $cache = $this->createMock(ICache::class); + $cache->method('inCache') + ->willReturn(false); + $storage = $this->createMock(IStorage::class); + $storage->method('getCache') + ->willReturn($cache); + $homeMount = $this->createMock(IMountPoint::class); + $homeMount->method('getStorage') + ->willReturn($storage); + $userFolder = $this->createMock(Folder::class); + $userFolder->method('getMountPoint') + ->willReturn($homeMount); + $this->rootFolder->method('getUserFolder') + ->with('alice') + ->willReturn($userFolder); + + $federatedUser = new FederatedUser(); + $federatedUser->setSingleId('alice-single-id'); + $this->federatedUserService->method('getLocalFederatedUser') + ->with('alice') + ->willReturn($federatedUser); + } + + public function testGenerateCircleMountCreatesExternalStorageForUser(): void { + $discoveryService = $this->createMock(IOCMDiscoveryService::class); + $discoveryService->method('discover') + ->willThrowException(new OCMProviderException()); + $this->overwriteService(IOCMDiscoveryService::class, $discoveryService); + $mount = $this->createTeamMount(); + + $circleMount = $this->provider->generateCircleMount($mount, new StorageFactory(), $this->user); + + $this->assertSame($this->user, $mount->toMount()['recipient']); + $this->assertTrue($circleMount->getStorage()->instanceOfStorage(ExternalStorage::class)); + } + + public function testGetMountsForUserPassesUserAsRecipient(): void { + $this->mockFreeMountPoint(); + $mount = $this->createTeamMount(); + $this->mountRequest->method('getForUser') + ->willReturn([$mount]); + + $mounts = $this->provider->getMountsForUser($this->user, new StorageFactory()); + + $this->assertCount(1, $mounts); + $this->assertSame($this->user, $mount->toMount()['recipient']); + } +}