@@ -33,57 +33,57 @@ |
||
| 33 | 33 | |
| 34 | 34 | class DirectEditingService { |
| 35 | 35 | |
| 36 | - /** @var IManager */ |
|
| 37 | - private $directEditingManager; |
|
| 38 | - /** @var IEventDispatcher */ |
|
| 39 | - private $eventDispatcher; |
|
| 36 | + /** @var IManager */ |
|
| 37 | + private $directEditingManager; |
|
| 38 | + /** @var IEventDispatcher */ |
|
| 39 | + private $eventDispatcher; |
|
| 40 | 40 | |
| 41 | - public function __construct(IEventDispatcher $eventDispatcher, IManager $directEditingManager) { |
|
| 42 | - $this->directEditingManager = $directEditingManager; |
|
| 43 | - $this->eventDispatcher = $eventDispatcher; |
|
| 44 | - } |
|
| 41 | + public function __construct(IEventDispatcher $eventDispatcher, IManager $directEditingManager) { |
|
| 42 | + $this->directEditingManager = $directEditingManager; |
|
| 43 | + $this->eventDispatcher = $eventDispatcher; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - public function getDirectEditingETag(): string { |
|
| 47 | - return \md5(\json_encode($this->getDirectEditingCapabilitites())); |
|
| 48 | - } |
|
| 46 | + public function getDirectEditingETag(): string { |
|
| 47 | + return \md5(\json_encode($this->getDirectEditingCapabilitites())); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - public function getDirectEditingCapabilitites(): array { |
|
| 51 | - $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager)); |
|
| 50 | + public function getDirectEditingCapabilitites(): array { |
|
| 51 | + $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager)); |
|
| 52 | 52 | |
| 53 | - $capabilities = [ |
|
| 54 | - 'editors' => [], |
|
| 55 | - 'creators' => [] |
|
| 56 | - ]; |
|
| 53 | + $capabilities = [ |
|
| 54 | + 'editors' => [], |
|
| 55 | + 'creators' => [] |
|
| 56 | + ]; |
|
| 57 | 57 | |
| 58 | - if (!$this->directEditingManager->isEnabled()) { |
|
| 59 | - return $capabilities; |
|
| 60 | - } |
|
| 58 | + if (!$this->directEditingManager->isEnabled()) { |
|
| 59 | + return $capabilities; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * @var string $id |
|
| 64 | - * @var IEditor $editor |
|
| 65 | - */ |
|
| 66 | - foreach ($this->directEditingManager->getEditors() as $id => $editor) { |
|
| 67 | - $capabilities['editors'][$id] = [ |
|
| 68 | - 'id' => $editor->getId(), |
|
| 69 | - 'name' => $editor->getName(), |
|
| 70 | - 'mimetypes' => $editor->getMimetypes(), |
|
| 71 | - 'optionalMimetypes' => $editor->getMimetypesOptional(), |
|
| 72 | - 'secure' => $editor->isSecure(), |
|
| 73 | - ]; |
|
| 74 | - /** @var ACreateEmpty|ACreateFromTemplate $creator */ |
|
| 75 | - foreach ($editor->getCreators() as $creator) { |
|
| 76 | - $id = $creator->getId(); |
|
| 77 | - $capabilities['creators'][$id] = [ |
|
| 78 | - 'id' => $id, |
|
| 79 | - 'editor' => $editor->getId(), |
|
| 80 | - 'name' => $creator->getName(), |
|
| 81 | - 'extension' => $creator->getExtension(), |
|
| 82 | - 'templates' => $creator instanceof ACreateFromTemplate, |
|
| 83 | - 'mimetype' => $creator->getMimetype() |
|
| 84 | - ]; |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - return $capabilities; |
|
| 88 | - } |
|
| 62 | + /** |
|
| 63 | + * @var string $id |
|
| 64 | + * @var IEditor $editor |
|
| 65 | + */ |
|
| 66 | + foreach ($this->directEditingManager->getEditors() as $id => $editor) { |
|
| 67 | + $capabilities['editors'][$id] = [ |
|
| 68 | + 'id' => $editor->getId(), |
|
| 69 | + 'name' => $editor->getName(), |
|
| 70 | + 'mimetypes' => $editor->getMimetypes(), |
|
| 71 | + 'optionalMimetypes' => $editor->getMimetypesOptional(), |
|
| 72 | + 'secure' => $editor->isSecure(), |
|
| 73 | + ]; |
|
| 74 | + /** @var ACreateEmpty|ACreateFromTemplate $creator */ |
|
| 75 | + foreach ($editor->getCreators() as $creator) { |
|
| 76 | + $id = $creator->getId(); |
|
| 77 | + $capabilities['creators'][$id] = [ |
|
| 78 | + 'id' => $id, |
|
| 79 | + 'editor' => $editor->getId(), |
|
| 80 | + 'name' => $creator->getName(), |
|
| 81 | + 'extension' => $creator->getExtension(), |
|
| 82 | + 'templates' => $creator instanceof ACreateFromTemplate, |
|
| 83 | + 'mimetype' => $creator->getMimetype() |
|
| 84 | + ]; |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + return $capabilities; |
|
| 88 | + } |
|
| 89 | 89 | } |
@@ -37,97 +37,97 @@ |
||
| 37 | 37 | |
| 38 | 38 | class DirectEditingController extends OCSController { |
| 39 | 39 | |
| 40 | - /** @var IEventDispatcher */ |
|
| 41 | - private $eventDispatcher; |
|
| 42 | - |
|
| 43 | - /** @var IManager */ |
|
| 44 | - private $directEditingManager; |
|
| 45 | - |
|
| 46 | - /** @var IURLGenerator */ |
|
| 47 | - private $urlGenerator; |
|
| 48 | - |
|
| 49 | - /** @var ILogger */ |
|
| 50 | - private $logger; |
|
| 51 | - |
|
| 52 | - /** @var DirectEditingService */ |
|
| 53 | - private $directEditingService; |
|
| 54 | - |
|
| 55 | - public function __construct($appName, IRequest $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge, |
|
| 56 | - IEventDispatcher $eventDispatcher, IURLGenerator $urlGenerator, IManager $manager, DirectEditingService $directEditingService, ILogger $logger) { |
|
| 57 | - parent::__construct($appName, $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge); |
|
| 58 | - |
|
| 59 | - $this->eventDispatcher = $eventDispatcher; |
|
| 60 | - $this->directEditingManager = $manager; |
|
| 61 | - $this->directEditingService = $directEditingService; |
|
| 62 | - $this->logger = $logger; |
|
| 63 | - $this->urlGenerator = $urlGenerator; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @NoAdminRequired |
|
| 68 | - */ |
|
| 69 | - public function info(): DataResponse { |
|
| 70 | - $response = new DataResponse($this->directEditingService->getDirectEditingCapabilitites()); |
|
| 71 | - $response->setETag($this->directEditingService->getDirectEditingETag()); |
|
| 72 | - return $response; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @NoAdminRequired |
|
| 77 | - */ |
|
| 78 | - public function create(string $path, string $editorId, string $creatorId, string $templateId = null): DataResponse { |
|
| 79 | - if (!$this->directEditingManager->isEnabled()) { |
|
| 80 | - return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 81 | - } |
|
| 82 | - $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager)); |
|
| 83 | - |
|
| 84 | - try { |
|
| 85 | - $token = $this->directEditingManager->create($path, $editorId, $creatorId, $templateId); |
|
| 86 | - return new DataResponse([ |
|
| 87 | - 'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token]) |
|
| 88 | - ]); |
|
| 89 | - } catch (Exception $e) { |
|
| 90 | - $this->logger->logException($e, ['message' => 'Exception when creating a new file through direct editing']); |
|
| 91 | - return new DataResponse(['message' => 'Failed to create file: ' . $e->getMessage()], Http::STATUS_FORBIDDEN); |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @NoAdminRequired |
|
| 97 | - */ |
|
| 98 | - public function open(string $path, string $editorId = null): DataResponse { |
|
| 99 | - if (!$this->directEditingManager->isEnabled()) { |
|
| 100 | - return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 101 | - } |
|
| 102 | - $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager)); |
|
| 103 | - |
|
| 104 | - try { |
|
| 105 | - $token = $this->directEditingManager->open($path, $editorId); |
|
| 106 | - return new DataResponse([ |
|
| 107 | - 'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token]) |
|
| 108 | - ]); |
|
| 109 | - } catch (Exception $e) { |
|
| 110 | - $this->logger->logException($e, ['message' => 'Exception when opening a file through direct editing']); |
|
| 111 | - return new DataResponse(['message' => 'Failed to open file: ' . $e->getMessage()], Http::STATUS_FORBIDDEN); |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @NoAdminRequired |
|
| 119 | - */ |
|
| 120 | - public function templates(string $editorId, string $creatorId): DataResponse { |
|
| 121 | - if (!$this->directEditingManager->isEnabled()) { |
|
| 122 | - return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 123 | - } |
|
| 124 | - $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager)); |
|
| 125 | - |
|
| 126 | - try { |
|
| 127 | - return new DataResponse($this->directEditingManager->getTemplates($editorId, $creatorId)); |
|
| 128 | - } catch (Exception $e) { |
|
| 129 | - $this->logger->logException($e); |
|
| 130 | - return new DataResponse(['message' => 'Failed to obtain template list: ' . $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 131 | - } |
|
| 132 | - } |
|
| 40 | + /** @var IEventDispatcher */ |
|
| 41 | + private $eventDispatcher; |
|
| 42 | + |
|
| 43 | + /** @var IManager */ |
|
| 44 | + private $directEditingManager; |
|
| 45 | + |
|
| 46 | + /** @var IURLGenerator */ |
|
| 47 | + private $urlGenerator; |
|
| 48 | + |
|
| 49 | + /** @var ILogger */ |
|
| 50 | + private $logger; |
|
| 51 | + |
|
| 52 | + /** @var DirectEditingService */ |
|
| 53 | + private $directEditingService; |
|
| 54 | + |
|
| 55 | + public function __construct($appName, IRequest $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge, |
|
| 56 | + IEventDispatcher $eventDispatcher, IURLGenerator $urlGenerator, IManager $manager, DirectEditingService $directEditingService, ILogger $logger) { |
|
| 57 | + parent::__construct($appName, $request, $corsMethods, $corsAllowedHeaders, $corsMaxAge); |
|
| 58 | + |
|
| 59 | + $this->eventDispatcher = $eventDispatcher; |
|
| 60 | + $this->directEditingManager = $manager; |
|
| 61 | + $this->directEditingService = $directEditingService; |
|
| 62 | + $this->logger = $logger; |
|
| 63 | + $this->urlGenerator = $urlGenerator; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @NoAdminRequired |
|
| 68 | + */ |
|
| 69 | + public function info(): DataResponse { |
|
| 70 | + $response = new DataResponse($this->directEditingService->getDirectEditingCapabilitites()); |
|
| 71 | + $response->setETag($this->directEditingService->getDirectEditingETag()); |
|
| 72 | + return $response; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @NoAdminRequired |
|
| 77 | + */ |
|
| 78 | + public function create(string $path, string $editorId, string $creatorId, string $templateId = null): DataResponse { |
|
| 79 | + if (!$this->directEditingManager->isEnabled()) { |
|
| 80 | + return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 81 | + } |
|
| 82 | + $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager)); |
|
| 83 | + |
|
| 84 | + try { |
|
| 85 | + $token = $this->directEditingManager->create($path, $editorId, $creatorId, $templateId); |
|
| 86 | + return new DataResponse([ |
|
| 87 | + 'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token]) |
|
| 88 | + ]); |
|
| 89 | + } catch (Exception $e) { |
|
| 90 | + $this->logger->logException($e, ['message' => 'Exception when creating a new file through direct editing']); |
|
| 91 | + return new DataResponse(['message' => 'Failed to create file: ' . $e->getMessage()], Http::STATUS_FORBIDDEN); |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @NoAdminRequired |
|
| 97 | + */ |
|
| 98 | + public function open(string $path, string $editorId = null): DataResponse { |
|
| 99 | + if (!$this->directEditingManager->isEnabled()) { |
|
| 100 | + return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 101 | + } |
|
| 102 | + $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager)); |
|
| 103 | + |
|
| 104 | + try { |
|
| 105 | + $token = $this->directEditingManager->open($path, $editorId); |
|
| 106 | + return new DataResponse([ |
|
| 107 | + 'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token]) |
|
| 108 | + ]); |
|
| 109 | + } catch (Exception $e) { |
|
| 110 | + $this->logger->logException($e, ['message' => 'Exception when opening a file through direct editing']); |
|
| 111 | + return new DataResponse(['message' => 'Failed to open file: ' . $e->getMessage()], Http::STATUS_FORBIDDEN); |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @NoAdminRequired |
|
| 119 | + */ |
|
| 120 | + public function templates(string $editorId, string $creatorId): DataResponse { |
|
| 121 | + if (!$this->directEditingManager->isEnabled()) { |
|
| 122 | + return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 123 | + } |
|
| 124 | + $this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager)); |
|
| 125 | + |
|
| 126 | + try { |
|
| 127 | + return new DataResponse($this->directEditingManager->getTemplates($editorId, $creatorId)); |
|
| 128 | + } catch (Exception $e) { |
|
| 129 | + $this->logger->logException($e); |
|
| 130 | + return new DataResponse(['message' => 'Failed to obtain template list: ' . $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | 133 | } |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | ]); |
| 89 | 89 | } catch (Exception $e) { |
| 90 | 90 | $this->logger->logException($e, ['message' => 'Exception when creating a new file through direct editing']); |
| 91 | - return new DataResponse(['message' => 'Failed to create file: ' . $e->getMessage()], Http::STATUS_FORBIDDEN); |
|
| 91 | + return new DataResponse(['message' => 'Failed to create file: '.$e->getMessage()], Http::STATUS_FORBIDDEN); |
|
| 92 | 92 | } |
| 93 | 93 | } |
| 94 | 94 | |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | ]); |
| 109 | 109 | } catch (Exception $e) { |
| 110 | 110 | $this->logger->logException($e, ['message' => 'Exception when opening a file through direct editing']); |
| 111 | - return new DataResponse(['message' => 'Failed to open file: ' . $e->getMessage()], Http::STATUS_FORBIDDEN); |
|
| 111 | + return new DataResponse(['message' => 'Failed to open file: '.$e->getMessage()], Http::STATUS_FORBIDDEN); |
|
| 112 | 112 | } |
| 113 | 113 | } |
| 114 | 114 | |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | return new DataResponse($this->directEditingManager->getTemplates($editorId, $creatorId)); |
| 128 | 128 | } catch (Exception $e) { |
| 129 | 129 | $this->logger->logException($e); |
| 130 | - return new DataResponse(['message' => 'Failed to obtain template list: ' . $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 130 | + return new DataResponse(['message' => 'Failed to obtain template list: '.$e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 131 | 131 | } |
| 132 | 132 | } |
| 133 | 133 | } |
@@ -51,251 +51,251 @@ |
||
| 51 | 51 | use function in_array; |
| 52 | 52 | |
| 53 | 53 | class Manager implements IManager { |
| 54 | - private const TOKEN_CLEANUP_TIME = 12 * 60 * 60 ; |
|
| 54 | + private const TOKEN_CLEANUP_TIME = 12 * 60 * 60 ; |
|
| 55 | 55 | |
| 56 | - public const TABLE_TOKENS = 'direct_edit'; |
|
| 56 | + public const TABLE_TOKENS = 'direct_edit'; |
|
| 57 | 57 | |
| 58 | - /** @var IEditor[] */ |
|
| 59 | - private $editors = []; |
|
| 60 | - /** @var IDBConnection */ |
|
| 61 | - private $connection; |
|
| 62 | - /** @var ISecureRandom */ |
|
| 63 | - private $random; |
|
| 64 | - /** @var string|null */ |
|
| 65 | - private $userId; |
|
| 66 | - /** @var IRootFolder */ |
|
| 67 | - private $rootFolder; |
|
| 68 | - /** @var IL10N */ |
|
| 69 | - private $l10n; |
|
| 70 | - /** @var EncryptionManager */ |
|
| 71 | - private $encryptionManager; |
|
| 58 | + /** @var IEditor[] */ |
|
| 59 | + private $editors = []; |
|
| 60 | + /** @var IDBConnection */ |
|
| 61 | + private $connection; |
|
| 62 | + /** @var ISecureRandom */ |
|
| 63 | + private $random; |
|
| 64 | + /** @var string|null */ |
|
| 65 | + private $userId; |
|
| 66 | + /** @var IRootFolder */ |
|
| 67 | + private $rootFolder; |
|
| 68 | + /** @var IL10N */ |
|
| 69 | + private $l10n; |
|
| 70 | + /** @var EncryptionManager */ |
|
| 71 | + private $encryptionManager; |
|
| 72 | 72 | |
| 73 | - public function __construct( |
|
| 74 | - ISecureRandom $random, |
|
| 75 | - IDBConnection $connection, |
|
| 76 | - IUserSession $userSession, |
|
| 77 | - IRootFolder $rootFolder, |
|
| 78 | - IFactory $l10nFactory, |
|
| 79 | - EncryptionManager $encryptionManager |
|
| 80 | - ) { |
|
| 81 | - $this->random = $random; |
|
| 82 | - $this->connection = $connection; |
|
| 83 | - $this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null; |
|
| 84 | - $this->rootFolder = $rootFolder; |
|
| 85 | - $this->l10n = $l10nFactory->get('core'); |
|
| 86 | - $this->encryptionManager = $encryptionManager; |
|
| 87 | - } |
|
| 73 | + public function __construct( |
|
| 74 | + ISecureRandom $random, |
|
| 75 | + IDBConnection $connection, |
|
| 76 | + IUserSession $userSession, |
|
| 77 | + IRootFolder $rootFolder, |
|
| 78 | + IFactory $l10nFactory, |
|
| 79 | + EncryptionManager $encryptionManager |
|
| 80 | + ) { |
|
| 81 | + $this->random = $random; |
|
| 82 | + $this->connection = $connection; |
|
| 83 | + $this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null; |
|
| 84 | + $this->rootFolder = $rootFolder; |
|
| 85 | + $this->l10n = $l10nFactory->get('core'); |
|
| 86 | + $this->encryptionManager = $encryptionManager; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - public function registerDirectEditor(IEditor $directEditor): void { |
|
| 90 | - $this->editors[$directEditor->getId()] = $directEditor; |
|
| 91 | - } |
|
| 89 | + public function registerDirectEditor(IEditor $directEditor): void { |
|
| 90 | + $this->editors[$directEditor->getId()] = $directEditor; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - public function getEditors(): array { |
|
| 94 | - return $this->editors; |
|
| 95 | - } |
|
| 93 | + public function getEditors(): array { |
|
| 94 | + return $this->editors; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - public function getTemplates(string $editor, string $type): array { |
|
| 98 | - if (!array_key_exists($editor, $this->editors)) { |
|
| 99 | - throw new \RuntimeException('No matching editor found'); |
|
| 100 | - } |
|
| 101 | - $templates = []; |
|
| 102 | - foreach ($this->editors[$editor]->getCreators() as $creator) { |
|
| 103 | - if ($creator->getId() === $type) { |
|
| 104 | - $templates = [ |
|
| 105 | - 'empty' => [ |
|
| 106 | - 'id' => 'empty', |
|
| 107 | - 'title' => $this->l10n->t('Empty file'), |
|
| 108 | - 'preview' => null |
|
| 109 | - ] |
|
| 110 | - ]; |
|
| 97 | + public function getTemplates(string $editor, string $type): array { |
|
| 98 | + if (!array_key_exists($editor, $this->editors)) { |
|
| 99 | + throw new \RuntimeException('No matching editor found'); |
|
| 100 | + } |
|
| 101 | + $templates = []; |
|
| 102 | + foreach ($this->editors[$editor]->getCreators() as $creator) { |
|
| 103 | + if ($creator->getId() === $type) { |
|
| 104 | + $templates = [ |
|
| 105 | + 'empty' => [ |
|
| 106 | + 'id' => 'empty', |
|
| 107 | + 'title' => $this->l10n->t('Empty file'), |
|
| 108 | + 'preview' => null |
|
| 109 | + ] |
|
| 110 | + ]; |
|
| 111 | 111 | |
| 112 | - if ($creator instanceof ACreateFromTemplate) { |
|
| 113 | - $templates = $creator->getTemplates(); |
|
| 114 | - } |
|
| 112 | + if ($creator instanceof ACreateFromTemplate) { |
|
| 113 | + $templates = $creator->getTemplates(); |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - $templates = array_map(function ($template) use ($creator) { |
|
| 117 | - $template['extension'] = $creator->getExtension(); |
|
| 118 | - $template['mimetype'] = $creator->getMimetype(); |
|
| 119 | - return $template; |
|
| 120 | - }, $templates); |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - $return = []; |
|
| 124 | - $return['templates'] = $templates; |
|
| 125 | - return $return; |
|
| 126 | - } |
|
| 116 | + $templates = array_map(function ($template) use ($creator) { |
|
| 117 | + $template['extension'] = $creator->getExtension(); |
|
| 118 | + $template['mimetype'] = $creator->getMimetype(); |
|
| 119 | + return $template; |
|
| 120 | + }, $templates); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + $return = []; |
|
| 124 | + $return['templates'] = $templates; |
|
| 125 | + return $return; |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - public function create(string $path, string $editorId, string $creatorId, $templateId = null): string { |
|
| 129 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 130 | - if ($userFolder->nodeExists($path)) { |
|
| 131 | - throw new \RuntimeException('File already exists'); |
|
| 132 | - } else { |
|
| 133 | - $file = $userFolder->newFile($path); |
|
| 134 | - $editor = $this->getEditor($editorId); |
|
| 135 | - $creators = $editor->getCreators(); |
|
| 136 | - foreach ($creators as $creator) { |
|
| 137 | - if ($creator->getId() === $creatorId) { |
|
| 138 | - $creator->create($file, $creatorId, $templateId); |
|
| 139 | - return $this->createToken($editorId, $file, $path); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - } |
|
| 128 | + public function create(string $path, string $editorId, string $creatorId, $templateId = null): string { |
|
| 129 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 130 | + if ($userFolder->nodeExists($path)) { |
|
| 131 | + throw new \RuntimeException('File already exists'); |
|
| 132 | + } else { |
|
| 133 | + $file = $userFolder->newFile($path); |
|
| 134 | + $editor = $this->getEditor($editorId); |
|
| 135 | + $creators = $editor->getCreators(); |
|
| 136 | + foreach ($creators as $creator) { |
|
| 137 | + if ($creator->getId() === $creatorId) { |
|
| 138 | + $creator->create($file, $creatorId, $templateId); |
|
| 139 | + return $this->createToken($editorId, $file, $path); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - throw new \RuntimeException('No creator found'); |
|
| 145 | - } |
|
| 144 | + throw new \RuntimeException('No creator found'); |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - public function open(string $filePath, string $editorId = null): string { |
|
| 148 | - /** @var File $file */ |
|
| 149 | - $file = $this->rootFolder->getUserFolder($this->userId)->get($filePath); |
|
| 147 | + public function open(string $filePath, string $editorId = null): string { |
|
| 148 | + /** @var File $file */ |
|
| 149 | + $file = $this->rootFolder->getUserFolder($this->userId)->get($filePath); |
|
| 150 | 150 | |
| 151 | - if ($editorId === null) { |
|
| 152 | - $editorId = $this->findEditorForFile($file); |
|
| 153 | - } |
|
| 154 | - if (!array_key_exists($editorId, $this->editors)) { |
|
| 155 | - throw new \RuntimeException("Editor $editorId is unknown"); |
|
| 156 | - } |
|
| 151 | + if ($editorId === null) { |
|
| 152 | + $editorId = $this->findEditorForFile($file); |
|
| 153 | + } |
|
| 154 | + if (!array_key_exists($editorId, $this->editors)) { |
|
| 155 | + throw new \RuntimeException("Editor $editorId is unknown"); |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | - return $this->createToken($editorId, $file, $filePath); |
|
| 159 | - } |
|
| 158 | + return $this->createToken($editorId, $file, $filePath); |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - private function findEditorForFile(File $file) { |
|
| 162 | - foreach ($this->editors as $editor) { |
|
| 163 | - if (in_array($file->getMimeType(), $editor->getMimetypes())) { |
|
| 164 | - return $editor->getId(); |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - throw new \RuntimeException('No default editor found for files mimetype'); |
|
| 168 | - } |
|
| 161 | + private function findEditorForFile(File $file) { |
|
| 162 | + foreach ($this->editors as $editor) { |
|
| 163 | + if (in_array($file->getMimeType(), $editor->getMimetypes())) { |
|
| 164 | + return $editor->getId(); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + throw new \RuntimeException('No default editor found for files mimetype'); |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - public function edit(string $token): Response { |
|
| 171 | - try { |
|
| 172 | - /** @var IEditor $editor */ |
|
| 173 | - $tokenObject = $this->getToken($token); |
|
| 174 | - if ($tokenObject->hasBeenAccessed()) { |
|
| 175 | - throw new \RuntimeException('Token has already been used and can only be used for followup requests'); |
|
| 176 | - } |
|
| 177 | - $editor = $this->getEditor($tokenObject->getEditor()); |
|
| 178 | - $this->accessToken($token); |
|
| 179 | - } catch (Throwable $throwable) { |
|
| 180 | - $this->invalidateToken($token); |
|
| 181 | - return new NotFoundResponse(); |
|
| 182 | - } |
|
| 183 | - return $editor->open($tokenObject); |
|
| 184 | - } |
|
| 170 | + public function edit(string $token): Response { |
|
| 171 | + try { |
|
| 172 | + /** @var IEditor $editor */ |
|
| 173 | + $tokenObject = $this->getToken($token); |
|
| 174 | + if ($tokenObject->hasBeenAccessed()) { |
|
| 175 | + throw new \RuntimeException('Token has already been used and can only be used for followup requests'); |
|
| 176 | + } |
|
| 177 | + $editor = $this->getEditor($tokenObject->getEditor()); |
|
| 178 | + $this->accessToken($token); |
|
| 179 | + } catch (Throwable $throwable) { |
|
| 180 | + $this->invalidateToken($token); |
|
| 181 | + return new NotFoundResponse(); |
|
| 182 | + } |
|
| 183 | + return $editor->open($tokenObject); |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - public function editSecure(File $file, string $editorId): TemplateResponse { |
|
| 187 | - // TODO: Implementation in follow up |
|
| 188 | - } |
|
| 186 | + public function editSecure(File $file, string $editorId): TemplateResponse { |
|
| 187 | + // TODO: Implementation in follow up |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - private function getEditor($editorId): IEditor { |
|
| 191 | - if (!array_key_exists($editorId, $this->editors)) { |
|
| 192 | - throw new \RuntimeException('No editor found'); |
|
| 193 | - } |
|
| 194 | - return $this->editors[$editorId]; |
|
| 195 | - } |
|
| 190 | + private function getEditor($editorId): IEditor { |
|
| 191 | + if (!array_key_exists($editorId, $this->editors)) { |
|
| 192 | + throw new \RuntimeException('No editor found'); |
|
| 193 | + } |
|
| 194 | + return $this->editors[$editorId]; |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - public function getToken(string $token): IToken { |
|
| 198 | - $query = $this->connection->getQueryBuilder(); |
|
| 199 | - $query->select('*')->from(self::TABLE_TOKENS) |
|
| 200 | - ->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR))); |
|
| 201 | - $result = $query->execute(); |
|
| 202 | - if ($tokenRow = $result->fetch(FetchMode::ASSOCIATIVE)) { |
|
| 203 | - return new Token($this, $tokenRow); |
|
| 204 | - } |
|
| 205 | - throw new \RuntimeException('Failed to validate the token'); |
|
| 206 | - } |
|
| 197 | + public function getToken(string $token): IToken { |
|
| 198 | + $query = $this->connection->getQueryBuilder(); |
|
| 199 | + $query->select('*')->from(self::TABLE_TOKENS) |
|
| 200 | + ->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR))); |
|
| 201 | + $result = $query->execute(); |
|
| 202 | + if ($tokenRow = $result->fetch(FetchMode::ASSOCIATIVE)) { |
|
| 203 | + return new Token($this, $tokenRow); |
|
| 204 | + } |
|
| 205 | + throw new \RuntimeException('Failed to validate the token'); |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - public function cleanup(): int { |
|
| 209 | - $query = $this->connection->getQueryBuilder(); |
|
| 210 | - $query->delete(self::TABLE_TOKENS) |
|
| 211 | - ->where($query->expr()->lt('timestamp', $query->createNamedParameter(time() - self::TOKEN_CLEANUP_TIME))); |
|
| 212 | - return $query->execute(); |
|
| 213 | - } |
|
| 208 | + public function cleanup(): int { |
|
| 209 | + $query = $this->connection->getQueryBuilder(); |
|
| 210 | + $query->delete(self::TABLE_TOKENS) |
|
| 211 | + ->where($query->expr()->lt('timestamp', $query->createNamedParameter(time() - self::TOKEN_CLEANUP_TIME))); |
|
| 212 | + return $query->execute(); |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - public function refreshToken(string $token): bool { |
|
| 216 | - $query = $this->connection->getQueryBuilder(); |
|
| 217 | - $query->update(self::TABLE_TOKENS) |
|
| 218 | - ->set('timestamp', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) |
|
| 219 | - ->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR))); |
|
| 220 | - $result = $query->execute(); |
|
| 221 | - return $result !== 0; |
|
| 222 | - } |
|
| 215 | + public function refreshToken(string $token): bool { |
|
| 216 | + $query = $this->connection->getQueryBuilder(); |
|
| 217 | + $query->update(self::TABLE_TOKENS) |
|
| 218 | + ->set('timestamp', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) |
|
| 219 | + ->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR))); |
|
| 220 | + $result = $query->execute(); |
|
| 221 | + return $result !== 0; |
|
| 222 | + } |
|
| 223 | 223 | |
| 224 | 224 | |
| 225 | - public function invalidateToken(string $token): bool { |
|
| 226 | - $query = $this->connection->getQueryBuilder(); |
|
| 227 | - $query->delete(self::TABLE_TOKENS) |
|
| 228 | - ->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR))); |
|
| 229 | - $result = $query->execute(); |
|
| 230 | - return $result !== 0; |
|
| 231 | - } |
|
| 225 | + public function invalidateToken(string $token): bool { |
|
| 226 | + $query = $this->connection->getQueryBuilder(); |
|
| 227 | + $query->delete(self::TABLE_TOKENS) |
|
| 228 | + ->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR))); |
|
| 229 | + $result = $query->execute(); |
|
| 230 | + return $result !== 0; |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | - public function accessToken(string $token): bool { |
|
| 234 | - $query = $this->connection->getQueryBuilder(); |
|
| 235 | - $query->update(self::TABLE_TOKENS) |
|
| 236 | - ->set('accessed', $query->createNamedParameter(true, IQueryBuilder::PARAM_BOOL)) |
|
| 237 | - ->set('timestamp', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) |
|
| 238 | - ->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR))); |
|
| 239 | - $result = $query->execute(); |
|
| 240 | - return $result !== 0; |
|
| 241 | - } |
|
| 233 | + public function accessToken(string $token): bool { |
|
| 234 | + $query = $this->connection->getQueryBuilder(); |
|
| 235 | + $query->update(self::TABLE_TOKENS) |
|
| 236 | + ->set('accessed', $query->createNamedParameter(true, IQueryBuilder::PARAM_BOOL)) |
|
| 237 | + ->set('timestamp', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) |
|
| 238 | + ->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR))); |
|
| 239 | + $result = $query->execute(); |
|
| 240 | + return $result !== 0; |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - public function invokeTokenScope($userId): void { |
|
| 244 | - \OC_User::setIncognitoMode(true); |
|
| 245 | - \OC_User::setUserId($userId); |
|
| 246 | - } |
|
| 243 | + public function invokeTokenScope($userId): void { |
|
| 244 | + \OC_User::setIncognitoMode(true); |
|
| 245 | + \OC_User::setUserId($userId); |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | - public function createToken($editorId, File $file, string $filePath, IShare $share = null): string { |
|
| 249 | - $token = $this->random->generate(64, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 250 | - $query = $this->connection->getQueryBuilder(); |
|
| 251 | - $query->insert(self::TABLE_TOKENS) |
|
| 252 | - ->values([ |
|
| 253 | - 'token' => $query->createNamedParameter($token), |
|
| 254 | - 'editor_id' => $query->createNamedParameter($editorId), |
|
| 255 | - 'file_id' => $query->createNamedParameter($file->getId()), |
|
| 256 | - 'file_path' => $query->createNamedParameter($filePath), |
|
| 257 | - 'user_id' => $query->createNamedParameter($this->userId), |
|
| 258 | - 'share_id' => $query->createNamedParameter($share !== null ? $share->getId(): null), |
|
| 259 | - 'timestamp' => $query->createNamedParameter(time()) |
|
| 260 | - ]); |
|
| 261 | - $query->execute(); |
|
| 262 | - return $token; |
|
| 263 | - } |
|
| 248 | + public function createToken($editorId, File $file, string $filePath, IShare $share = null): string { |
|
| 249 | + $token = $this->random->generate(64, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 250 | + $query = $this->connection->getQueryBuilder(); |
|
| 251 | + $query->insert(self::TABLE_TOKENS) |
|
| 252 | + ->values([ |
|
| 253 | + 'token' => $query->createNamedParameter($token), |
|
| 254 | + 'editor_id' => $query->createNamedParameter($editorId), |
|
| 255 | + 'file_id' => $query->createNamedParameter($file->getId()), |
|
| 256 | + 'file_path' => $query->createNamedParameter($filePath), |
|
| 257 | + 'user_id' => $query->createNamedParameter($this->userId), |
|
| 258 | + 'share_id' => $query->createNamedParameter($share !== null ? $share->getId(): null), |
|
| 259 | + 'timestamp' => $query->createNamedParameter(time()) |
|
| 260 | + ]); |
|
| 261 | + $query->execute(); |
|
| 262 | + return $token; |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - /** |
|
| 266 | - * @param $userId |
|
| 267 | - * @param $fileId |
|
| 268 | - * @param null $filePath |
|
| 269 | - * @return Node |
|
| 270 | - * @throws NotFoundException |
|
| 271 | - */ |
|
| 272 | - public function getFileForToken($userId, $fileId, $filePath = null): Node { |
|
| 273 | - $userFolder = $this->rootFolder->getUserFolder($userId); |
|
| 274 | - if ($filePath !== null) { |
|
| 275 | - return $userFolder->get($filePath); |
|
| 276 | - } |
|
| 277 | - $files = $userFolder->getById($fileId); |
|
| 278 | - if (count($files) === 0) { |
|
| 279 | - throw new NotFoundException('File nound found by id ' . $fileId); |
|
| 280 | - } |
|
| 281 | - return $files[0]; |
|
| 282 | - } |
|
| 265 | + /** |
|
| 266 | + * @param $userId |
|
| 267 | + * @param $fileId |
|
| 268 | + * @param null $filePath |
|
| 269 | + * @return Node |
|
| 270 | + * @throws NotFoundException |
|
| 271 | + */ |
|
| 272 | + public function getFileForToken($userId, $fileId, $filePath = null): Node { |
|
| 273 | + $userFolder = $this->rootFolder->getUserFolder($userId); |
|
| 274 | + if ($filePath !== null) { |
|
| 275 | + return $userFolder->get($filePath); |
|
| 276 | + } |
|
| 277 | + $files = $userFolder->getById($fileId); |
|
| 278 | + if (count($files) === 0) { |
|
| 279 | + throw new NotFoundException('File nound found by id ' . $fileId); |
|
| 280 | + } |
|
| 281 | + return $files[0]; |
|
| 282 | + } |
|
| 283 | 283 | |
| 284 | - public function isEnabled(): bool { |
|
| 285 | - if (!$this->encryptionManager->isEnabled()) { |
|
| 286 | - return true; |
|
| 287 | - } |
|
| 284 | + public function isEnabled(): bool { |
|
| 285 | + if (!$this->encryptionManager->isEnabled()) { |
|
| 286 | + return true; |
|
| 287 | + } |
|
| 288 | 288 | |
| 289 | - try { |
|
| 290 | - $moduleId = $this->encryptionManager->getDefaultEncryptionModuleId(); |
|
| 291 | - $module = $this->encryptionManager->getEncryptionModule($moduleId); |
|
| 292 | - /** @var \OCA\Encryption\Util $util */ |
|
| 293 | - $util = \OC::$server->get(\OCA\Encryption\Util::class); |
|
| 294 | - if ($module->isReadyForUser($this->userId) && $util->isMasterKeyEnabled()) { |
|
| 295 | - return true; |
|
| 296 | - } |
|
| 297 | - } catch (Throwable $e) { |
|
| 298 | - } |
|
| 299 | - return false; |
|
| 300 | - } |
|
| 289 | + try { |
|
| 290 | + $moduleId = $this->encryptionManager->getDefaultEncryptionModuleId(); |
|
| 291 | + $module = $this->encryptionManager->getEncryptionModule($moduleId); |
|
| 292 | + /** @var \OCA\Encryption\Util $util */ |
|
| 293 | + $util = \OC::$server->get(\OCA\Encryption\Util::class); |
|
| 294 | + if ($module->isReadyForUser($this->userId) && $util->isMasterKeyEnabled()) { |
|
| 295 | + return true; |
|
| 296 | + } |
|
| 297 | + } catch (Throwable $e) { |
|
| 298 | + } |
|
| 299 | + return false; |
|
| 300 | + } |
|
| 301 | 301 | } |
@@ -37,59 +37,59 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | interface IManager { |
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Register a new editor |
|
| 42 | - * |
|
| 43 | - * @since 18.0.0 |
|
| 44 | - * @param IEditor $directEditor |
|
| 45 | - */ |
|
| 46 | - public function registerDirectEditor(IEditor $directEditor): void; |
|
| 40 | + /** |
|
| 41 | + * Register a new editor |
|
| 42 | + * |
|
| 43 | + * @since 18.0.0 |
|
| 44 | + * @param IEditor $directEditor |
|
| 45 | + */ |
|
| 46 | + public function registerDirectEditor(IEditor $directEditor): void; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Open the editing page for a provided token |
|
| 50 | - * |
|
| 51 | - * @since 18.0.0 |
|
| 52 | - * @param string $token |
|
| 53 | - * @return Response |
|
| 54 | - */ |
|
| 55 | - public function edit(string $token): Response; |
|
| 48 | + /** |
|
| 49 | + * Open the editing page for a provided token |
|
| 50 | + * |
|
| 51 | + * @since 18.0.0 |
|
| 52 | + * @param string $token |
|
| 53 | + * @return Response |
|
| 54 | + */ |
|
| 55 | + public function edit(string $token): Response; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Create a new token based on the file path and editor details |
|
| 59 | - * |
|
| 60 | - * @since 18.0.0 |
|
| 61 | - * @param string $path |
|
| 62 | - * @param string $editorId |
|
| 63 | - * @param string $creatorId |
|
| 64 | - * @param null $templateId |
|
| 65 | - * @return string |
|
| 66 | - * @throws NotPermittedException |
|
| 67 | - * @throws RuntimeException |
|
| 68 | - */ |
|
| 69 | - public function create(string $path, string $editorId, string $creatorId, $templateId = null): string; |
|
| 57 | + /** |
|
| 58 | + * Create a new token based on the file path and editor details |
|
| 59 | + * |
|
| 60 | + * @since 18.0.0 |
|
| 61 | + * @param string $path |
|
| 62 | + * @param string $editorId |
|
| 63 | + * @param string $creatorId |
|
| 64 | + * @param null $templateId |
|
| 65 | + * @return string |
|
| 66 | + * @throws NotPermittedException |
|
| 67 | + * @throws RuntimeException |
|
| 68 | + */ |
|
| 69 | + public function create(string $path, string $editorId, string $creatorId, $templateId = null): string; |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Get the token details for a given token |
|
| 73 | - * |
|
| 74 | - * @since 18.0.0 |
|
| 75 | - * @param string $token |
|
| 76 | - * @return IToken |
|
| 77 | - */ |
|
| 78 | - public function getToken(string $token): IToken; |
|
| 71 | + /** |
|
| 72 | + * Get the token details for a given token |
|
| 73 | + * |
|
| 74 | + * @since 18.0.0 |
|
| 75 | + * @param string $token |
|
| 76 | + * @return IToken |
|
| 77 | + */ |
|
| 78 | + public function getToken(string $token): IToken; |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Cleanup expired tokens |
|
| 82 | - * |
|
| 83 | - * @since 18.0.0 |
|
| 84 | - * @return int number of deleted tokens |
|
| 85 | - */ |
|
| 86 | - public function cleanup(): int; |
|
| 80 | + /** |
|
| 81 | + * Cleanup expired tokens |
|
| 82 | + * |
|
| 83 | + * @since 18.0.0 |
|
| 84 | + * @return int number of deleted tokens |
|
| 85 | + */ |
|
| 86 | + public function cleanup(): int; |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Check if direct editing is enabled |
|
| 90 | - * |
|
| 91 | - * @since 20.0.0 |
|
| 92 | - * @return bool |
|
| 93 | - */ |
|
| 94 | - public function isEnabled(): bool; |
|
| 88 | + /** |
|
| 89 | + * Check if direct editing is enabled |
|
| 90 | + * |
|
| 91 | + * @since 20.0.0 |
|
| 92 | + * @return bool |
|
| 93 | + */ |
|
| 94 | + public function isEnabled(): bool; |
|
| 95 | 95 | } |