From aea8aaac1f60090a127db035d75b70eadf65fec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sat, 11 Jun 2022 10:50:26 +0200 Subject: [PATCH] refactor: [WIP] Migrate config account controller. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../SaveController.php} | 90 ++++++++++++------- .../web/Controllers/SimpleControllerBase.php | 8 -- .../web/Controllers/Traits/ConfigTrait.php | 18 ++-- 3 files changed, 61 insertions(+), 55 deletions(-) rename app/modules/web/Controllers/{ConfigAccountController.php => ConfigAccount/SaveController.php} (68%) diff --git a/app/modules/web/Controllers/ConfigAccountController.php b/app/modules/web/Controllers/ConfigAccount/SaveController.php similarity index 68% rename from app/modules/web/Controllers/ConfigAccountController.php rename to app/modules/web/Controllers/ConfigAccount/SaveController.php index 97aa5220..01144e2c 100644 --- a/app/modules/web/Controllers/ConfigAccountController.php +++ b/app/modules/web/Controllers/ConfigAccount/SaveController.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -22,15 +22,15 @@ * along with sysPass. If not, see . */ -namespace SP\Modules\Web\Controllers; +namespace SP\Modules\Web\Controllers\ConfigAccount; -use DI\DependencyException; -use DI\NotFoundException; use SP\Core\Acl\ActionsInterface; use SP\Core\Acl\UnauthorizedPageException; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; -use SP\Http\JsonResponse; +use SP\Core\Exceptions\ValidationException; +use SP\Domain\Config\In\ConfigDataInterface; +use SP\Modules\Web\Controllers\SimpleControllerBase; use SP\Modules\Web\Controllers\Traits\ConfigTrait; /** @@ -38,14 +38,14 @@ use SP\Modules\Web\Controllers\Traits\ConfigTrait; * * @package SP\Modules\Web\Controllers */ -final class ConfigAccountController extends SimpleControllerBase +final class SaveController extends SimpleControllerBase { use ConfigTrait; + private const MAX_FILES_SIZE = 16384; + /** * @return bool - * @throws DependencyException - * @throws NotFoundException * @throws \JsonException */ public function saveAction(): bool @@ -54,7 +54,32 @@ final class ConfigAccountController extends SimpleControllerBase $eventMessage = EventMessage::factory(); - // Accounts + try { + $this->handleAccountsConfig($configData); + $this->handleFilesConfig($configData, $eventMessage); + $this->handlePublicLinksConfig($configData, $eventMessage); + } catch (ValidationException $e) { + $this->eventDispatcher->notifyEvent('exception', new Event($e)); + + $this->returnJsonResponseException($e); + } + + return $this->saveConfig( + $configData, + $this->config, + function () use ($eventMessage) { + $this->eventDispatcher->notifyEvent('save.config.account', new Event($this, $eventMessage)); + } + ); + } + + /** + * @param \SP\Domain\Config\In\ConfigDataInterface $configData + * + * @return void + */ + private function handleAccountsConfig(ConfigDataInterface $configData): void + { $configData->setGlobalSearch($this->request->analyzeBool('account_globalsearch_enabled', false)); $configData->setAccountPassToImage($this->request->analyzeBool('account_passtoimage_enabled', false)); $configData->setAccountLink($this->request->analyzeBool('account_link_enabled', false)); @@ -63,18 +88,24 @@ final class ConfigAccountController extends SimpleControllerBase $configData->setResultsAsCards($this->request->analyzeBool('account_resultsascards_enabled', false)); $configData->setAccountExpireEnabled($this->request->analyzeBool('account_expire_enabled', false)); $configData->setAccountExpireTime($this->request->analyzeInt('account_expire_time', 10368000) * 24 * 3600); + } - // Files + /** + * @param \SP\Domain\Config\In\ConfigDataInterface $configData + * @param \SP\Core\Events\EventMessage $eventMessage + * + * @return void + * @throws \SP\Core\Exceptions\ValidationException + */ + private function handleFilesConfig(ConfigDataInterface $configData, EventMessage $eventMessage): void + { $filesEnabled = $this->request->analyzeBool('files_enabled', false); if ($filesEnabled) { $filesAllowedSize = $this->request->analyzeInt('files_allowed_size', 1024); - if ($filesAllowedSize > 16384) { - return $this->returnJsonResponse( - JsonResponse::JSON_ERROR, - __u('Maximum size per file is 16MB') - ); + if ($filesAllowedSize > self::MAX_FILES_SIZE) { + throw new ValidationException(__u('Maximum size per file is 16MB')); } $configData->setFilesEnabled(true); @@ -89,8 +120,16 @@ final class ConfigAccountController extends SimpleControllerBase $eventMessage->addDescription(__u('Files disabled')); } + } - // Public Links + /** + * @param \SP\Domain\Config\In\ConfigDataInterface $configData + * @param \SP\Core\Events\EventMessage $eventMessage + * + * @return void + */ + private function handlePublicLinksConfig(ConfigDataInterface $configData, EventMessage $eventMessage): void + { $pubLinksEnabled = $this->request->analyzeBool('publiclinks_enabled', false); if ($pubLinksEnabled) { @@ -107,24 +146,10 @@ final class ConfigAccountController extends SimpleControllerBase $eventMessage->addDescription(__u('Public links disabled')); } - - - return $this->saveConfig( - $configData, - $this->config, - function () use ($eventMessage) { - $this->eventDispatcher->notifyEvent( - 'save.config.account', - new Event($this, $eventMessage) - ); - } - ); } /** * @return void - * @throws \DI\DependencyException - * @throws \DI\NotFoundException * @throws \JsonException * @throws \SP\Core\Exceptions\SessionTimeout */ @@ -134,10 +159,7 @@ final class ConfigAccountController extends SimpleControllerBase $this->checks(); $this->checkAccess(ActionsInterface::CONFIG_ACCOUNT); } catch (UnauthorizedPageException $e) { - $this->eventDispatcher->notifyEvent( - 'exception', - new Event($e) - ); + $this->eventDispatcher->notifyEvent('exception', new Event($e)); $this->returnJsonResponseException($e); } diff --git a/app/modules/web/Controllers/SimpleControllerBase.php b/app/modules/web/Controllers/SimpleControllerBase.php index eb6c4239..ef74ef80 100644 --- a/app/modules/web/Controllers/SimpleControllerBase.php +++ b/app/modules/web/Controllers/SimpleControllerBase.php @@ -25,11 +25,9 @@ namespace SP\Modules\Web\Controllers; use Klein\Klein; -use Psr\Container\ContainerInterface; use SP\Core\Acl\Acl; use SP\Core\Acl\UnauthorizedPageException; use SP\Core\Application; -use SP\Core\Bootstrap\BootstrapBase; use SP\Core\Context\ContextInterface; use SP\Core\Events\EventDispatcher; use SP\Core\Exceptions\SessionTimeout; @@ -50,9 +48,6 @@ abstract class SimpleControllerBase { use WebControllerTrait; - // TODO: remove when controllers are ready - protected ContainerInterface $dic; - protected EventDispatcher $eventDispatcher; protected ConfigFileService $config; protected ContextInterface $session; @@ -74,9 +69,6 @@ abstract class SimpleControllerBase RequestInterface $request, PhpExtensionChecker $extensionChecker ) { - // TODO: remove when controllers are ready - $this->dic = BootstrapBase::getContainer(); - $this->controllerName = $this->getControllerName(); $this->config = $application->getConfig(); $this->configData = $this->config->getConfigData(); diff --git a/app/modules/web/Controllers/Traits/ConfigTrait.php b/app/modules/web/Controllers/Traits/ConfigTrait.php index 9c586a75..691b70e6 100644 --- a/app/modules/web/Controllers/Traits/ConfigTrait.php +++ b/app/modules/web/Controllers/Traits/ConfigTrait.php @@ -43,8 +43,6 @@ trait ConfigTrait /** * Guardar la configuración * - * @throws \DI\DependencyException - * @throws \DI\NotFoundException * @throws \JsonException */ protected function saveConfig( @@ -54,16 +52,12 @@ trait ConfigTrait ): bool { try { if ($configData->isDemoEnabled()) { - return $this->returnJsonResponse( - JsonResponse::JSON_WARNING, - __u('Ey, this is a DEMO!!') - ); + return $this->returnJsonResponse(JsonResponse::JSON_WARNING, __u('Ey, this is a DEMO!!')); } $config->saveConfig($configData); - if (BootstrapBase::$LOCK !== false - && $configData->isMaintenance() === false) { + if (BootstrapBase::$LOCK !== false && $configData->isMaintenance() === false) { Util::unlockApp(); } @@ -71,16 +65,14 @@ trait ConfigTrait $onSuccess(); } - return $this->returnJsonResponse( - JsonResponse::JSON_SUCCESS, - __u('Configuration updated') - ); + return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Configuration updated')); } catch (Exception $e) { processException($e); return $this->returnJsonResponse( JsonResponse::JSON_ERROR, - __u('Error while saving the configuration') + __u('Error while saving the configuration'), + [$e] ); } }