diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index c7105b13..a8491633 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -12,6 +12,7 @@ use OC\Files\Filesystem; use OCA\Files\Event\LoadAdditionalScriptsEvent; use OCA\FilesLock\Capability; +use OCA\FilesLock\ConfigLexicon; use OCA\FilesLock\Listeners\LoadAdditionalScripts; use OCA\FilesLock\Listeners\PropfindPropertiesListener; use OCA\FilesLock\LockProvider; @@ -56,6 +57,7 @@ public function register(IRegistrationContext $context): void { BeforeRemotePropfindEvent::class, PropfindPropertiesListener::class ); + $context->registerConfigLexicon(ConfigLexicon::class); } #[\Override] diff --git a/lib/Command/Lock.php b/lib/Command/Lock.php index a6a820a7..3362e54c 100644 --- a/lib/Command/Lock.php +++ b/lib/Command/Lock.php @@ -17,9 +17,9 @@ use OCA\FilesLock\Exceptions\SuccessException; use OCA\FilesLock\Exceptions\UnauthorizedUnlockException; use OCA\FilesLock\Model\FileLock; -use OCA\FilesLock\Service\ConfigService; use OCA\FilesLock\Service\FileService; use OCA\FilesLock\Service\LockService; +use OCP\AppFramework\Services\IAppConfig; use OCP\Files\InvalidPathException; use OCP\Files\Lock\ILock; use OCP\Files\Lock\LockContext; @@ -33,41 +33,14 @@ use Symfony\Component\Console\Question\ConfirmationQuestion; class Lock extends Base { - /** @var IUserManager */ - private $userManager; - - /** @var LocksRequest */ - private $locksRequest; - - /** @var FileService */ - private $fileService; - - /** @var LockService */ - private $lockService; - - /** @var ConfigService */ - private $configService; - - /** - * CacheUpdate constructor. - * - * @param IUserManager $userManager - * @param FileService $fileService - * @param LockService $lockService - * @param LocksRequest $locksRequest - * @param ConfigService $configService - */ public function __construct( - IUserManager $userManager, LocksRequest $locksRequest, FileService $fileService, - LockService $lockService, ConfigService $configService, + private readonly IUserManager $userManager, + private readonly LocksRequest $locksRequest, + private readonly FileService $fileService, + private readonly LockService $lockService, + private readonly IAppConfig $appConfig, ) { parent::__construct(); - - $this->userManager = $userManager; - $this->locksRequest = $locksRequest; - $this->fileService = $fileService; - $this->lockService = $lockService; - $this->configService = $configService; } /** @@ -227,7 +200,7 @@ private function uninstallApp(InputInterface $input, OutputInterface $output) { } $this->locksRequest->uninstall(); - $this->configService->unsetAppConfig(); + $this->appConfig->deleteAppValues(); $output->writeln('FilesLock App fully uninstalled.'); throw new SuccessException(); diff --git a/lib/ConfigLexicon.php b/lib/ConfigLexicon.php new file mode 100644 index 00000000..4335f5c1 --- /dev/null +++ b/lib/ConfigLexicon.php @@ -0,0 +1,44 @@ +configService = $configService; + public function __construct( + private readonly IAppConfig $appConfig, + ) { + $this->timeout = $this->appConfig->getAppValueInt(ConfigLexicon::LOCK_TIMEOUT) * 60; } /** @@ -117,7 +118,7 @@ public function getLocksFromRequest(CoreQueryBuilder $qb): array { * @return FileLock */ public function parseLockSelectSql(array $data): FileLock { - $lock = new FileLock($this->configService->getTimeoutSeconds()); + $lock = new FileLock($this->timeout); $lock->importFromDatabase($data); return $lock; diff --git a/lib/Service/ConfigService.php b/lib/Service/ConfigService.php deleted file mode 100644 index 88ec7045..00000000 --- a/lib/Service/ConfigService.php +++ /dev/null @@ -1,103 +0,0 @@ - '-1' - ]; - - /** @var string */ - private $appName; - - /** @var IConfig */ - private $config; - - /** @var string */ - private $userId; - - /** @var IRequest */ - private $request; - - /** - * ConfigService constructor. - * - * @param string $appName - * @param IConfig $config - * @param IRequest $request - * @param string $userId - */ - public function __construct($appName, IConfig $config, IRequest $request, $userId) { - $this->appName = $appName; - $this->config = $config; - $this->request = $request; - $this->userId = $userId; - } - - /** - * @return int - */ - public function getTimeoutSeconds(): int { - return ((int)$this->getAppValue(ConfigService::LOCK_TIMEOUT)) * 60; - } - - /** - * Get a value by key - * - * @param string $key - * - * @return string - */ - public function getAppValue($key) { - $defaultValue = null; - - if (array_key_exists($key, $this->defaults)) { - $defaultValue = $this->defaults[$key]; - } - - return $this->config->getAppValue($this->appName, $key, $defaultValue); - } - - /** - * Set a value by key - * - * @param string $key - * @param string $value - * - * @return void - */ - public function setAppValue($key, $value) { - $this->config->setAppValue($this->appName, $key, $value); - } - - /** - * remove a key - * - * @param string $key - * - * @return string - */ - public function deleteAppValue($key) { - return $this->config->deleteAppValue($this->appName, $key); - } - - /** - * - */ - public function unsetAppConfig() { - $this->config->deleteAppValues(Application::APP_ID); - } -} diff --git a/lib/Service/LockService.php b/lib/Service/LockService.php index 095f5d05..7fbae143 100644 --- a/lib/Service/LockService.php +++ b/lib/Service/LockService.php @@ -13,12 +13,14 @@ use OC\Files\Storage\DAV; use OC\Files\Storage\Wrapper\Wrapper; use OCA\FilesLock\AppInfo\Application; +use OCA\FilesLock\ConfigLexicon; use OCA\FilesLock\Db\LocksRequest; use OCA\FilesLock\Exceptions\LockNotFoundException; use OCA\FilesLock\Exceptions\UnauthorizedUnlockException; use OCA\FilesLock\Model\FileLock; use OCA\FilesLock\Tools\Traits\TStringTools; use OCP\App\IAppManager; +use OCP\AppFramework\Services\IAppConfig; use OCP\Constants; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\InvalidPathException; @@ -43,7 +45,6 @@ class LockService { private IL10N $l10n; private LocksRequest $locksRequest; private FileService $fileService; - private ConfigService $configService; private IAppManager $appManager; private IEventDispatcher $eventDispatcher; private IUserSession $userSession; @@ -62,7 +63,7 @@ public function __construct( IUserManager $userManager, LocksRequest $locksRequest, FileService $fileService, - ConfigService $configService, + private IAppConfig $appConfig, IAppManager $appManager, IEventDispatcher $eventDispatcher, IUserSession $userSession, @@ -74,7 +75,6 @@ public function __construct( $this->userManager = $userManager; $this->locksRequest = $locksRequest; $this->fileService = $fileService; - $this->configService = $configService; $this->appManager = $appManager; $this->eventDispatcher = $eventDispatcher; $this->userSession = $userSession; @@ -159,6 +159,7 @@ public function getLockForNodeIds(array $nodeIds): array { public function lock(LockContext $lockScope): FileLock { $this->canLock($lockScope); + $timeout = $this->appConfig->getAppValueInt(ConfigLexicon::LOCK_TIMEOUT) * 60; try { $known = $this->getLockFromFileId($lockScope->getNode()->getId()); @@ -167,9 +168,7 @@ public function lock(LockContext $lockScope): FileLock { if ( $known->getType() === $lockScope->getType() && ($known->getOwner() === $lockScope->getOwner() || $known->getToken() === $lockScope->getOwner()) ) { - $known->setTimeout( - $known->getETA() !== FileLock::ETA_INFINITE ? $known->getTimeout() - $known->getETA() + $this->configService->getTimeoutSeconds() : 0 - ); + $known->setTimeout($known->getETA() !== FileLock::ETA_INFINITE ? $known->getTimeout() - $known->getETA() + $timeout : 0); $this->logger->notice('extending existing lock', ['fileLock' => $known]); $this->locksRequest->update($known); $this->lockCache[$lockScope->getNode()->getId()] = $known; @@ -180,7 +179,7 @@ public function lock(LockContext $lockScope): FileLock { $this->injectMetadata($known); throw new OwnerLockedException($known); } catch (LockNotFoundException $e) { - $lock = FileLock::fromLockScope($lockScope, $this->configService->getTimeoutSeconds()); + $lock = FileLock::fromLockScope($lockScope, $timeout); $this->generateToken($lock); $lock->setCreation(time()); $this->logger->notice('locking file', ['fileLock' => $lock]); @@ -306,8 +305,7 @@ public function unlockFile(int $fileId, string $userId, bool $force = false, int * @return FileLock[] */ public function getDeprecatedLocks(int $limit = 0): array { - $timeout = (int)$this->configService->getAppValue(ConfigService::LOCK_TIMEOUT); - + $timeout = $this->appConfig->getAppValueInt(ConfigLexicon::LOCK_TIMEOUT); if ($timeout === FileLock::ETA_INFINITE) { return []; } diff --git a/tests/Feature/LockFeatureTest.php b/tests/Feature/LockFeatureTest.php index b75e7d04..84a66883 100644 --- a/tests/Feature/LockFeatureTest.php +++ b/tests/Feature/LockFeatureTest.php @@ -7,8 +7,8 @@ use OC\Files\Lock\LockManager; use OCA\FilesLock\AppInfo\Application; +use OCA\FilesLock\ConfigLexicon; use OCA\FilesLock\Model\FileLock; -use OCA\FilesLock\Service\ConfigService; use OCA\FilesLock\Service\LockService; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Files\File; @@ -189,7 +189,7 @@ public function testUnlockEtagShare() { } public function testLockUserExpire() { - \OCP\Server::get(IConfig::class)->setAppValue(Application::APP_ID, ConfigService::LOCK_TIMEOUT, 30); + \OCP\Server::get(IConfig::class)->setAppValue(Application::APP_ID, ConfigLexicon::LOCK_TIMEOUT, 30); $file = $this->loginAndGetUserFolder(self::TEST_USER1) ->newFile('test-file-expire', 'AAA'); $this->shareFileWithUser($file, self::TEST_USER1, self::TEST_USER2); @@ -213,7 +213,7 @@ public function testLockUserExpire() { } public function testExpiredLocksAreDeprecated() { - \OCP\Server::get(IConfig::class)->setAppValue(Application::APP_ID, ConfigService::LOCK_TIMEOUT, 30); + \OCP\Server::get(IConfig::class)->setAppValue(Application::APP_ID, ConfigLexicon::LOCK_TIMEOUT, 30); $file = $this->loginAndGetUserFolder(self::TEST_USER1) ->newFile('test-expired-lock-is-deprecated', 'AAA'); $this->lockManager->lock(new LockContext($file, ILock::TYPE_USER, self::TEST_USER1)); @@ -224,7 +224,7 @@ public function testExpiredLocksAreDeprecated() { public function testRemoveLocks(): void { $service = \OCP\Server::get(LockService::class); - \OCP\Server::get(IConfig::class)->setAppValue(Application::APP_ID, ConfigService::LOCK_TIMEOUT, 30); + \OCP\Server::get(IConfig::class)->setAppValue(Application::APP_ID, ConfigLexicon::LOCK_TIMEOUT, 30); $file = $this->loginAndGetUserFolder(self::TEST_USER1)->newFile('test-expired-lock-is-deprecated', 'AAA'); $lock1 = $this->lockManager->lock(new LockContext($file, ILock::TYPE_USER, self::TEST_USER1)); $file2 = $this->loginAndGetUserFolder(self::TEST_USER1)->newFile('test-expired-lock-is-deprecated-2', 'AAA'); @@ -244,7 +244,7 @@ public function testRemoveLocks(): void { } public function testLockUserInfinite() { - \OCP\Server::get(IConfig::class)->setAppValue(Application::APP_ID, ConfigService::LOCK_TIMEOUT, 0); + \OCP\Server::get(IConfig::class)->setAppValue(Application::APP_ID, ConfigLexicon::LOCK_TIMEOUT, 0); $file = $this->loginAndGetUserFolder(self::TEST_USER1) ->newFile('test-file-infinite', 'AAA'); $this->shareFileWithUser($file, self::TEST_USER1, self::TEST_USER2); @@ -358,7 +358,7 @@ public function testLockDifferentAppsPublic() { * Ensure that a lock can be extended and the same lock is kept */ public function testExtendLock() { - \OCP\Server::get(IConfig::class)->setAppValue(Application::APP_ID, ConfigService::LOCK_TIMEOUT, 15); + \OCP\Server::get(IConfig::class)->setAppValue(Application::APP_ID, ConfigLexicon::LOCK_TIMEOUT, 15); // Create a file and lock it $file = $this->loginAndGetUserFolder(self::TEST_USER1)->newFile('test-file', 'AAA'); @@ -390,7 +390,7 @@ public function testExtendLock() { * Regression test for https://github.com/nextcloud/files_lock/issues/130 */ public function testExtendInfiniteLock() { - \OCP\Server::get(IConfig::class)->setAppValue(Application::APP_ID, ConfigService::LOCK_TIMEOUT, '0'); + \OCP\Server::get(IConfig::class)->setAppValue(Application::APP_ID, ConfigLexicon::LOCK_TIMEOUT, '0'); // Create a file and lock it $file = $this->loginAndGetUserFolder(self::TEST_USER1)->newFile('test-file', 'AAA'); @@ -413,7 +413,7 @@ public function testExtendInfiniteLock() { } public function testUnlockStaleClientLock() { - \OCP\Server::get(IConfig::class)->setAppValue(Application::APP_ID, ConfigService::LOCK_TIMEOUT, '0'); + \OCP\Server::get(IConfig::class)->setAppValue(Application::APP_ID, ConfigLexicon::LOCK_TIMEOUT, '0'); // Create a file and lock it as the desktop client would $file = $this->loginAndGetUserFolder(self::TEST_USER1)->newFile('test-file-client', 'AAA');