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
33 changes: 14 additions & 19 deletions apps/encryption/lib/Controller/RecoveryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace OCA\Encryption\Controller;

use OCA\Encryption\Recovery;
Expand Down Expand Up @@ -117,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);
}
}
46 changes: 10 additions & 36 deletions apps/encryption/lib/Recovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace OCA\Encryption;

use OC\Files\View;
Expand All @@ -16,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 @@ -40,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 @@ -83,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 @@ -102,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
7 changes: 2 additions & 5 deletions apps/encryption/lib/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace OCA\Encryption;

use OC\Files\Storage\Storage;
Expand Down Expand Up @@ -82,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
Loading