Skip to content

Commit a0d309f

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 1e8b4a0 commit a0d309f

3 files changed

Lines changed: 26 additions & 60 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 {

0 commit comments

Comments
 (0)