Skip to content

Commit 104a0c4

Browse files
authored
Merge pull request #61243 from nextcloud/backport/60801/stable34
[stable34] fix(encryption): Fix endpoint /ajax/userSetRecovery to support boolean
2 parents 9d6932f + 2723eaf commit 104a0c4

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
@@ -117,37 +117,31 @@ public function changeRecoveryPassword(string $newPassword, string $oldPassword,
117117
}
118118
}
119119

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

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-
}
124+
if ($result) {
125+
if (!$userEnableRecovery) {
138126
return new DataResponse(
139127
[
140128
'data' => [
141-
'message' => $this->l->t('Recovery Key enabled')]
129+
'message' => $this->l->t('Recovery Key disabled')]
142130
]
143131
);
144132
}
133+
return new DataResponse(
134+
[
135+
'data' => [
136+
'message' => $this->l->t('Recovery Key enabled')]
137+
]
138+
);
145139
}
140+
146141
return new DataResponse(
147142
[
148143
'data' => [
149-
'message' => $this->l->t('Could not enable the recovery key, please try again or contact your administrator')
150-
]
144+
'message' => $this->l->t('Could not enable the recovery key, please try again or contact your administrator')]
151145
], Http::STATUS_BAD_REQUEST);
152146
}
153147
}

apps/encryption/lib/Recovery.php

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

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

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-
*/
3221
public function __construct(
3322
IUserSession $userSession,
3423
protected Crypt $crypt,
@@ -40,11 +29,7 @@ public function __construct(
4029
$this->user = ($userSession->isLoggedIn()) ? $userSession->getUser() : null;
4130
}
4231

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

@@ -83,11 +68,7 @@ public function changeRecoveryKeyPassword(string $newPassword, string $oldPasswo
8368
return false;
8469
}
8570

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

9374
if ($keyManager->checkRecoveryPassword($recoveryPassword)) {
@@ -102,42 +83,34 @@ public function disableAdminRecovery($recoveryPassword) {
10283
* check if recovery is enabled for user
10384
*
10485
* @param string $user if no user is given we check the current logged-in user
105-
*
106-
* @return bool
10786
*/
108-
public function isRecoveryEnabledForUser($user = '') {
87+
public function isRecoveryEnabledForUser(string $user = ''): bool {
10988
$uid = $user === '' ? $this->user->getUID() : $user;
11089
$recoveryMode = $this->config->getUserValue($uid,
11190
'encryption',
11291
'recoveryEnabled',
113-
0);
92+
'0');
11493

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

11897
/**
11998
* check if recovery is key is enabled by the administrator
120-
*
121-
* @return bool
12299
*/
123-
public function isRecoveryKeyEnabled() {
100+
public function isRecoveryKeyEnabled(): bool {
124101
$enabled = $this->config->getAppValue('encryption', 'recoveryAdminEnabled', '0');
125102

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

129-
/**
130-
* @param string $value
131-
* @return bool
132-
*/
133-
public function setRecoveryForUser($value) {
106+
public function setRecoveryForUser(bool $value): bool {
134107
try {
135108
$this->config->setUserValue($this->user->getUID(),
136109
'encryption',
137110
'recoveryEnabled',
138-
$value);
111+
$value ? '1' : '0');
139112

140-
if ($value === '1') {
113+
if ($value) {
141114
$this->addRecoveryKeys('/' . $this->user->getUID() . '/files/');
142115
} else {
143116
$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
@@ -82,11 +82,7 @@ public function isMasterKeyEnabled(): bool {
8282
return ($userMasterKey === '1');
8383
}
8484

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

9288
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

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
@@ -32,7 +32,7 @@ class UtilTest extends TestCase {
3232
protected IMountPoint&MockObject $mountMock;
3333

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

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)