Skip to content

Commit 9ed8c19

Browse files
committed
OXDEV-9078 Add two FA settings in user account
1 parent 169941a commit 9ed8c19

16 files changed

Lines changed: 231 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Controller;
11+
12+
use OxidEsales\Eshop\Application\Controller\FrontendController;
13+
use OxidEsales\Eshop\Core\Registry;
14+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAUserSettingsServiceInterface;
15+
16+
// todo-critical: remove if we go with Change Password extension
17+
class AccountSecurityController extends FrontendController
18+
{
19+
/**
20+
* @var string
21+
* @SuppressWarnings("PHPMD.CamelCasePropertyName")
22+
*/
23+
protected $_sThisTemplate = '@oe_security_module/templates/account_security';
24+
25+
public function __construct(
26+
private readonly TwoFAUserSettingsServiceInterface $userSettingsService,
27+
) {
28+
parent::__construct();
29+
}
30+
31+
public function saveTwoFactorAuth(): void
32+
{
33+
$user = $this->getUser();
34+
if (!$user) {
35+
return;
36+
}
37+
38+
$enabled = (bool) Registry::getRequest()->getRequestParameter('twofa_enabled');
39+
$this->userSettingsService->setEnabledForUser($user->getId(), $enabled);
40+
}
41+
}

src/Authentication/TwoFactorAuth/Controller/services.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@ services:
99
public: true
1010
tags:
1111
- { name: 'oxid.view_controller', controller_key: 'twofactorauth' }
12+
13+
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Controller\AccountSecurityController:
14+
public: true
15+
tags:
16+
- { name: 'oxid.view_controller', controller_key: 'account_security' }

src/Authentication/TwoFactorAuth/Infrastructure/Repository/UserRepository.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,16 @@ public function getUserById(string $userId): UserInterface
3232

3333
return $this->userDtoFactory->createFromModel($userModel);
3434
}
35+
36+
public function setTwoFAEnabled(string $userId, bool $enabled): void
37+
{
38+
$userModel = $this->userFactory->create();
39+
40+
if (!$userModel->load($userId)) {
41+
throw new UserNotFoundException();
42+
}
43+
44+
$userModel->assign(['oe2faenabled' => (int) $enabled]);
45+
$userModel->save();
46+
}
3547
}

src/Authentication/TwoFactorAuth/Infrastructure/Repository/UserRepositoryInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ interface UserRepositoryInterface
1616
{
1717
/** @throws UserNotFoundException */
1818
public function getUserById(string $userId): UserInterface;
19+
20+
/** @throws UserNotFoundException */
21+
public function setTwoFAEnabled(string $userId, bool $enabled): void;
1922
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service;
11+
12+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Repository\UserRepositoryInterface;
13+
14+
class TwoFAUserSettingsService implements TwoFAUserSettingsServiceInterface
15+
{
16+
public function __construct(
17+
private UserRepositoryInterface $userRepository,
18+
) {
19+
}
20+
21+
public function isEnabledForUser(string $userId): bool
22+
{
23+
return $this->userRepository->getUserById($userId)->isTwoFAEnabled();
24+
}
25+
26+
public function setEnabledForUser(string $userId, bool $enabled): void
27+
{
28+
$this->userRepository->setTwoFAEnabled($userId, $enabled);
29+
}
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service;
11+
12+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Exception\UserNotFoundException;
13+
14+
interface TwoFAUserSettingsServiceInterface
15+
{
16+
/** @throws UserNotFoundException */
17+
public function isEnabledForUser(string $userId): bool;
18+
19+
/** @throws UserNotFoundException */
20+
public function setEnabledForUser(string $userId, bool $enabled): void;
21+
}

src/Authentication/TwoFactorAuth/Service/services.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ services:
88
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAUserServiceInterface:
99
class: OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAUserService
1010
public: true
11+
12+
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAUserSettingsServiceInterface:
13+
class: OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAUserSettingsService
14+
public: true

src/Shared/Core/ViewConfig.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAResendableInterface;
1414
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAServiceInterface;
1515
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAUserServiceInterface;
16+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAUserSettingsServiceInterface;
1617
use OxidEsales\SecurityModule\Captcha\Captcha\Image\Service\ImageCaptchaService;
1718
use OxidEsales\SecurityModule\Captcha\Service\CaptchaServiceInterface;
1819
use OxidEsales\SecurityModule\PasswordPolicy\Service\ModuleSettingsServiceInterface as PasswordSettingsServiceInterface;
@@ -84,6 +85,16 @@ public function getResendCooldownRemaining(): int
8485
return $twoFAService->getCooldownRemaining($userId);
8586
}
8687

88+
public function isTwoFAEnabled(): bool
89+
{
90+
$user = $this->getUser();
91+
if (!$user) {
92+
return false;
93+
}
94+
95+
return $this->getService(TwoFAUserSettingsServiceInterface::class)->isEnabledForUser($user->getId());
96+
}
97+
8798
public function isExternalAuthUser(): bool
8899
{
89100
$user = $this->getUser();

tests/PhpStan/phpstan-bootstrap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,10 @@ class_alias(
2929

3030
class_alias(
3131
\OxidEsales\Eshop\Application\Controller\ForgotPasswordController::class,
32+
\OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Controller\AccountPasswordController_parent::class
33+
);
34+
35+
class_alias(
36+
\OxidEsales\Eshop\Application\Controller\AccountPasswordController::class,
3237
\OxidEsales\SecurityModule\Shared\Controller\ForgotPasswordController_parent::class
3338
);

translations/de/oesecuritymodule_lang.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,10 @@
6262

6363
'OE_SECURITY_EXTERNAL_AUTH_PASSWORD_INFO' => 'Sie sind mit einem externen Anbieter angemeldet. Die Passwortverwaltung ist für dieses Konto nicht verfügbar.',
6464
'OE_SECURITY_RESET_PASSWORD' => 'Passwort zurücksetzen',
65+
66+
'OE_SECURITY_PASSWORD_AND_SECURITY' => 'Passwort & Sicherheit',
67+
'OE_SECURITY_SECURITY_TITLE' => 'Sicherheit',
68+
'OE_SECURITY_TWO_FACTOR_SETTINGS_TITLE' => 'Zwei-Faktor-Authentifizierung',
69+
'OE_SECURITY_TWO_FACTOR_SETTINGS_DESCRIPTION' => 'Fügen Sie Ihrem Konto eine zusätzliche Sicherheitsebene hinzu. Wenn aktiviert, müssen Sie bei jeder Anmeldung einen Bestätigungscode eingeben, der an Ihre E-Mail gesendet wird.',
70+
'OE_SECURITY_TWO_FACTOR_ENABLE' => 'Zwei-Faktor-Authentifizierung aktivieren',
6571
];

0 commit comments

Comments
 (0)