nextcloud/apps/encryption/lib/Util.php
Stephen Cuppett f2d0cb1876 refactor(encryption): Migrate remaining deprecated IConfig calls to typed APIs
Replace IConfig::{get,set}UserValue for the per-user 'recoveryEnabled' key
with IUserConfig::{getValueBool,setValueBool}, and IConfig::getAppValue for
'useMasterKey' with IAppConfig::getValueBool. IConfig is removed from Util
and Recovery constructors entirely. Clears the DeprecatedMethod psalm-baseline
entries for apps/encryption/lib/Util.php and the string-typed recoveryAdminEnabled
calls that were still in Recovery.php.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Stephen Cuppett <steve@cuppett.com>
2026-07-04 08:19:10 -04:00

113 lines
2.7 KiB
PHP

<?php
/**
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Encryption;
use OC\Files\Storage\Storage;
use OC\Files\View;
use OCA\Encryption\Crypto\Crypt;
use OCP\Config\IUserConfig;
use OCP\Files\Storage\IStorage;
use OCP\IAppConfig;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\PreConditionNotMetException;
class Util {
private IUser|false $user;
public function __construct(
private View $files,
private Crypt $crypt,
IUserSession $userSession,
private IAppConfig $appConfig,
private IUserConfig $userConfig,
private IUserManager $userManager,
) {
$this->user = $userSession->isLoggedIn() ? $userSession->getUser() : false;
}
/**
* check if recovery key is enabled for user
*
* @param string $uid
* @return bool
*/
public function isRecoveryEnabledForUser($uid) {
return $this->userConfig->getValueBool($uid, 'encryption', 'recoveryEnabled');
}
/**
* check if the home storage should be encrypted
*
* @return bool
*/
public function shouldEncryptHomeStorage() {
return $this->appConfig->getValueBool('encryption', 'encryptHomeStorage', true);
}
/**
* set the home storage encryption on/off
*
* @param bool $encryptHomeStorage
*/
public function setEncryptHomeStorage(bool $encryptHomeStorage) {
$this->appConfig->setValueBool('encryption', 'encryptHomeStorage', $encryptHomeStorage);
}
/**
* check if master key is enabled
*/
public function isMasterKeyEnabled(): bool {
return $this->appConfig->getValueBool('encryption', 'useMasterKey', true);
}
public function setRecoveryForUser(bool $enabled): bool {
try {
$this->userConfig->setValueBool($this->user->getUID(), 'encryption', 'recoveryEnabled', $enabled);
return true;
} catch (PreConditionNotMetException $e) {
return false;
}
}
/**
* @param string $uid
* @return bool
*/
public function userHasFiles($uid) {
return $this->files->file_exists($uid . '/files');
}
/**
* get owner from give path, path relative to data/ expected
*
* @param string $path relative to data/
* @return string
* @throws \BadMethodCallException
*/
public function getOwner($path) {
$owner = '';
$parts = explode('/', $path, 3);
if (count($parts) > 1) {
$owner = $parts[1];
if ($this->userManager->userExists($owner) === false) {
throw new \BadMethodCallException('Unknown user: '
. 'method expects path to a user folder relative to the data folder');
}
}
return $owner;
}
public function getStorage(string $path): ?IStorage {
return $this->files->getMount($path)->getStorage();
}
}