From d8bb5cf827bb07b6f48c91729d45c1a3daa64158 Mon Sep 17 00:00:00 2001 From: Jean-Yves <7360784+docjyJ@users.noreply.github.com> Date: Tue, 24 Sep 2024 19:43:53 +0200 Subject: [PATCH 1/6] use space for indent Signed-off-by: Jean-Yves <7360784+docjyJ@users.noreply.github.com> --- php/psalm.xml | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/php/psalm.xml b/php/psalm.xml index 59601afad4e..1d05fb822ed 100644 --- a/php/psalm.xml +++ b/php/psalm.xml @@ -1,17 +1,17 @@ - - - - - + + + + + From a0fba85886bab9b010918b99fd0826105a21db3b Mon Sep 17 00:00:00 2001 From: Jean-Yves <7360784+docjyJ@users.noreply.github.com> Date: Tue, 24 Sep 2024 19:51:18 +0200 Subject: [PATCH 2/6] Typesafe ConfigurationController.php Signed-off-by: Jean-Yves <7360784+docjyJ@users.noreply.github.com> --- .../Controller/ConfigurationController.php | 167 ++++++------------ 1 file changed, 54 insertions(+), 113 deletions(-) diff --git a/php/src/Controller/ConfigurationController.php b/php/src/Controller/ConfigurationController.php index 835d7b6294f..1d0d13179d0 100644 --- a/php/src/Controller/ConfigurationController.php +++ b/php/src/Controller/ConfigurationController.php @@ -21,123 +21,64 @@ public function __construct( public function SetConfig(Request $request, Response $response, array $args) : Response { try { - if (isset($request->getParsedBody()['domain'])) { - $domain = $request->getParsedBody()['domain'] ?? ''; - $this->configurationManager->SetDomain($domain); - } - - if (isset($request->getParsedBody()['current-master-password']) || isset($request->getParsedBody()['new-master-password'])) { - $currentMasterPassword = $request->getParsedBody()['current-master-password'] ?? ''; - $newMasterPassword = $request->getParsedBody()['new-master-password'] ?? ''; - $this->configurationManager->ChangeMasterPassword($currentMasterPassword, $newMasterPassword); - } - - if (isset($request->getParsedBody()['borg_backup_host_location'])) { - $location = $request->getParsedBody()['borg_backup_host_location'] ?? ''; - $this->configurationManager->SetBorgBackupHostLocation($location); - } - - if (isset($request->getParsedBody()['borg_restore_host_location']) || isset($request->getParsedBody()['borg_restore_password'])) { - $restoreLocation = $request->getParsedBody()['borg_restore_host_location'] ?? ''; - $borgPassword = $request->getParsedBody()['borg_restore_password'] ?? ''; - $this->configurationManager->SetBorgRestoreHostLocationAndPassword($restoreLocation, $borgPassword); - } - - if (isset($request->getParsedBody()['daily_backup_time'])) { - if (isset($request->getParsedBody()['automatic_updates'])) { - $enableAutomaticUpdates = true; - } else { - $enableAutomaticUpdates = false; - } - if (isset($request->getParsedBody()['success_notification'])) { - $successNotification = true; - } else { - $successNotification = false; - } - $dailyBackupTime = $request->getParsedBody()['daily_backup_time'] ?? ''; - $this->configurationManager->SetDailyBackupTime($dailyBackupTime, $enableAutomaticUpdates, $successNotification); - } - - if (isset($request->getParsedBody()['delete_daily_backup_time'])) { - $this->configurationManager->DeleteDailyBackupTime(); - } - - if (isset($request->getParsedBody()['additional_backup_directories'])) { - $additionalBackupDirectories = $request->getParsedBody()['additional_backup_directories'] ?? ''; - $this->configurationManager->SetAdditionalBackupDirectories($additionalBackupDirectories); - } - - if (isset($request->getParsedBody()['delete_timezone'])) { - $this->configurationManager->DeleteTimezone(); - } - - if (isset($request->getParsedBody()['timezone'])) { - $timezone = $request->getParsedBody()['timezone'] ?? ''; - $this->configurationManager->SetTimezone($timezone); - } - - if (isset($request->getParsedBody()['options-form'])) { - if (isset($request->getParsedBody()['collabora']) && isset($request->getParsedBody()['onlyoffice'])) { - throw new InvalidSettingConfigurationException("Collabora and Onlyoffice are not allowed to be enabled at the same time!"); - } - if (isset($request->getParsedBody()['clamav'])) { - $this->configurationManager->SetClamavEnabledState(1); - } else { - $this->configurationManager->SetClamavEnabledState(0); - } - if (isset($request->getParsedBody()['onlyoffice'])) { - $this->configurationManager->SetOnlyofficeEnabledState(1); - } else { - $this->configurationManager->SetOnlyofficeEnabledState(0); - } - if (isset($request->getParsedBody()['collabora'])) { - $this->configurationManager->SetCollaboraEnabledState(1); - } else { - $this->configurationManager->SetCollaboraEnabledState(0); - } - if (isset($request->getParsedBody()['talk'])) { - $this->configurationManager->SetTalkEnabledState(1); - } else { - $this->configurationManager->SetTalkEnabledState(0); + $body = $request->getParsedBody(); + if (is_array($body)) { + if (is_string($body['domain'])) + $this->configurationManager->SetDomain($body['domain']); + + $currentMasterPassword = is_string($body['current-master-password']) ? $body['current-master-password'] : null; + $newMasterPassword = is_string($body['new-master-password']) ? $body['new-master-password'] : null; + if ($currentMasterPassword !== null || $newMasterPassword !== null) + $this->configurationManager->ChangeMasterPassword($currentMasterPassword ?? '', $newMasterPassword ?? ''); + + if (is_string($body['borg_backup_host_location'])) + $this->configurationManager->SetBorgBackupHostLocation($body['borg_backup_host_location']); + + $borgRestoreHostLocation = is_string($body['borg_restore_host_location']) ? $body['borg_restore_host_location'] : null; + $borgRestorePassword = is_string($body['borg_restore_password']) ? $body['borg_restore_password'] : null; + if ($borgRestoreHostLocation !== null || $borgRestorePassword !== null) + $this->configurationManager->SetBorgRestoreHostLocationAndPassword($borgRestoreHostLocation ?? '', $borgRestorePassword ?? ''); + + if (is_string($body['daily_backup_time'])) + $this->configurationManager->SetDailyBackupTime( + $body['daily_backup_time'], + isset($body['automatic_updates']), + isset($body['success_notification'])); + + if (isset($body['delete_daily_backup_time'])) + $this->configurationManager->DeleteDailyBackupTime(); + + if (is_string($body['additional_backup_directories'])) + $this->configurationManager->SetAdditionalBackupDirectories($body['additional_backup_directories']); + + if (isset($body['delete_timezone'])) + $this->configurationManager->DeleteTimezone(); + + if (is_string($body['timezone'])) + $this->configurationManager->SetTimezone($body['timezone']); + + if (isset($body['options-form'])) { + if (isset($body['collabora']) && isset($body['onlyoffice'])) + throw new InvalidSettingConfigurationException("Collabora and Onlyoffice are not allowed to be enabled at the same time!"); + $this->configurationManager->SetClamavEnabledState(isset($body['clamav']) ? 1 : 0); + $this->configurationManager->SetOnlyofficeEnabledState(isset($body['onlyoffice']) ? 1 : 0); + $this->configurationManager->SetCollaboraEnabledState(isset($body['collabora']) ? 1 : 0); + $this->configurationManager->SetTalkEnabledState(isset($body['talk']) ? 1 : 0); + $this->configurationManager->SetTalkRecordingEnabledState(isset($body['talk-recording']) ? 1 : 0); + $this->configurationManager->SetImaginaryEnabledState(isset($body['imaginary']) ? 1 : 0); + $this->configurationManager->SetFulltextsearchEnabledState(isset($body['fulltextsearch']) ? 1 : 0); + $this->configurationManager->SetDockerSocketProxyEnabledState(isset($body['docker-socket-proxy']) ? 1 : 0); + $this->configurationManager->SetWhiteboardEnabledState(isset($body['whiteboard']) ? 1 : 0); } - if (isset($request->getParsedBody()['talk-recording'])) { - $this->configurationManager->SetTalkRecordingEnabledState(1); - } else { - $this->configurationManager->SetTalkRecordingEnabledState(0); - } - if (isset($request->getParsedBody()['imaginary'])) { - $this->configurationManager->SetImaginaryEnabledState(1); - } else { - $this->configurationManager->SetImaginaryEnabledState(0); - } - if (isset($request->getParsedBody()['fulltextsearch'])) { - $this->configurationManager->SetFulltextsearchEnabledState(1); - } else { - $this->configurationManager->SetFulltextsearchEnabledState(0); - } - if (isset($request->getParsedBody()['docker-socket-proxy'])) { - $this->configurationManager->SetDockerSocketProxyEnabledState(1); - } else { - $this->configurationManager->SetDockerSocketProxyEnabledState(0); - } - if (isset($request->getParsedBody()['whiteboard'])) { - $this->configurationManager->SetWhiteboardEnabledState(1); - } else { - $this->configurationManager->SetWhiteboardEnabledState(0); - } - } - if (isset($request->getParsedBody()['delete_collabora_dictionaries'])) { - $this->configurationManager->DeleteCollaboraDictionaries(); - } + if (isset($body['delete_collabora_dictionaries'])) + $this->configurationManager->DeleteCollaboraDictionaries(); - if (isset($request->getParsedBody()['collabora_dictionaries'])) { - $collaboraDictionaries = $request->getParsedBody()['collabora_dictionaries'] ?? ''; - $this->configurationManager->SetCollaboraDictionaries($collaboraDictionaries); - } + if (is_string($body['collabora_dictionaries'])) + $this->configurationManager->SetCollaboraDictionaries($body['collabora_dictionaries']); - if (isset($request->getParsedBody()['delete_borg_backup_host_location'])) { - $this->configurationManager->DeleteBorgBackupHostLocation(); + if (isset($body['delete_borg_backup_host_location'])) + $this->configurationManager->DeleteBorgBackupHostLocation(); } return $response->withStatus(201)->withHeader('Location', '/'); From 4dcf8be91096d51738a495edda2bb3c4c2530d77 Mon Sep 17 00:00:00 2001 From: Jean-Yves <7360784+docjyJ@users.noreply.github.com> Date: Tue, 24 Sep 2024 20:12:16 +0200 Subject: [PATCH 3/6] Clean code and use const Signed-off-by: Jean-Yves <7360784+docjyJ@users.noreply.github.com> --- php/src/Auth/AuthManager.php | 16 ++++++++-------- php/src/Auth/PasswordGenerator.php | 20 ++++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/php/src/Auth/AuthManager.php b/php/src/Auth/AuthManager.php index a4bc96b6d3c..f3775146400 100644 --- a/php/src/Auth/AuthManager.php +++ b/php/src/Auth/AuthManager.php @@ -4,25 +4,25 @@ use AIO\Data\ConfigurationManager; use AIO\Data\DataConst; -use \DateTime; +use DateTime; class AuthManager { private const string SESSION_KEY = 'aio_authenticated'; - private ConfigurationManager $configurationManager; - public function __construct(ConfigurationManager $configurationManager) { - $this->configurationManager = $configurationManager; + public function __construct( + private readonly ConfigurationManager $configurationManager + ) { } - public function CheckCredentials(string $password) : bool { + public function CheckCredentials(string $password): bool { return hash_equals($this->configurationManager->GetPassword(), $password); } - public function CheckToken(string $token) : bool { + public function CheckToken(string $token): bool { return hash_equals($this->configurationManager->GetToken(), $token); } - public function SetAuthState(bool $isLoggedIn) : void { + public function SetAuthState(bool $isLoggedIn): void { if (!$this->IsAuthenticated() && $isLoggedIn === true) { $date = new DateTime(); @@ -40,7 +40,7 @@ public function SetAuthState(bool $isLoggedIn) : void { $_SESSION[self::SESSION_KEY] = $isLoggedIn; } - public function IsAuthenticated() : bool { + public function IsAuthenticated(): bool { return isset($_SESSION[self::SESSION_KEY]) && $_SESSION[self::SESSION_KEY] === true; } } diff --git a/php/src/Auth/PasswordGenerator.php b/php/src/Auth/PasswordGenerator.php index 6e9ee9a182a..4c3e28194c6 100644 --- a/php/src/Auth/PasswordGenerator.php +++ b/php/src/Auth/PasswordGenerator.php @@ -2,11 +2,11 @@ namespace AIO\Auth; -use AIO\Data\ConfigurationManager; +use Random\RandomException; -class PasswordGenerator -{ - private array $words = [ +class PasswordGenerator { + + private const array WORDS = [ 'abacus', 'abdomen', 'abdominal', @@ -7785,14 +7785,18 @@ class PasswordGenerator 'zoom', ]; - public function GeneratePassword(int $length) : string { + + /** + * @throws RandomException + */ + public function GeneratePassword(int $length): string { $password = ''; - for($i = 0; $i < $length; $i ++) { - if($password !== '') { + for ($i = 0; $i < $length; $i++) { + if ($password !== '') { $password = $password . ' '; } - $password = $password . $this->words[random_int(0, 7775)]; + $password = $password . PasswordGenerator::WORDS[random_int(0, 7775)]; } return $password; From 73cbf84e3aeab09f00972d61b29c8b6493f21236 Mon Sep 17 00:00:00 2001 From: Jean-Yves <7360784+docjyJ@users.noreply.github.com> Date: Tue, 24 Sep 2024 20:52:22 +0200 Subject: [PATCH 4/6] Clean ConfigurationController.php Signed-off-by: Jean-Yves <7360784+docjyJ@users.noreply.github.com> --- php/src/Controller/ConfigurationController.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/php/src/Controller/ConfigurationController.php b/php/src/Controller/ConfigurationController.php index 1d0d13179d0..f56cd3362ee 100644 --- a/php/src/Controller/ConfigurationController.php +++ b/php/src/Controller/ConfigurationController.php @@ -2,24 +2,19 @@ namespace AIO\Controller; -use AIO\ContainerDefinitionFetcher; use AIO\Data\ConfigurationManager; use AIO\Data\InvalidSettingConfigurationException; -use AIO\Docker\DockerActionManager; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; -class ConfigurationController -{ - private ConfigurationManager $configurationManager; +readonly class ConfigurationController { public function __construct( - ConfigurationManager $configurationManager + private ConfigurationManager $configurationManager ) { - $this->configurationManager = $configurationManager; } - public function SetConfig(Request $request, Response $response, array $args) : Response { + public function SetConfig(Request $request, Response $response, array $args): Response { try { $body = $request->getParsedBody(); if (is_array($body)) { From 20857c432c3e370b53cf5e95ea186eda667d04cd Mon Sep 17 00:00:00 2001 From: Jean-Yves <7360784+docjyJ@users.noreply.github.com> Date: Tue, 24 Sep 2024 21:35:06 +0200 Subject: [PATCH 5/6] Refactor Signed-off-by: Jean-Yves <7360784+docjyJ@users.noreply.github.com> --- php/src/Auth/PasswordGenerator.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/php/src/Auth/PasswordGenerator.php b/php/src/Auth/PasswordGenerator.php index 4c3e28194c6..726a58709ee 100644 --- a/php/src/Auth/PasswordGenerator.php +++ b/php/src/Auth/PasswordGenerator.php @@ -7786,9 +7786,7 @@ class PasswordGenerator { ]; - /** - * @throws RandomException - */ + /** @throws RandomException */ public function GeneratePassword(int $length): string { $password = ''; From 25e2dd8468191a0b87c3f0ea8a4e449b3bce20ec Mon Sep 17 00:00:00 2001 From: Jean-Yves <7360784+docjyJ@users.noreply.github.com> Date: Tue, 24 Sep 2024 21:48:10 +0200 Subject: [PATCH 6/6] Auth readonly class Signed-off-by: Jean-Yves <7360784+docjyJ@users.noreply.github.com> --- php/src/Auth/AuthManager.php | 4 ++-- php/src/Auth/PasswordGenerator.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/php/src/Auth/AuthManager.php b/php/src/Auth/AuthManager.php index f3775146400..200942cbbf0 100644 --- a/php/src/Auth/AuthManager.php +++ b/php/src/Auth/AuthManager.php @@ -6,11 +6,11 @@ use AIO\Data\DataConst; use DateTime; -class AuthManager { +readonly class AuthManager { private const string SESSION_KEY = 'aio_authenticated'; public function __construct( - private readonly ConfigurationManager $configurationManager + private ConfigurationManager $configurationManager ) { } diff --git a/php/src/Auth/PasswordGenerator.php b/php/src/Auth/PasswordGenerator.php index 726a58709ee..57df7d43306 100644 --- a/php/src/Auth/PasswordGenerator.php +++ b/php/src/Auth/PasswordGenerator.php @@ -4,7 +4,7 @@ use Random\RandomException; -class PasswordGenerator { +readonly class PasswordGenerator { private const array WORDS = [ 'abacus',