diff options
Diffstat (limited to 'lib/Controller/OCSController.php')
-rw-r--r-- | lib/Controller/OCSController.php | 122 |
1 files changed, 84 insertions, 38 deletions
diff --git a/lib/Controller/OCSController.php b/lib/Controller/OCSController.php index ea79e0c5..23188a3c 100644 --- a/lib/Controller/OCSController.php +++ b/lib/Controller/OCSController.php @@ -26,6 +26,7 @@ namespace OCA\Richdocuments\Controller; use OCA\Richdocuments\Db\DirectMapper; use OCA\Richdocuments\Service\FederationService; use OCA\Richdocuments\TemplateManager; +use OCA\Richdocuments\TokenManager; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCS\OCSBadRequestException; @@ -38,6 +39,7 @@ use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\IRequest; use OCP\IURLGenerator; +use OCP\Share\IManager; class OCSController extends \OCP\AppFramework\OCSController { @@ -56,20 +58,15 @@ class OCSController extends \OCP\AppFramework\OCSController { /** @var TemplateManager */ private $manager; + /** @var TokenManager */ + private $tokenManager; + + /** @var IManager */ + private $shareManager; + /** @var FederationService */ private $federationService; - /** - * OCS controller - * - * @param string $appName - * @param IRequest $request - * @param IRootFolder $rootFolder - * @param string $userId - * @param DirectMapper $directMapper - * @param IURLGenerator $urlGenerator - * @param TemplateManager $manager - */ public function __construct(string $appName, IRequest $request, IRootFolder $rootFolder, @@ -77,6 +74,8 @@ class OCSController extends \OCP\AppFramework\OCSController { DirectMapper $directMapper, IURLGenerator $urlGenerator, TemplateManager $manager, + TokenManager $tokenManager, + IManager $shareManager, FederationService $federationService ) { parent::__construct($appName, $request); @@ -86,19 +85,21 @@ class OCSController extends \OCP\AppFramework\OCSController { $this->directMapper = $directMapper; $this->urlGenerator = $urlGenerator; $this->manager = $manager; + $this->tokenManager = $tokenManager; + $this->shareManager = $shareManager; $this->federationService = $federationService; } /** * @NoAdminRequired * - * Init an editing session + * Init a direct editing session * * @param int $fileId * @return DataResponse * @throws OCSNotFoundException|OCSBadRequestException */ - public function create($fileId) { + public function createDirect($fileId) { try { $userFolder = $this->rootFolder->getUserFolder($this->userId); $nodes = $userFolder->getById($fileId); @@ -125,41 +126,86 @@ class OCSController extends \OCP\AppFramework\OCSController { } /** + * Generate a direct editing link for a file in a public share to open with the current user + * + * If + * * @NoAdminRequired - * @param $shareToken - * @param $path - * @param $password - * @param $guestName - * @param null $remoteUserHost - * @param null $remoteUserToken - * @return DataResponse - * @throws NotFoundException * @throws OCSForbiddenException - * @throws \OCP\Share\Exceptions\ShareNotFound */ - public function createPublic(string $shareToken, string $path = '', string $password = null, string $guestName = null, $remoteUserHost = null, $remoteUserToken = null) { - if ($remoteUserHost && $remoteUserToken) { - // may be optional depending on the requirements - // fetch user details from remote host + public function createPublic( + string $shareTokenSourceInstance = null, + string $shareToken, + string $path = '', + string $password = null + ): DataResponse { + if ($shareTokenSourceInstance) { + $remoteCollabora = $this->federationService->getRemoteCollaboraURL($shareTokenSourceInstance); + if ($remoteCollabora === '') { + throw new OCSNotFoundException('Failed to connect to remote collabora instance.'); + } + + $wopi = $this->tokenManager->newInitiatorToken($shareTokenSourceInstance, null, $shareToken, true, $this->userId); + + $client = \OC::$server->getHTTPClientService()->newClient(); + $response = $client->post(rtrim($shareTokenSourceInstance, '/') . '/ocs/v2.php/apps/richdocuments/api/v1/direct/share/initiator?format=json', [ + 'body' => [ + 'initiatorServer' => \OC::$server->getURLGenerator()->getAbsoluteURL(''), + 'initiatorToken' => $wopi->getToken(), + 'shareToken' => $shareToken, + 'path' => $path, + 'password' => $password + ], + 'timeout' => 5 + ]); + $url = \json_decode($response->getBody(), true)['ocs']['data']['url']; + + return new DataResponse([ + 'url' => $url, + ]); } - $this->shareManager = \OC::$server->getShareManager(); + $share = $this->shareManager->getShareByToken($shareToken); - if ($share->getPassword()) { - if (!$this->shareManager->checkPassword($share, $password)) { - throw new OCSForbiddenException(); - } + if ($share->getPassword() && !$this->shareManager->checkPassword($share, $password)) { + throw new OCSForbiddenException(); + } + + $node = $share->getNode(); + if ($node instanceof Folder) { + $node = $node->get($path); + } + $direct = $this->directMapper->newDirect($this->userId, $node->getId(), 0, $shareToken); + + return new DataResponse([ + 'url' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.directView.show', [ + 'token' => $direct->getToken() + ]) + ]); + } + + /** + * @PublicPage + * @NoCSRFRequired + * @throws OCSForbiddenException + */ + public function createPublicFromInitiator( + string $initiatorServer, + string $initiatorToken, + string $shareToken, + string $path = '', + string $password = null + ): DataResponse { + $share = $this->shareManager->getShareByToken($shareToken); + if ($share->getPassword() && !$this->shareManager->checkPassword($share, $password)) { + throw new OCSForbiddenException(); } + $node = $share->getNode(); if ($node instanceof Folder) { - try { - $node = $node->get($path); - } catch (NotFoundException $e) { - throw new OCSForbiddenException(); - } + $node = $node->get($path); } - // create new direct token and link with either $guestName or remote user info - $direct = $this->directMapper->newDirect( - ); + $direct = $this->directMapper->newDirect(null, $node->getId(), null, $shareToken, $initiatorServer, $initiatorToken); return new DataResponse([ 'url' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.directView.show', [ |