From 458c2fa2971e6595a18a289b0afeb4a79ea0e0d3 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Thu, 12 May 2022 17:08:54 +0200 Subject: Remove OCP\App and OCP\BackgroundJob Both deprecated since NC 23 IAppManager is the replacement for OCP\App unfortunately it can't be dependency injected in classes used by the installed otherwise the database connection is initialised too early Signed-off-by: Carl Schwan --- lib/private/Encryption/File.php | 27 +++++++++++++++------------ lib/private/Encryption/Util.php | 37 ++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 33 deletions(-) (limited to 'lib/private/Encryption') diff --git a/lib/private/Encryption/File.php b/lib/private/Encryption/File.php index 844059923bd..87bc35bc159 100644 --- a/lib/private/Encryption/File.php +++ b/lib/private/Encryption/File.php @@ -29,27 +29,22 @@ namespace OC\Encryption; use OCP\Cache\CappedMemoryCache; use OCA\Files_External\Service\GlobalStoragesService; +use OCP\App\IAppManager; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\Share\IManager; class File implements \OCP\Encryption\IFile { - - /** @var Util */ - protected $util; - - /** @var IRootFolder */ - private $rootFolder; - - /** @var IManager */ - private $shareManager; + protected Util $util; + private IRootFolder $rootFolder; + private IManager $shareManager; /** - * cache results of already checked folders - * + * Cache results of already checked folders * @var CappedMemoryCache */ protected CappedMemoryCache $cache; + private ?IAppManager $appManager = null; public function __construct(Util $util, IRootFolder $rootFolder, @@ -60,6 +55,14 @@ class File implements \OCP\Encryption\IFile { $this->shareManager = $shareManager; } + public function getAppManager(): IAppManager { + // Lazy evaluate app manager as it initialize the db too early otherwise + if ($this->appManager) { + return $this->appManager; + } + $this->appManager = \OCP\Server::get(IAppManager::class); + return $this->appManager; + } /** * Get list of users with access to the file @@ -110,7 +113,7 @@ class File implements \OCP\Encryption\IFile { } // check if it is a group mount - if (\OCP\App::isEnabled("files_external")) { + if ($this->getAppManager()->isEnabledForUser("files_external")) { /** @var GlobalStoragesService $storageService */ $storageService = \OC::$server->get(GlobalStoragesService::class); $storages = $storageService->getAllStorages(); diff --git a/lib/private/Encryption/Util.php b/lib/private/Encryption/Util.php index 174af2e8b89..410ea19da81 100644 --- a/lib/private/Encryption/Util.php +++ b/lib/private/Encryption/Util.php @@ -34,9 +34,12 @@ use OC\Files\Filesystem; use OC\Files\View; use OCA\Files_External\Lib\StorageConfig; use OCA\Files_External\Service\GlobalStoragesService; +use OCP\App\IAppManager; use OCP\Encryption\IEncryptionModule; use OCP\IConfig; +use OCP\IGroupManager; use OCP\IUser; +use OCP\IUserManager; class Util { public const HEADER_START = 'HBEGIN'; @@ -65,29 +68,23 @@ class Util { /** @var array */ protected $ocHeaderKeys; - /** @var \OC\User\Manager */ - protected $userManager; - /** @var IConfig */ protected $config; /** @var array paths excluded from encryption */ protected $excludedPaths; - - /** @var \OC\Group\Manager $manager */ - protected $groupManager; + protected IGroupManager $groupManager; + protected IUserManager $userManager; /** * * @param View $rootView - * @param \OC\User\Manager $userManager - * @param \OC\Group\Manager $groupManager * @param IConfig $config */ public function __construct( View $rootView, - \OC\User\Manager $userManager, - \OC\Group\Manager $groupManager, + IUserManager $userManager, + IGroupManager $groupManager, IConfig $config) { $this->ocHeaderKeys = [ self::HEADER_ENCRYPTION_MODULE_KEY @@ -275,7 +272,7 @@ class Util { } else { $result = array_merge($result, $users); - $groupManager = \OC::$server->getGroupManager(); + $groupManager = $this->groupManager; foreach ($groups as $group) { $groupObject = $groupManager->get($group); if ($groupObject) { @@ -299,7 +296,8 @@ class Util { * @return boolean */ public function isSystemWideMountPoint($path, $uid) { - if (\OCP\App::isEnabled("files_external")) { + // No DI here as this initialise the db too soon + if (\OCP\Server::get(IAppManager::class)->isEnabledForUser("files_external")) { /** @var GlobalStoragesService $storageService */ $storageService = \OC::$server->get(GlobalStoragesService::class); $storages = $storageService->getAllStorages(); @@ -377,32 +375,29 @@ class Util { } /** - * check if recovery key is enabled for user - * - * @param string $uid - * @return boolean + * Check if recovery key is enabled for user */ - public function recoveryEnabled($uid) { + public function recoveryEnabled(string $uid): bool { $enabled = $this->config->getUserValue($uid, 'encryption', 'recovery_enabled', '0'); return $enabled === '1'; } /** - * set new key storage root + * Set new key storage root * * @param string $root new key store root relative to the data folder */ - public function setKeyStorageRoot($root) { + public function setKeyStorageRoot(string $root): void { $this->config->setAppValue('core', 'encryption_key_storage_root', $root); } /** - * get key storage root + * Get key storage root * * @return string key storage root */ - public function getKeyStorageRoot() { + public function getKeyStorageRoot(): string { return $this->config->getAppValue('core', 'encryption_key_storage_root', ''); } } -- cgit v1.2.3