Skip to content

Commit 9fee741

Browse files
committed
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 b839335 commit 9fee741

7 files changed

Lines changed: 52 additions & 91 deletions

File tree

apps/encryption/lib/Controller/RecoveryController.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
66
* SPDX-License-Identifier: AGPL-3.0-only
77
*/
8+
89
namespace OCA\Encryption\Controller;
910

1011
use OCA\Encryption\Recovery;
@@ -117,37 +118,31 @@ public function changeRecoveryPassword(string $newPassword, string $oldPassword,
117118
}
118119
}
119120

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

129-
if ($result) {
130-
if ($userEnableRecovery === '0') {
131-
return new DataResponse(
132-
[
133-
'data' => [
134-
'message' => $this->l->t('Recovery Key disabled')]
135-
]
136-
);
137-
}
125+
if ($result) {
126+
if (!$userEnableRecovery) {
138127
return new DataResponse(
139128
[
140129
'data' => [
141-
'message' => $this->l->t('Recovery Key enabled')]
130+
'message' => $this->l->t('Recovery Key disabled')]
142131
]
143132
);
144133
}
134+
return new DataResponse(
135+
[
136+
'data' => [
137+
'message' => $this->l->t('Recovery Key enabled')]
138+
]
139+
);
145140
}
141+
146142
return new DataResponse(
147143
[
148144
'data' => [
149-
'message' => $this->l->t('Could not enable the recovery key, please try again or contact your administrator')
150-
]
145+
'message' => $this->l->t('Could not enable the recovery key, please try again or contact your administrator')]
151146
], Http::STATUS_BAD_REQUEST);
152147
}
153148
}

apps/encryption/lib/Recovery.php

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
66
* SPDX-License-Identifier: AGPL-3.0-only
77
*/
8+
89
namespace OCA\Encryption;
910

1011
use OC\Files\View;
@@ -16,19 +17,8 @@
1617
use OCP\PreConditionNotMetException;
1718

1819
class Recovery {
19-
/**
20-
* @var null|IUser
21-
*/
22-
protected $user;
20+
protected ?IUser $user;
2321

24-
/**
25-
* @param IUserSession $userSession
26-
* @param Crypt $crypt
27-
* @param KeyManager $keyManager
28-
* @param IConfig $config
29-
* @param IFile $file
30-
* @param View $view
31-
*/
3222
public function __construct(
3323
IUserSession $userSession,
3424
protected Crypt $crypt,
@@ -40,11 +30,7 @@ public function __construct(
4030
$this->user = ($userSession->isLoggedIn()) ? $userSession->getUser() : null;
4131
}
4232

43-
/**
44-
* @param string $password
45-
* @return bool
46-
*/
47-
public function enableAdminRecovery($password) {
33+
public function enableAdminRecovery(string $password): bool {
4834
$appConfig = $this->config;
4935
$keyManager = $this->keyManager;
5036

@@ -83,11 +69,7 @@ public function changeRecoveryKeyPassword(string $newPassword, string $oldPasswo
8369
return false;
8470
}
8571

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

9375
if ($keyManager->checkRecoveryPassword($recoveryPassword)) {
@@ -102,42 +84,34 @@ public function disableAdminRecovery($recoveryPassword) {
10284
* check if recovery is enabled for user
10385
*
10486
* @param string $user if no user is given we check the current logged-in user
105-
*
106-
* @return bool
10787
*/
108-
public function isRecoveryEnabledForUser($user = '') {
88+
public function isRecoveryEnabledForUser(string $user = ''): bool {
10989
$uid = $user === '' ? $this->user->getUID() : $user;
11090
$recoveryMode = $this->config->getUserValue($uid,
11191
'encryption',
11292
'recoveryEnabled',
113-
0);
93+
'0');
11494

11595
return ($recoveryMode === '1');
11696
}
11797

11898
/**
11999
* check if recovery is key is enabled by the administrator
120-
*
121-
* @return bool
122100
*/
123-
public function isRecoveryKeyEnabled() {
101+
public function isRecoveryKeyEnabled(): bool {
124102
$enabled = $this->config->getAppValue('encryption', 'recoveryAdminEnabled', '0');
125103

126104
return ($enabled === '1');
127105
}
128106

129-
/**
130-
* @param string $value
131-
* @return bool
132-
*/
133-
public function setRecoveryForUser($value) {
107+
public function setRecoveryForUser(bool $value): bool {
134108
try {
135109
$this->config->setUserValue($this->user->getUID(),
136110
'encryption',
137111
'recoveryEnabled',
138-
$value);
112+
$value ? '1' : '0');
139113

140-
if ($value === '1') {
114+
if ($value) {
141115
$this->addRecoveryKeys('/' . $this->user->getUID() . '/files/');
142116
} else {
143117
$this->removeRecoveryKeys('/' . $this->user->getUID() . '/files/');

apps/encryption/lib/Util.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
66
* SPDX-License-Identifier: AGPL-3.0-only
77
*/
8+
89
namespace OCA\Encryption;
910

1011
use OC\Files\Storage\Storage;
@@ -82,11 +83,7 @@ public function isMasterKeyEnabled(): bool {
8283
return ($userMasterKey === '1');
8384
}
8485

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

9289
try {

apps/encryption/tests/Controller/RecoveryControllerTest.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
88
* SPDX-License-Identifier: AGPL-3.0-only
99
*/
10+
1011
namespace OCA\Encryption\Tests\Controller;
1112

1213
use OCA\Encryption\Controller\RecoveryController;
@@ -58,7 +59,6 @@ public function testAdminRecovery($recoveryPassword, $passConfirm, $enableRecove
5859
$passConfirm,
5960
$enableRecovery);
6061

61-
6262
$this->assertEquals($expectedMessage, $response->getData()['data']['message']);
6363
$this->assertEquals($expectedStatus, $response->getStatus());
6464
}
@@ -100,27 +100,21 @@ 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

123-
124118
$response = $this->controller->userSetRecovery($enableRecovery);
125119

126120
$this->assertEquals($expectedMessage, $response->getData()['data']['message']);

apps/encryption/tests/RecoveryTest.php

Lines changed: 2 additions & 4 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 {
@@ -206,7 +206,6 @@ public function testRecoverFile(): void {
206206
->with($this->anything(), $this->anything(), $this->equalTo('admin'))
207207
->willReturn(['admin' => 'publicKey']);
208208

209-
210209
$this->cryptMock->expects($this->once())
211210
->method('multiKeyEncrypt')
212211
->willReturn(['admin' => 'shareKey']);
@@ -259,7 +258,6 @@ protected function setUp(): void {
259258
$this->viewMock);
260259
}
261260

262-
263261
/**
264262
* @param $app
265263
* @param $key

apps/encryption/tests/UtilTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
88
* SPDX-License-Identifier: AGPL-3.0-only
99
*/
10+
1011
namespace OCA\Encryption\Tests;
1112

1213
use OC\Files\View;
@@ -32,7 +33,7 @@ class UtilTest extends TestCase {
3233
protected IMountPoint&MockObject $mountMock;
3334

3435
public function testSetRecoveryForUser(): void {
35-
$this->instance->setRecoveryForUser('1');
36+
$this->instance->setRecoveryForUser(true);
3637
$this->assertArrayHasKey('recoveryEnabled', self::$tempStorage);
3738
}
3839

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)