Skip to content

Commit 108858d

Browse files
committed
refactor(encryption): migrate to Vue 3 and Typescript and script setup
Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent ab8e4e6 commit 108858d

28 files changed

+664
-430
lines changed

apps/encryption/js/encryption.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

apps/encryption/js/settings-admin.js

Lines changed: 0 additions & 80 deletions
This file was deleted.

apps/encryption/js/settings-personal.js

Lines changed: 0 additions & 64 deletions
This file was deleted.

apps/encryption/lib/Controller/RecoveryController.php

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,23 @@
1212
use OCP\AppFramework\Http;
1313
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1414
use OCP\AppFramework\Http\DataResponse;
15-
use OCP\IConfig;
15+
use OCP\Encryption\Exceptions\GenericEncryptionException;
1616
use OCP\IL10N;
1717
use OCP\IRequest;
18+
use Psr\Log\LoggerInterface;
1819

1920
class RecoveryController extends Controller {
20-
/**
21-
* @param string $AppName
22-
* @param IRequest $request
23-
* @param IConfig $config
24-
* @param IL10N $l
25-
* @param Recovery $recovery
26-
*/
2721
public function __construct(
28-
$appName,
22+
string $appName,
2923
IRequest $request,
30-
private IConfig $config,
3124
private IL10N $l,
3225
private Recovery $recovery,
26+
private LoggerInterface $logger,
3327
) {
3428
parent::__construct($appName, $request);
3529
}
3630

37-
/**
38-
* @param string $recoveryPassword
39-
* @param string $confirmPassword
40-
* @param string $adminEnableRecovery
41-
* @return DataResponse
42-
*/
43-
public function adminRecovery($recoveryPassword, $confirmPassword, $adminEnableRecovery) {
31+
public function adminRecovery(string $recoveryPassword, string $confirmPassword, bool $adminEnableRecovery): DataResponse {
4432
// Check if both passwords are the same
4533
if (empty($recoveryPassword)) {
4634
$errorMessage = $this->l->t('Missing recovery key password');
@@ -60,28 +48,28 @@ public function adminRecovery($recoveryPassword, $confirmPassword, $adminEnableR
6048
Http::STATUS_BAD_REQUEST);
6149
}
6250

63-
if (isset($adminEnableRecovery) && $adminEnableRecovery === '1') {
64-
if ($this->recovery->enableAdminRecovery($recoveryPassword)) {
65-
return new DataResponse(['data' => ['message' => $this->l->t('Recovery key successfully enabled')]]);
51+
try {
52+
if ($adminEnableRecovery) {
53+
if ($this->recovery->enableAdminRecovery($recoveryPassword)) {
54+
return new DataResponse(['data' => ['message' => $this->l->t('Recovery key successfully enabled')]]);
55+
}
56+
return new DataResponse(['data' => ['message' => $this->l->t('Could not enable recovery key. Please check your recovery key password!')]], Http::STATUS_BAD_REQUEST);
57+
} else {
58+
if ($this->recovery->disableAdminRecovery($recoveryPassword)) {
59+
return new DataResponse(['data' => ['message' => $this->l->t('Recovery key successfully disabled')]]);
60+
}
61+
return new DataResponse(['data' => ['message' => $this->l->t('Could not disable recovery key. Please check your recovery key password!')]], Http::STATUS_BAD_REQUEST);
6662
}
67-
return new DataResponse(['data' => ['message' => $this->l->t('Could not enable recovery key. Please check your recovery key password!')]], Http::STATUS_BAD_REQUEST);
68-
} elseif (isset($adminEnableRecovery) && $adminEnableRecovery === '0') {
69-
if ($this->recovery->disableAdminRecovery($recoveryPassword)) {
70-
return new DataResponse(['data' => ['message' => $this->l->t('Recovery key successfully disabled')]]);
63+
} catch (\Exception $e) {
64+
$this->logger->error('Error enabling or disabling recovery key', ['exception' => $e]);
65+
if ($e instanceof GenericEncryptionException) {
66+
return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_INTERNAL_SERVER_ERROR);
7167
}
72-
return new DataResponse(['data' => ['message' => $this->l->t('Could not disable recovery key. Please check your recovery key password!')]], Http::STATUS_BAD_REQUEST);
68+
return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
7369
}
74-
// this response should never be sent but just in case.
75-
return new DataResponse(['data' => ['message' => $this->l->t('Missing parameters')]], Http::STATUS_BAD_REQUEST);
7670
}
7771

78-
/**
79-
* @param string $newPassword
80-
* @param string $oldPassword
81-
* @param string $confirmPassword
82-
* @return DataResponse
83-
*/
84-
public function changeRecoveryPassword($newPassword, $oldPassword, $confirmPassword) {
72+
public function changeRecoveryPassword(string $newPassword, string $oldPassword, string $confirmPassword): DataResponse {
8573
//check if both passwords are the same
8674
if (empty($oldPassword)) {
8775
$errorMessage = $this->l->t('Please provide the old recovery password');
@@ -103,23 +91,30 @@ public function changeRecoveryPassword($newPassword, $oldPassword, $confirmPassw
10391
return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_BAD_REQUEST);
10492
}
10593

106-
$result = $this->recovery->changeRecoveryKeyPassword($newPassword,
107-
$oldPassword);
94+
try {
95+
$result = $this->recovery->changeRecoveryKeyPassword($newPassword,
96+
$oldPassword);
10897

109-
if ($result) {
110-
return new DataResponse(
111-
[
112-
'data' => [
113-
'message' => $this->l->t('Password successfully changed.')]
114-
]
115-
);
116-
}
117-
return new DataResponse(
118-
[
98+
if ($result) {
99+
return new DataResponse(
100+
[
101+
'data' => [
102+
'message' => $this->l->t('Password successfully changed.')]
103+
]
104+
);
105+
}
106+
return new DataResponse([
119107
'data' => [
120108
'message' => $this->l->t('Could not change the password. Maybe the old password was not correct.')
121109
]
122110
], Http::STATUS_BAD_REQUEST);
111+
} catch (\Exception $e) {
112+
$this->logger->error('Error changing recovery password', ['exception' => $e]);
113+
if ($e instanceof GenericEncryptionException) {
114+
return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_INTERNAL_SERVER_ERROR);
115+
}
116+
return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
117+
}
123118
}
124119

125120
/**

apps/encryption/lib/Controller/StatusController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ public function getStatus() {
6868
return new DataResponse(
6969
[
7070
'status' => $status,
71+
'initStatus' => $this->session->getStatus(),
7172
'data' => [
72-
'message' => $message]
73+
'message' => $message,
74+
],
7375
]
7476
);
7577
}

apps/encryption/lib/Settings/Admin.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
namespace OCA\Encryption\Settings;
88

99
use OC\Files\View;
10+
use OCA\Encryption\AppInfo\Application;
1011
use OCA\Encryption\Crypto\Crypt;
1112
use OCA\Encryption\Session;
1213
use OCA\Encryption\Util;
1314
use OCP\AppFramework\Http\TemplateResponse;
15+
use OCP\AppFramework\Services\IInitialState;
16+
use OCP\IAppConfig;
1417
use OCP\IConfig;
1518
use OCP\IL10N;
1619
use OCP\ISession;
@@ -27,6 +30,8 @@ public function __construct(
2730
private IConfig $config,
2831
private IUserManager $userManager,
2932
private ISession $session,
33+
private IInitialState $initialState,
34+
private IAppConfig $appConfig,
3035
) {
3136
}
3237

@@ -48,19 +53,21 @@ public function getForm() {
4853
$this->userManager);
4954

5055
// Check if an adminRecovery account is enabled for recovering files after lost pwd
51-
$recoveryAdminEnabled = $this->config->getAppValue('encryption', 'recoveryAdminEnabled', '0');
56+
$recoveryAdminEnabled = $this->appConfig->getValueBool('encryption', 'recoveryAdminEnabled');
5257
$session = new Session($this->session);
5358

5459
$encryptHomeStorage = $util->shouldEncryptHomeStorage();
5560

56-
$parameters = [
61+
$this->initialState->provideInitialState('adminSettings', [
5762
'recoveryEnabled' => $recoveryAdminEnabled,
5863
'initStatus' => $session->getStatus(),
5964
'encryptHomeStorage' => $encryptHomeStorage,
6065
'masterKeyEnabled' => $util->isMasterKeyEnabled(),
61-
];
66+
]);
6267

63-
return new TemplateResponse('encryption', 'settings-admin', $parameters, '');
68+
\OCP\Util::addStyle(Application::APP_ID, 'settings_admin');
69+
\OCP\Util::addScript(Application::APP_ID, 'settings_admin');
70+
return new TemplateResponse(Application::APP_ID, 'settings', renderAs: '');
6471
}
6572

6673
/**

0 commit comments

Comments
 (0)