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
32 changes: 13 additions & 19 deletions apps/encryption/lib/Controller/RecoveryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,37 +118,31 @@ public function changeRecoveryPassword(string $newPassword, string $oldPassword,
}
}

/**
* @param string $userEnableRecovery
* @return DataResponse
*/
#[NoAdminRequired]
public function userSetRecovery($userEnableRecovery) {
if ($userEnableRecovery === '0' || $userEnableRecovery === '1') {
$result = $this->recovery->setRecoveryForUser($userEnableRecovery);
public function userSetRecovery(bool $userEnableRecovery): DataResponse {
$result = $this->recovery->setRecoveryForUser($userEnableRecovery);

if ($result) {
if ($userEnableRecovery === '0') {
return new DataResponse(
[
'data' => [
'message' => $this->l->t('Recovery Key disabled')]
]
);
}
if ($result) {
if (!$userEnableRecovery) {
return new DataResponse(
[
'data' => [
'message' => $this->l->t('Recovery Key enabled')]
'message' => $this->l->t('Recovery Key disabled')]
]
);
}
return new DataResponse(
[
'data' => [
'message' => $this->l->t('Recovery Key enabled')]
]
);
}

return new DataResponse(
[
'data' => [
'message' => $this->l->t('Could not enable the recovery key, please try again or contact your administrator')
]
'message' => $this->l->t('Could not enable the recovery key, please try again or contact your administrator')]
], Http::STATUS_BAD_REQUEST);
}
}
45 changes: 9 additions & 36 deletions apps/encryption/lib/Recovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,8 @@
use OCP\PreConditionNotMetException;

class Recovery {
/**
* @var null|IUser
*/
protected $user;
protected ?IUser $user;

/**
* @param IUserSession $userSession
* @param Crypt $crypt
* @param KeyManager $keyManager
* @param IConfig $config
* @param IFile $file
* @param View $view
*/
public function __construct(
IUserSession $userSession,
protected Crypt $crypt,
Expand All @@ -41,11 +30,7 @@ public function __construct(
$this->user = ($userSession->isLoggedIn()) ? $userSession->getUser() : null;
}

/**
* @param string $password
* @return bool
*/
public function enableAdminRecovery($password) {
public function enableAdminRecovery(string $password): bool {
$appConfig = $this->config;
$keyManager = $this->keyManager;

Expand Down Expand Up @@ -84,11 +69,7 @@ public function changeRecoveryKeyPassword(string $newPassword, string $oldPasswo
return false;
}

/**
* @param string $recoveryPassword
* @return bool
*/
public function disableAdminRecovery($recoveryPassword) {
public function disableAdminRecovery(string $recoveryPassword): bool {
$keyManager = $this->keyManager;

if ($keyManager->checkRecoveryPassword($recoveryPassword)) {
Expand All @@ -103,42 +84,34 @@ public function disableAdminRecovery($recoveryPassword) {
* check if recovery is enabled for user
*
* @param string $user if no user is given we check the current logged-in user
*
* @return bool
*/
public function isRecoveryEnabledForUser($user = '') {
public function isRecoveryEnabledForUser(string $user = ''): bool {
$uid = $user === '' ? $this->user->getUID() : $user;
$recoveryMode = $this->config->getUserValue($uid,
'encryption',
'recoveryEnabled',
0);
'0');

return ($recoveryMode === '1');
}

/**
* check if recovery is key is enabled by the administrator
*
* @return bool
*/
public function isRecoveryKeyEnabled() {
public function isRecoveryKeyEnabled(): bool {
$enabled = $this->config->getAppValue('encryption', 'recoveryAdminEnabled', '0');

return ($enabled === '1');
}

/**
* @param string $value
* @return bool
*/
public function setRecoveryForUser($value) {
public function setRecoveryForUser(bool $value): bool {
try {
$this->config->setUserValue($this->user->getUID(),
'encryption',
'recoveryEnabled',
$value);
$value ? '1' : '0');

if ($value === '1') {
if ($value) {
$this->addRecoveryKeys('/' . $this->user->getUID() . '/files/');
} else {
$this->removeRecoveryKeys('/' . $this->user->getUID() . '/files/');
Expand Down
6 changes: 1 addition & 5 deletions apps/encryption/lib/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ public function isMasterKeyEnabled(): bool {
return ($userMasterKey === '1');
}

/**
* @param $enabled
* @return bool
*/
public function setRecoveryForUser($enabled) {
public function setRecoveryForUser(bool $enabled): bool {
$value = $enabled ? '1' : '0';

try {
Expand Down
15 changes: 5 additions & 10 deletions apps/encryption/tests/Controller/RecoveryControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,19 @@ public function testChangeRecoveryPassword($password, $confirmPassword, $oldPass

public static function userSetRecoveryProvider(): array {
return [
['1', 'Recovery Key enabled', Http::STATUS_OK],
['0', 'Could not enable the recovery key, please try again or contact your administrator', Http::STATUS_BAD_REQUEST]
[true, 'Recovery Key enabled', Http::STATUS_OK],
[false, 'Could not enable the recovery key, please try again or contact your administrator', Http::STATUS_BAD_REQUEST]
];
}

/**
* @param $enableRecovery
* @param $expectedMessage
* @param $expectedStatus
*/
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'userSetRecoveryProvider')]
public function testUserSetRecovery($enableRecovery, $expectedMessage, $expectedStatus): void {
public function testUserSetRecovery(bool $enableRecovery, string $expectedMessage, int $expectedStatus): void {
$this->recoveryMock->expects($this->any())
->method('setRecoveryForUser')
->with($enableRecovery)
->willReturnMap([
['1', true],
['0', false]
[true, true],
[false, false]
]);

$response = $this->controller->userSetRecovery($enableRecovery);
Expand Down
4 changes: 2 additions & 2 deletions apps/encryption/tests/RecoveryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ public function testSetRecoveryFolderForUser(): void {
$this->viewMock->expects($this->exactly(2))
->method('getDirectoryContent')
->willReturn([]);
$this->assertTrue($this->instance->setRecoveryForUser(0));
$this->assertTrue($this->instance->setRecoveryForUser('1'));
$this->assertTrue($this->instance->setRecoveryForUser(false));
$this->assertTrue($this->instance->setRecoveryForUser(true));
}

public function testRecoverUserFiles(): void {
Expand Down
2 changes: 1 addition & 1 deletion apps/encryption/tests/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class UtilTest extends TestCase {
protected IMountPoint&MockObject $mountMock;

public function testSetRecoveryForUser(): void {
$this->instance->setRecoveryForUser('1');
$this->instance->setRecoveryForUser(true);
$this->assertArrayHasKey('recoveryEnabled', self::$tempStorage);
}

Expand Down
30 changes: 16 additions & 14 deletions apps/settings/lib/Controller/ChangePasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,22 @@ public function changeUserPassword(?string $username = null, ?string $password =
],
]);
}
if (!$result && $recoveryEnabledForUser) {
return new JSONResponse([
'status' => 'error',
'data' => [
'message' => $this->l->t('Backend does not support password change, but the encryption of the account key was updated.'),
]
]);
} elseif (!$result && !$recoveryEnabledForUser) {
return new JSONResponse([
'status' => 'error',
'data' => [
'message' => $this->l->t('Unable to change password'),
]
]);
if (!$result) {
if ($recoveryEnabledForUser) {
return new JSONResponse([
'status' => 'error',
'data' => [
'message' => $this->l->t('Backend does not support password change, but the encryption of the account key was updated.'),
]
]);
} else {
return new JSONResponse([
'status' => 'error',
'data' => [
'message' => $this->l->t('Unable to change password'),
]
]);
}
}
}
} else {
Expand Down
Loading