Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 8 additions & 50 deletions apps/encryption/lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,91 +9,49 @@

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');
\OCP\Util::addScript(Application::APP_ID, 'settings_admin');
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;
}
}
16 changes: 0 additions & 16 deletions apps/encryption/lib/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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
Expand Down
58 changes: 25 additions & 33 deletions apps/encryption/tests/Settings/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -81,6 +72,7 @@ public function testGetForm(): void {
'encryptHomeStorage' => true,
'masterKeyEnabled' => true
]);

$expected = new TemplateResponse('encryption', 'settings', renderAs: '');
$this->assertEquals($expected, $this->admin->getForm());
}
Expand Down
32 changes: 3 additions & 29 deletions apps/encryption/tests/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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
Expand Down
8 changes: 0 additions & 8 deletions build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1284,14 +1284,6 @@
<code><![CDATA[is_dir]]></code>
</InternalMethod>
</file>
<file src="apps/encryption/lib/Settings/Admin.php">
<InternalClass>
<code><![CDATA[new View()]]></code>
</InternalClass>
<InternalMethod>
<code><![CDATA[new View()]]></code>
</InternalMethod>
</file>
<file src="apps/encryption/lib/Util.php">
<InternalMethod>
<code><![CDATA[file_exists]]></code>
Expand Down
Loading