diff --git a/lib/Middleware/SessionMiddleware.php b/lib/Middleware/SessionMiddleware.php index 2c0852be7fc..72f19602133 100644 --- a/lib/Middleware/SessionMiddleware.php +++ b/lib/Middleware/SessionMiddleware.php @@ -9,6 +9,7 @@ use OC\User\NoUserException; use OCA\Text\Controller\ISessionAwareController; +use OCA\Text\Db\Document; use OCA\Text\Exception\AccountDisabledException; use OCA\Text\Exception\InvalidDocumentBaseVersionEtagException; use OCA\Text\Exception\InvalidSessionException; @@ -37,6 +38,7 @@ use ReflectionException; class SessionMiddleware extends Middleware { + private ?Document $document = null; public function __construct( private IRequest $request, @@ -88,7 +90,7 @@ private function assertDocumentBaseVersionEtag(): void { $documentId = (int)$this->request->getParam('documentId'); $baseVersionEtag = $this->request->getParam('baseVersionEtag'); - $document = $this->documentService->getDocument($documentId); + $document = $this->getDocument($documentId); if ($baseVersionEtag && $document?->getBaseVersionEtag() !== $baseVersionEtag) { throw new InvalidDocumentBaseVersionEtagException(); } @@ -116,7 +118,7 @@ private function assertDocumentSession(ISessionAwareController $controller): voi } } - $document = $this->documentService->getDocument($documentId); + $document = $this->getDocument($documentId); if (!$document) { throw new InvalidSessionException(); } @@ -194,6 +196,13 @@ private function assertUserOrShareToken(ISessionAwareController $controller): vo throw new InvalidSessionException(); } + private function getDocument(int $documentId): ?Document { + if ($this->document?->getId() !== $documentId) { + $this->document = $this->documentService->getDocument($documentId); + } + return $this->document; + } + public function afterException($controller, $methodName, \Exception $exception): JSONResponse|Response { if ($exception instanceof InvalidDocumentBaseVersionEtagException) { return new JSONResponse(['error' => $this->l10n->t('Editing session has expired. Please reload the page.')], Http::STATUS_PRECONDITION_FAILED);