From e509aa7eff90787b4cf3565586ce3f13c910d03a Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Sat, 21 Sep 2024 13:34:44 +0200 Subject: [PATCH] get module setting from service --- Application/Controller/Admin/d3force_2fa.php | 29 ++++++++++++++++---- Modules/Core/d3_totp_utils.php | 27 ++++++++++++++---- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/Application/Controller/Admin/d3force_2fa.php b/Application/Controller/Admin/d3force_2fa.php index 8bcc12e..584edbd 100644 --- a/Application/Controller/Admin/d3force_2fa.php +++ b/Application/Controller/Admin/d3force_2fa.php @@ -4,13 +4,20 @@ declare(strict_types=1); namespace D3\Totp\Application\Controller\Admin; +use D3\Totp\Application\Model\Constants; use D3\Totp\Application\Model\d3totp_conf; use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Session; +use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\ModuleConfigurationDaoBridgeInterface; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Exception\ModuleSettingNotFountException; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; class d3force_2fa extends d3user_totp { - public function render() + public function render(): string { $this->addTplParam('force2FA', true); @@ -21,7 +28,7 @@ class d3force_2fa extends d3user_totp } - protected function _authorize() + protected function authorize(): bool { $userID = $this->d3TotpGetSessionObject()->getVariable(d3totp_conf::OXID_ADMIN_AUTH); @@ -31,17 +38,27 @@ class d3force_2fa extends d3user_totp /** * @return Session */ - private function d3TotpGetSessionObject() + private function d3TotpGetSessionObject(): Session { return Registry::getSession(); } /** * @return bool + * @throws ModuleSettingNotFountException + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ - private function d3IsAdminForce2FA() + private function d3IsAdminForce2FA(): bool { - return $this->isAdmin() && - Registry::getConfig()->getConfigParam('D3_TOTP_ADMIN_FORCE_2FA') == true; + if (!$this->isAdmin()) { + return false; + } + + $container = ContainerFactory::getInstance()->getContainer(); + $moduleConfigurationBridge = $container->get(ModuleConfigurationDaoBridgeInterface::class); + /** @var ModuleConfiguration $moduleConfiguration */ + $moduleConfiguration = $moduleConfigurationBridge->get(Constants::OXID_MODULE_ID); + return (bool) $moduleConfiguration->getModuleSetting('D3_TOTP_ADMIN_FORCE_2FA')->getValue(); } } diff --git a/Modules/Core/d3_totp_utils.php b/Modules/Core/d3_totp_utils.php index 0232eb6..bc5906c 100644 --- a/Modules/Core/d3_totp_utils.php +++ b/Modules/Core/d3_totp_utils.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace D3\Totp\Modules\Core; +use D3\Totp\Application\Model\Constants; use D3\Totp\Application\Model\d3totp; use D3\Totp\Application\Model\d3totp_conf; use Doctrine\DBAL\DBALException; @@ -22,6 +23,12 @@ use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException; use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Session; +use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\ModuleConfigurationDaoBridgeInterface; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration; +use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Exception\ModuleSettingNotFountException; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; class d3_totp_utils extends d3_totp_utils_parent { @@ -60,7 +67,7 @@ class d3_totp_utils extends d3_totp_utils_parent /** * @return Session */ - public function d3TotpGetSessionObject() + public function d3TotpGetSessionObject(): Session { return Registry::getSession(); } @@ -68,7 +75,7 @@ class d3_totp_utils extends d3_totp_utils_parent /** * @return d3totp */ - public function d3GetTotpObject() + public function d3GetTotpObject(): d3totp { return oxNew(d3totp::class); } @@ -83,11 +90,21 @@ class d3_totp_utils extends d3_totp_utils_parent /** * @return bool + * @throws ModuleSettingNotFountException + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ - protected function d3IsAdminForce2FA() + protected function d3IsAdminForce2FA(): bool { - return $this->isAdmin() && - $this->d3GetConfig()->getConfigParam('D3_TOTP_ADMIN_FORCE_2FA') === true; + if (!$this->isAdmin()) { + return false; + } + + $container = ContainerFactory::getInstance()->getContainer(); + $moduleConfigurationBridge = $container->get(ModuleConfigurationDaoBridgeInterface::class); + /** @var ModuleConfiguration $moduleConfiguration */ + $moduleConfiguration = $moduleConfigurationBridge->get(Constants::OXID_MODULE_ID); + return (bool) $moduleConfiguration->getModuleSetting('D3_TOTP_ADMIN_FORCE_2FA')->getValue(); } /**