root = $root; $this->logger = $logger; $this->userSession = $userSession; $this->userManager = $userManager; $this->shareManager = $shareManager; $this->appConfig = $appConfig; if ($this->appConfig->getAdvanced() && \OC::$server->getAppManager()->isInstalled("files_sharing")) { $this->extraPermissions = new ExtraPermissions($AppName, $logger, $shareManager, $appConfig); } } /** * Get shares for file * * @param integer $fileId - file identifier * * @return DataResponse */ #[NoAdminRequired] #[NoCSRFRequired] public function getShares($fileId) { if ($this->extraPermissions === null) { $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("\OCA\Files_Sharing\SharedStorage")) { return new DataResponse([]); } $sharesUser = $this->shareManager->getSharesBy($userId, IShare::TYPE_USER, $sourceFile, null, true); $sharesGroup = $this->shareManager->getSharesBy($userId, IShare::TYPE_GROUP, $sourceFile, null, true); $sharesRoom = $this->shareManager->getSharesBy($userId, IShare::TYPE_ROOM, $sourceFile, null, true); $sharesLink = $this->shareManager->getSharesBy($userId, IShare::TYPE_LINK, $sourceFile, null, true); $shares = array_merge($sharesUser, $sharesGroup, $sharesRoom, $sharesLink); $extras = $this->extraPermissions->getExtras($shares, $sourceFile); return new DataResponse($extras); } /** * Set shares for file * * @param integer $extraId - extra permission identifier * @param integer $shareId - share identifier * @param integer $fileId - file identifier * @param integer $permissions - permissions value * * @return DataResponse */ #[NoAdminRequired] #[NoCSRFRequired] public function setShares($extraId, $shareId, $fileId, $permissions) { if ($this->extraPermissions === null) { $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("\OCA\Files_Sharing\SharedStorage")) { 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 * * @return File */ private function getFile($fileId, $userId) { 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]; } }