diff options
author | Daniel Calviño Sánchez <danxuliu@gmail.com> | 2018-10-05 21:41:47 +0300 |
---|---|---|
committer | Daniel Calviño Sánchez <danxuliu@gmail.com> | 2018-11-29 20:35:21 +0300 |
commit | 19e166191ba58a6d0919f81ef491eecc867458f8 (patch) | |
tree | e1594a95cf042260dc76cba24d940e8606874a01 | |
parent | be97a530e1f59ba371006c62c71d04b7cedd0b47 (diff) |
Add end point to get the token of a room associated with a file
Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
-rw-r--r-- | appinfo/routes.php | 13 | ||||
-rw-r--r-- | lib/Controller/FilesController.php | 109 | ||||
-rw-r--r-- | lib/Files/Util.php | 140 |
3 files changed, 262 insertions, 0 deletions
diff --git a/appinfo/routes.php b/appinfo/routes.php index 884e86b9f..b5804d711 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -320,6 +320,19 @@ return [ ], /** + * Files + */ + [ + 'name' => 'Files#getRoom', + 'url' => '/api/{apiVersion}/file/{fileId}', + 'verb' => 'GET', + 'requirements' => [ + 'apiVersion' => 'v1', + 'fileId' => '.+' + ], + ], + + /** * Guest */ [ diff --git a/lib/Controller/FilesController.php b/lib/Controller/FilesController.php new file mode 100644 index 000000000..a706b8404 --- /dev/null +++ b/lib/Controller/FilesController.php @@ -0,0 +1,109 @@ +<?php +declare(strict_types=1); + +/** + * + * @copyright Copyright (c) 2018, Daniel Calviño Sánchez (danxuliu@gmail.com) + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Spreed\Controller; + +use OCA\Spreed\Exceptions\RoomNotFoundException; +use OCA\Spreed\Files\Util; +use OCA\Spreed\Manager; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\OCS\OCSNotFoundException; +use OCP\AppFramework\OCSController; +use OCP\IL10N; +use OCP\IRequest; + +class FilesController extends OCSController { + + /** @var string */ + private $currentUser; + /** @var Manager */ + private $manager; + /** @var Util */ + private $util; + /** @var IL10N */ + private $l; + + /** + * @param string $appName + * @param IRequest $request + * @param string $userId + * @param Manager $manager + * @param Util $util + * @param IL10N $l10n + */ + public function __construct( + string $appName, + IRequest $request, + string $userId, + Manager $manager, + Util $util, + IL10N $l10n + ) { + parent::__construct($appName, $request); + $this->currentUser = $userId; + $this->manager = $manager; + $this->util = $util; + $this->l = $l10n; + } + + /** + * @NoAdminRequired + * + * Returns the token of the room associated to the given file id. + * + * If there is no room associated to the given file id a new room is + * created; the new room is a public room associated with a "file" object + * with the given file id. Unlike normal rooms in which the owner is the + * user that created the room these are special rooms without owner or any + * other persistent participant. + * + * In any case, to create or even get the token of the room, the file must + * be shared and the user must have direct access to that file; an error + * is returned otherwise. A user has direct access to a file if she has + * access to it through a user, group, circle or room share (but not through + * a link share, for example), or if she is the owner of such a file. + * + * @param string $fileId + * @return DataResponse the status code is "200 OK" if a room is returned, + * or "404 Not found" if the given file id was invalid. + */ + public function getRoom(string $fileId): DataResponse { + $share = $this->util->getAnyDirectShareOfFileAccessibleByUser($fileId, $this->currentUser); + if (!$share) { + throw new OCSNotFoundException($this->l->t('File is not shared, or shared but not with the user')); + } + + try { + $room = $this->manager->getRoomByObject('file', $fileId); + } catch (RoomNotFoundException $e) { + $node = $share->getNode(); + $room = $this->manager->createPublicRoom($node->getName(), 'file', $fileId); + } + + return new DataResponse([ + 'token' => $room->getToken() + ]); + } + +} diff --git a/lib/Files/Util.php b/lib/Files/Util.php new file mode 100644 index 000000000..5e2f2b90e --- /dev/null +++ b/lib/Files/Util.php @@ -0,0 +1,140 @@ +<?php +declare(strict_types=1); + +/** + * + * @copyright Copyright (c) 2018, Daniel Calviño Sánchez (danxuliu@gmail.com) + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Spreed\Files; + +use OCP\Files\IRootFolder; +use OCP\Files\Node; +use OCP\Share\IManager as IShareManager; +use OCP\Share\IShare; + +class Util { + + /** @var IRootFolder */ + private $rootFolder; + /** @var IShareManager */ + private $shareManager; + + /** + * @param IRootFolder $rootFolder + * @param IShareManager $shareManager + */ + public function __construct( + IRootFolder $rootFolder, + IShareManager $shareManager + ) { + $this->rootFolder = $rootFolder; + $this->shareManager = $shareManager; + } + + /** + * Returns any share of the file that the user has direct access to. + * + * A user has direct access to a share and, thus, to a file, if she received + * the file through a user, group, circle or room share (but not through a + * public link, for example), or if she is the owner of such a share. + * + * @param string $fileId + * @param string $userId + * @return IShare|null + */ + public function getAnyDirectShareOfFileAccessibleByUser(string $fileId, string $userId) { + $userFolder = $this->rootFolder->getUserFolder($userId); + $fileById = $userFolder->getById($fileId); + if (empty($fileById)) { + return null; + } + + foreach ($fileById as $node) { + $share = $this->getAnyDirectShareOfNodeAccessibleByUser($node, $userId); + if ($share) { + return $share; + } + } + + return null; + } + + /** + * Returns any share of the node that the user has direct access to. + * + * @param Node $node + * @param string $userId + * @return IShare|null + */ + private function getAnyDirectShareOfNodeAccessibleByUser(Node $node, string $userId) { + $reshares = false; + $limit = 1; + + $shares = $this->shareManager->getSharesBy($userId, \OCP\Share::SHARE_TYPE_USER, $node, $reshares, $limit); + if (\count($shares) > 0) { + return $shares[0]; + } + + $shares = $this->shareManager->getSharesBy($userId, \OCP\Share::SHARE_TYPE_GROUP, $node, $reshares, $limit); + if (\count($shares) > 0) { + return $shares[0]; + } + + $shares = $this->shareManager->getSharesBy($userId, \OCP\Share::SHARE_TYPE_CIRCLE, $node, $reshares, $limit); + if (\count($shares) > 0) { + return $shares[0]; + } + + $shares = $this->shareManager->getSharesBy($userId, \OCP\Share::SHARE_TYPE_ROOM, $node, $reshares, $limit); + if (\count($shares) > 0) { + return $shares[0]; + } + + // If the node is not shared then there is no need for further checks. + // Note that "isShared()" returns false for owned shares, so the check + // can not be moved above. + if (!$node->isShared()) { + return null; + } + + $shares = $this->shareManager->getSharedWith($userId, \OCP\Share::SHARE_TYPE_USER, $node, $limit); + if (\count($shares) > 0) { + return $shares[0]; + } + + $shares = $this->shareManager->getSharedWith($userId, \OCP\Share::SHARE_TYPE_GROUP, $node, $limit); + if (\count($shares) > 0) { + return $shares[0]; + } + + $shares = $this->shareManager->getSharedWith($userId, \OCP\Share::SHARE_TYPE_CIRCLE, $node, $limit); + if (\count($shares) > 0) { + return $shares[0]; + } + + $shares = $this->shareManager->getSharedWith($userId, \OCP\Share::SHARE_TYPE_ROOM, $node, $limit); + if (\count($shares) > 0) { + return $shares[0]; + } + + return null; + } + +} |