appConfig->getAdvanced()) { $this->logger->debug("extraPermissions isn't init"); return new DataResponse([], Http::STATUS_BAD_REQUEST); } $user = $this->userSession->getUser(); $userId = $user->getUID(); $sourceFile = $this->getFile($fileId, $userId); $fileStorage = $sourceFile->getStorage(); if ($fileStorage->instanceOfStorage(SharedStorage::class)) { return new DataResponse([]); } $shares = $this->getSharesByOwner($userId, $sourceFile); $extras = $this->extraPermissions->getExtras($shares); return new DataResponse($extras); } /** * Set shares for file * * @param integer $extraId - extra permission identifier * @param string $shareId - share identifier * @param integer $fileId - file identifier * @param integer $permissions - permissions bitmask * * @return DataResponse */ #[NoAdminRequired] #[NoCSRFRequired] public function setShares(int $extraId, string $shareId, int $fileId, int $permissions): DataResponse { if (!$this->appConfig->getAdvanced()) { $this->logger->debug("extraPermissions isn't init"); return new DataResponse([], Http::STATUS_BAD_REQUEST); } $user = $this->userSession->getUser(); $userId = $user->getUID(); $sourceFile = $this->getFile($fileId, $userId); $fileStorage = $sourceFile->getStorage(); if ($fileStorage->instanceOfStorage(SharedStorage::class)) { return new DataResponse([], Http::STATUS_BAD_REQUEST); } $shares = $this->getSharesByOwner($userId, $sourceFile); if (!array_filter($shares, fn (IShare $share) => $share->getId() === $shareId)) { $this->logger->error("setShares: share $shareId not found for file $fileId."); return new DataResponse([], Http::STATUS_BAD_REQUEST); } if (!$this->extraPermissions->setExtra($shareId, $permissions, $extraId)) { $this->logger->error("setShares: couldn't set extra permissions for: " . $shareId); return new DataResponse([], Http::STATUS_BAD_REQUEST); } $extra = $this->extraPermissions->getExtra($shareId); return new DataResponse($extra); } /** * Get source file * * @param integer $fileId - file identifier * @param string $userId - user identifier */ private function getFile(int $fileId, string $userId): ?File { try { $folder = $this->root->getUserFolder($userId); $files = $folder->getById($fileId); } catch (\Exception $e) { $this->logger->error("getFile: $fileId", ["exception" => $e]); return null; } if (empty($files)) { $this->logger->error("getFile: file not found: " . $fileId); return null; } return $files[0]; } /** * Get all shares for a file created by the given user * @param string $userId * @param File $file * @return IShare[] */ private function getSharesByOwner(string $userId, File $file): array { $shareTypes = [ IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_ROOM, IShare::TYPE_LINK, IShare::TYPE_CIRCLE, ]; $shares = []; foreach ($shareTypes as $type) { $shares = [...$shares, ...$this->shareManager->getSharesBy($userId, $type, $file)]; } return $shares; } }