diff --git a/apps/encryption/lib/Settings/Admin.php b/apps/encryption/lib/Settings/Admin.php index 045dfa7c69876..28897ec294d82 100644 --- a/apps/encryption/lib/Settings/Admin.php +++ b/apps/encryption/lib/Settings/Admin.php @@ -9,67 +9,35 @@ namespace OCA\Encryption\Settings; -use OC\Files\View; use OCA\Encryption\AppInfo\Application; -use OCA\Encryption\Crypto\Crypt; use OCA\Encryption\Session; use OCA\Encryption\Util; use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Services\IInitialState; -use OCP\Config\IUserConfig; use OCP\IAppConfig; -use OCP\IConfig; -use OCP\IL10N; -use OCP\ISession; -use OCP\IUserManager; -use OCP\IUserSession; use OCP\Settings\ISettings; -use Psr\Log\LoggerInterface; class Admin implements ISettings { public function __construct( - private IL10N $l, - private LoggerInterface $logger, - private IUserSession $userSession, - private IConfig $config, - private IUserManager $userManager, - private ISession $session, + private Session $session, + private Util $util, private IInitialState $initialState, private IAppConfig $appConfig, - private IUserConfig $userConfig, ) { } - /** - * @return TemplateResponse - */ #[\Override] - public function getForm() { - $crypt = new Crypt( - $this->logger, - $this->userSession, - $this->config, - $this->l); - - $util = new Util( - new View(), - $crypt, - $this->userSession, - $this->appConfig, - $this->userConfig, - $this->userManager); - + public function getForm(): TemplateResponse { // Check if an adminRecovery account is enabled for recovering files after lost pwd $recoveryAdminEnabled = $this->appConfig->getValueBool('encryption', 'recoveryAdminEnabled'); - $session = new Session($this->session); - $encryptHomeStorage = $util->shouldEncryptHomeStorage(); + $encryptHomeStorage = $this->util->shouldEncryptHomeStorage(); $this->initialState->provideInitialState('adminSettings', [ 'recoveryEnabled' => $recoveryAdminEnabled, - 'initStatus' => $session->getStatus(), + 'initStatus' => $this->session->getStatus(), 'encryptHomeStorage' => $encryptHomeStorage, - 'masterKeyEnabled' => $util->isMasterKeyEnabled(), + 'masterKeyEnabled' => $this->util->isMasterKeyEnabled(), ]); \OCP\Util::addStyle(Application::APP_ID, 'settings_admin'); @@ -77,23 +45,13 @@ public function getForm() { return new TemplateResponse(Application::APP_ID, 'settings', renderAs: ''); } - /** - * @return string the section ID, e.g. 'sharing' - */ #[\Override] - public function getSection() { + public function getSection(): string { return 'security'; } - /** - * @return int whether the form should be rather on the top or bottom of - * the admin section. The forms are arranged in ascending order of the - * priority values. It is required to return a value between 0 and 100. - * - * E.g.: 70 - */ #[\Override] - public function getPriority() { + public function getPriority(): int { return 11; } } diff --git a/apps/encryption/lib/Util.php b/apps/encryption/lib/Util.php index 8422a2b5fb1e0..d25c1acbcc75c 100644 --- a/apps/encryption/lib/Util.php +++ b/apps/encryption/lib/Util.php @@ -13,23 +13,16 @@ 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; } /** @@ -67,15 +60,6 @@ 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 diff --git a/apps/encryption/tests/Settings/AdminTest.php b/apps/encryption/tests/Settings/AdminTest.php index 1591b8d95c71f..968cafa715788 100644 --- a/apps/encryption/tests/Settings/AdminTest.php +++ b/apps/encryption/tests/Settings/AdminTest.php @@ -9,68 +9,59 @@ namespace OCA\Encryption\Tests\Settings; +use OCA\Encryption\Session; use OCA\Encryption\Settings\Admin; +use OCA\Encryption\Util; use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Services\IInitialState; -use OCP\Config\IUserConfig; use OCP\IAppConfig; -use OCP\IConfig; -use OCP\IL10N; -use OCP\ISession; -use OCP\IUserManager; -use OCP\IUserSession; use PHPUnit\Framework\MockObject\MockObject; -use Psr\Log\LoggerInterface; use Test\TestCase; class AdminTest extends TestCase { - protected Admin $admin; - protected IL10N&MockObject $l; - protected LoggerInterface&MockObject $logger; - protected IUserSession&MockObject $userSession; - protected IConfig&MockObject $config; - protected IUserManager&MockObject $userManager; - protected ISession&MockObject $session; + protected Session&MockObject $session; + protected Util&MockObject $util; protected IInitialState&MockObject $initialState; protected IAppConfig&MockObject $appConfig; - protected IUserConfig&MockObject $userConfig; protected function setUp(): void { parent::setUp(); - $this->l = $this->createMock(IL10N::class); - $this->logger = $this->createMock(LoggerInterface::class); - $this->userSession = $this->createMock(IUserSession::class); - $this->config = $this->createMock(IConfig::class); - $this->userManager = $this->createMock(IUserManager::class); - $this->session = $this->createMock(ISession::class); + $this->session = $this->createMock(Session::class); + $this->util = $this->createMock(Util::class); $this->initialState = $this->createMock(IInitialState::class); $this->appConfig = $this->createMock(IAppConfig::class); - $this->userConfig = $this->createMock(IUserConfig::class); $this->admin = new Admin( - $this->l, - $this->logger, - $this->userSession, - $this->config, - $this->userManager, $this->session, + $this->util, $this->initialState, $this->appConfig, - $this->userConfig, ); } public function testGetForm(): void { $this->appConfig ->method('getValueBool') - ->willReturnMap([ - ['encryption', 'recoveryAdminEnabled', true], - ['encryption', 'encryptHomeStorage', true, true], - ['encryption', 'useMasterKey', true, true], - ]); + ->with('encryption', 'recoveryAdminEnabled') + ->willReturn(true); + + $this->session + ->expects(self::once()) + ->method('getStatus') + ->willReturn('0'); + + $this->util + ->expects(self::once()) + ->method('shouldEncryptHomeStorage') + ->willReturn(true); + + $this->util + ->expects(self::once()) + ->method('isMasterKeyEnabled') + ->willReturn(true); $this->initialState ->expects(self::once()) @@ -81,6 +72,7 @@ public function testGetForm(): void { 'encryptHomeStorage' => true, 'masterKeyEnabled' => true ]); + $expected = new TemplateResponse('encryption', 'settings', renderAs: ''); $this->assertEquals($expected, $this->admin->getForm()); } diff --git a/apps/encryption/tests/UtilTest.php b/apps/encryption/tests/UtilTest.php index 5de953b1d47c4..626eb1eba1659 100644 --- a/apps/encryption/tests/UtilTest.php +++ b/apps/encryption/tests/UtilTest.php @@ -17,9 +17,7 @@ use OCP\Files\Mount\IMountPoint; use OCP\Files\Storage\IStorage; use OCP\IAppConfig; -use OCP\IUser; use OCP\IUserManager; -use OCP\IUserSession; use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; @@ -36,6 +34,7 @@ class UtilTest extends TestCase { protected function setUp(): void { parent::setUp(); + self::$tempStorage = []; $this->mountMock = $this->createMock(IMountPoint::class); $this->filesMock = $this->createMock(View::class); $this->userManagerMock = $this->createMock(IUserManager::class); @@ -45,20 +44,6 @@ protected function setUp(): void { ->disableOriginalConstructor() ->getMock(); - $user = $this->createMock(IUser::class); - $user->expects($this->any()) - ->method('getUID') - ->willReturn('admin'); - - /** @var IUserSession|MockObject $userSessionMock */ - $userSessionMock = $this->createMock(IUserSession::class); - $userSessionMock->expects($this->any()) - ->method('getUser') - ->willReturn($user); - $userSessionMock->expects($this->any()) - ->method('isLoggedIn') - ->willReturn(true); - $this->appConfigMock = $this->createMock(IAppConfig::class); $this->userConfigMock = $this->createMock(IUserConfig::class); @@ -68,22 +53,11 @@ protected function setUp(): void { return self::$tempStorage[$key] ?? $default; }); - $this->userConfigMock->expects($this->any()) - ->method('setValueBool') - ->willReturnCallback(function (string $userId, string $app, string $key, bool $value): bool { - self::$tempStorage[$key] = $value; - return true; - }); - - $this->instance = new Util($this->filesMock, $cryptMock, $userSessionMock, $this->appConfigMock, $this->userConfigMock, $this->userManagerMock); - } - - public function testSetRecoveryForUser(): void { - $this->instance->setRecoveryForUser(true); - $this->assertArrayHasKey('recoveryEnabled', self::$tempStorage); + $this->instance = new Util($this->filesMock, $cryptMock, $this->appConfigMock, $this->userConfigMock, $this->userManagerMock); } public function testIsRecoveryEnabledForUser(): void { + self::$tempStorage['recoveryEnabled'] = true; $this->assertTrue($this->instance->isRecoveryEnabledForUser('admin')); // Assert recovery will return default value if not set diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml index b6da12c8b93f5..8e1b11719a9d6 100644 --- a/build/psalm-baseline.xml +++ b/build/psalm-baseline.xml @@ -1284,14 +1284,6 @@ - - - - - - - -