From 5b86077a1cbdade2300c950de8e2bac3b374820c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Tue, 19 Dec 2023 16:07:49 +0100 Subject: [PATCH] feat: Add API parameters to specify the lock type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Controller/LockController.php | 6 +++--- lib/DAV/LockPlugin.php | 6 ++++-- lib/Service/LockService.php | 25 ++++++++++--------------- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/lib/Controller/LockController.php b/lib/Controller/LockController.php index 7eb7970f..1636ec66 100644 --- a/lib/Controller/LockController.php +++ b/lib/Controller/LockController.php @@ -90,13 +90,13 @@ public function __construct( * * @return DataResponse */ - public function locking(string $fileId): DataResponse { + public function locking(string $fileId, int $lockType = ILock::TYPE_USER): DataResponse { try { $user = $this->userSession->getUser(); $file = $this->fileService->getFileFromId($user->getUID(), (int)$fileId); $lock = $this->lockService->lock(new LockContext( - $file, ILock::TYPE_USER, $user->getUID() + $file, $lockType, $user->getUID() )); return new DataResponse($lock, Http::STATUS_OK); @@ -115,7 +115,7 @@ public function locking(string $fileId): DataResponse { * * @return DataResponse */ - public function unlocking(string $fileId): DataResponse { + public function unlocking(string $fileId, int $lockType = ILock::TYPE_USER): DataResponse { try { $user = $this->userSession->getUser(); $this->lockService->enableUserOverride(); diff --git a/lib/DAV/LockPlugin.php b/lib/DAV/LockPlugin.php index 37caeac9..d0591501 100644 --- a/lib/DAV/LockPlugin.php +++ b/lib/DAV/LockPlugin.php @@ -183,13 +183,14 @@ public function customProperties(PropFind $propFind, INode $node) { public function httpLock(RequestInterface $request, ResponseInterface $response) { if ($request->getHeader('X-User-Lock')) { + $lockType = (int)($request->getHeader('X-User-Lock-Type') ?? ILock::TYPE_USER); $response->setHeader('Content-Type', 'application/xml; charset=utf-8'); $file = $this->fileService->getFileFromAbsoluteUri($this->server->getRequestUri()); try { $lockInfo = $this->lockService->lock(new LockContext( - $file, ILock::TYPE_USER, $this->userSession->getUser()->getUID() + $file, $lockType, $this->userSession->getUser()->getUID() )); $response->setStatus(200); $response->setBody( @@ -216,6 +217,7 @@ public function httpLock(RequestInterface $request, ResponseInterface $response) public function httpUnlock(RequestInterface $request, ResponseInterface $response) { if ($request->getHeader('X-User-Lock')) { + $lockType = (int)($request->getHeader('X-User-Lock-Type') ?? ILock::TYPE_USER); $response->setHeader('Content-Type', 'application/xml; charset=utf-8'); $file = $this->fileService->getFileFromAbsoluteUri($this->server->getRequestUri()); @@ -223,7 +225,7 @@ public function httpUnlock(RequestInterface $request, ResponseInterface $respons try { $this->lockService->enableUserOverride(); $this->lockService->unlock(new LockContext( - $file, ILock::TYPE_USER, $this->userSession->getUser()->getUID() + $file, $lockType, $this->userSession->getUser()->getUID() )); $response->setStatus(200); $response->setBody( diff --git a/lib/Service/LockService.php b/lib/Service/LockService.php index 7adabe9b..0db5eb72 100644 --- a/lib/Service/LockService.php +++ b/lib/Service/LockService.php @@ -47,12 +47,8 @@ use OCP\Files\NotFoundException; use OCP\IL10N; use OCP\IUserManager; +use OCP\IUserSession; -/** - * Class LockService - * - * @package OCA\FilesLock\Service - */ class LockService { public const PREFIX = 'files_lock'; @@ -61,7 +57,6 @@ class LockService { use TLogger; - private ?string $userId; private IUserManager $userManager; private IL10N $l10n; private LocksRequest $locksRequest; @@ -69,6 +64,7 @@ class LockService { private ConfigService $configService; private IAppManager $appManager; private IEventDispatcher $eventDispatcher; + private IUserSession $userSession; private array $locks = []; @@ -79,16 +75,15 @@ class LockService { public function __construct( - $userId, IL10N $l10n, IUserManager $userManager, LocksRequest $locksRequest, FileService $fileService, ConfigService $configService, IAppManager $appManager, - IEventDispatcher $eventDispatcher + IEventDispatcher $eventDispatcher, + IUserSession $userSession, ) { - $this->userId = $userId; $this->l10n = $l10n; $this->userManager = $userManager; $this->locksRequest = $locksRequest; @@ -96,6 +91,7 @@ public function __construct( $this->configService = $configService; $this->appManager = $appManager; $this->eventDispatcher = $eventDispatcher; + $this->userSession = $userSession; $this->setup('app', 'files_lock'); } @@ -224,7 +220,7 @@ public function enableUserOverride(): void { } public function canUnlock(LockContext $request, FileLock $current): void { - $isSameUser = $current->getOwner() === $this->userId; + $isSameUser = $current->getOwner() === $this->userSession->getUser()?->getUID(); $isSameToken = $request->getOwner() === $current->getToken(); $isSameOwner = $request->getOwner() === $current->getOwner(); $isSameType = $request->getType() === $current->getType(); @@ -251,7 +247,7 @@ public function canUnlock(LockContext $request, FileLock $current): void { 'OCA\Files_External\Config\ConfigAdapter' ]; if ($request->getType() === ILock::TYPE_USER - && $request->getNode()->getOwner()->getUID() === $this->userId + && $request->getNode()->getOwner()->getUID() === $this->userSession->getUser()?->getUID() && !in_array($request->getNode()->getMountPoint()->getMountProvider(), $ignoreFileOwnership) ) { return; @@ -269,22 +265,21 @@ public function canUnlock(LockContext $request, FileLock $current): void { * @throws NotFoundException * @throws UnauthorizedUnlockException */ - public function unlockFile(int $fileId, string $userId, bool $force = false): FileLock { + public function unlockFile(int $fileId, string $userId, bool $force = false, int $lockType = ILock::TYPE_USER): FileLock { $lock = $this->getLockForNodeId($fileId); if (!$lock) { throw new LockNotFoundException(); } - $type = ILock::TYPE_USER; if ($force) { $userId = $lock->getOwner(); - $type = $lock->getType(); + $lockType = $lock->getType(); } $node = $this->fileService->getFileFromId($userId, $fileId); $lock = new LockContext( $node, - $type, + $lockType, $userId, ); $this->propagateEtag($lock);