Skip to content

Commit 2188c0a

Browse files
come-ncAndyScherzinger
authored andcommitted
fix(encryption): Fix endpoint /ajax/userSetRecovery to support boolean
Frontend is sending a boolean but backend was still expecting a string. Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent 17622d0 commit 2188c0a

7 files changed

Lines changed: 47 additions & 87 deletions

File tree

apps/encryption/lib/Controller/RecoveryController.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,37 +118,31 @@ public function changeRecoveryPassword(string $newPassword, string $oldPassword,
118118
}
119119
}
120120

121-
/**
122-
* @param string $userEnableRecovery
123-
* @return DataResponse
124-
*/
125121
#[NoAdminRequired]
126-
public function userSetRecovery($userEnableRecovery) {
127-
if ($userEnableRecovery === '0' || $userEnableRecovery === '1') {
128-
$result = $this->recovery->setRecoveryForUser($userEnableRecovery);
122+
public function userSetRecovery(bool $userEnableRecovery): DataResponse {
123+
$result = $this->recovery->setRecoveryForUser($userEnableRecovery);
129124

130-
if ($result) {
131-
if ($userEnableRecovery === '0') {
132-
return new DataResponse(
133-
[
134-
'data' => [
135-
'message' => $this->l->t('Recovery Key disabled')]
136-
]
137-
);
138-
}
125+
if ($result) {
126+
if (!$userEnableRecovery) {
139127
return new DataResponse(
140128
[
141129
'data' => [
142-
'message' => $this->l->t('Recovery Key enabled')]
130+
'message' => $this->l->t('Recovery Key disabled')]
143131
]
144132
);
145133
}
134+
return new DataResponse(
135+
[
136+
'data' => [
137+
'message' => $this->l->t('Recovery Key enabled')]
138+
]
139+
);
146140
}
141+
147142
return new DataResponse(
148143
[
149144
'data' => [
150-
'message' => $this->l->t('Could not enable the recovery key, please try again or contact your administrator')
151-
]
145+
'message' => $this->l->t('Could not enable the recovery key, please try again or contact your administrator')]
152146
], Http::STATUS_BAD_REQUEST);
153147
}
154148
}

apps/encryption/lib/Recovery.php

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,8 @@
1717
use OCP\PreConditionNotMetException;
1818

1919
class Recovery {
20-
/**
21-
* @var null|IUser
22-
*/
23-
protected $user;
20+
protected ?IUser $user;
2421

25-
/**
26-
* @param IUserSession $userSession
27-
* @param Crypt $crypt
28-
* @param KeyManager $keyManager
29-
* @param IConfig $config
30-
* @param IFile $file
31-
* @param View $view
32-
*/
3322
public function __construct(
3423
IUserSession $userSession,
3524
protected Crypt $crypt,
@@ -41,11 +30,7 @@ public function __construct(
4130
$this->user = ($userSession->isLoggedIn()) ? $userSession->getUser() : null;
4231
}
4332

44-
/**
45-
* @param string $password
46-
* @return bool
47-
*/
48-
public function enableAdminRecovery($password) {
33+
public function enableAdminRecovery(string $password): bool {
4934
$appConfig = $this->config;
5035
$keyManager = $this->keyManager;
5136

@@ -84,11 +69,7 @@ public function changeRecoveryKeyPassword(string $newPassword, string $oldPasswo
8469
return false;
8570
}
8671

87-
/**
88-
* @param string $recoveryPassword
89-
* @return bool
90-
*/
91-
public function disableAdminRecovery($recoveryPassword) {
72+
public function disableAdminRecovery(string $recoveryPassword): bool {
9273
$keyManager = $this->keyManager;
9374

9475
if ($keyManager->checkRecoveryPassword($recoveryPassword)) {
@@ -103,42 +84,34 @@ public function disableAdminRecovery($recoveryPassword) {
10384
* check if recovery is enabled for user
10485
*
10586
* @param string $user if no user is given we check the current logged-in user
106-
*
107-
* @return bool
10887
*/
109-
public function isRecoveryEnabledForUser($user = '') {
88+
public function isRecoveryEnabledForUser(string $user = ''): bool {
11089
$uid = $user === '' ? $this->user->getUID() : $user;
11190
$recoveryMode = $this->config->getUserValue($uid,
11291
'encryption',
11392
'recoveryEnabled',
114-
0);
93+
'0');
11594

11695
return ($recoveryMode === '1');
11796
}
11897

11998
/**
12099
* check if recovery is key is enabled by the administrator
121-
*
122-
* @return bool
123100
*/
124-
public function isRecoveryKeyEnabled() {
101+
public function isRecoveryKeyEnabled(): bool {
125102
$enabled = $this->config->getAppValue('encryption', 'recoveryAdminEnabled', '0');
126103

127104
return ($enabled === '1');
128105
}
129106

130-
/**
131-
* @param string $value
132-
* @return bool
133-
*/
134-
public function setRecoveryForUser($value) {
107+
public function setRecoveryForUser(bool $value): bool {
135108
try {
136109
$this->config->setUserValue($this->user->getUID(),
137110
'encryption',
138111
'recoveryEnabled',
139-
$value);
112+
$value ? '1' : '0');
140113

141-
if ($value === '1') {
114+
if ($value) {
142115
$this->addRecoveryKeys('/' . $this->user->getUID() . '/files/');
143116
} else {
144117
$this->removeRecoveryKeys('/' . $this->user->getUID() . '/files/');

apps/encryption/lib/Util.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,7 @@ public function isMasterKeyEnabled(): bool {
8383
return ($userMasterKey === '1');
8484
}
8585

86-
/**
87-
* @param $enabled
88-
* @return bool
89-
*/
90-
public function setRecoveryForUser($enabled) {
86+
public function setRecoveryForUser(bool $enabled): bool {
9187
$value = $enabled ? '1' : '0';
9288

9389
try {

apps/encryption/tests/Controller/RecoveryControllerTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,19 @@ public function testChangeRecoveryPassword($password, $confirmPassword, $oldPass
100100

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

108-
/**
109-
* @param $enableRecovery
110-
* @param $expectedMessage
111-
* @param $expectedStatus
112-
*/
113108
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'userSetRecoveryProvider')]
114-
public function testUserSetRecovery($enableRecovery, $expectedMessage, $expectedStatus): void {
109+
public function testUserSetRecovery(bool $enableRecovery, string $expectedMessage, int $expectedStatus): void {
115110
$this->recoveryMock->expects($this->any())
116111
->method('setRecoveryForUser')
117112
->with($enableRecovery)
118113
->willReturnMap([
119-
['1', true],
120-
['0', false]
114+
[true, true],
115+
[false, false]
121116
]);
122117

123118
$response = $this->controller->userSetRecovery($enableRecovery);

apps/encryption/tests/RecoveryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ public function testSetRecoveryFolderForUser(): void {
164164
$this->viewMock->expects($this->exactly(2))
165165
->method('getDirectoryContent')
166166
->willReturn([]);
167-
$this->assertTrue($this->instance->setRecoveryForUser(0));
168-
$this->assertTrue($this->instance->setRecoveryForUser('1'));
167+
$this->assertTrue($this->instance->setRecoveryForUser(false));
168+
$this->assertTrue($this->instance->setRecoveryForUser(true));
169169
}
170170

171171
public function testRecoverUserFiles(): void {

apps/encryption/tests/UtilTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class UtilTest extends TestCase {
3333
protected IMountPoint&MockObject $mountMock;
3434

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

apps/settings/lib/Controller/ChangePasswordController.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,22 @@ public function changeUserPassword(?string $username = null, ?string $password =
173173
],
174174
]);
175175
}
176-
if (!$result && $recoveryEnabledForUser) {
177-
return new JSONResponse([
178-
'status' => 'error',
179-
'data' => [
180-
'message' => $this->l->t('Backend does not support password change, but the encryption of the account key was updated.'),
181-
]
182-
]);
183-
} elseif (!$result && !$recoveryEnabledForUser) {
184-
return new JSONResponse([
185-
'status' => 'error',
186-
'data' => [
187-
'message' => $this->l->t('Unable to change password'),
188-
]
189-
]);
176+
if (!$result) {
177+
if ($recoveryEnabledForUser) {
178+
return new JSONResponse([
179+
'status' => 'error',
180+
'data' => [
181+
'message' => $this->l->t('Backend does not support password change, but the encryption of the account key was updated.'),
182+
]
183+
]);
184+
} else {
185+
return new JSONResponse([
186+
'status' => 'error',
187+
'data' => [
188+
'message' => $this->l->t('Unable to change password'),
189+
]
190+
]);
191+
}
190192
}
191193
}
192194
} else {

0 commit comments

Comments
 (0)