Skip to content

Commit b7948f7

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

17 files changed

Lines changed: 274 additions & 0 deletions

metadata.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
'extend' => [
3636
\OxidEsales\Eshop\Application\Controller\NewsletterController::class => \OxidEsales\SecurityModule\Captcha\Shop\NewsletterController::class,
3737
\OxidEsales\Eshop\Application\Controller\ForgotPasswordController::class => \OxidEsales\SecurityModule\Shared\Controller\ForgotPasswordController::class,
38+
\OxidEsales\Eshop\Application\Controller\AccountPasswordController::class => \OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Controller\AccountPasswordController::class,
3839
\OxidEsales\Eshop\Application\Model\User::class => \OxidEsales\SecurityModule\Shared\Model\User::class,
3940
\OxidEsales\Eshop\Core\InputValidator::class => \OxidEsales\SecurityModule\Shared\Core\InputValidator::class,
4041
\OxidEsales\Eshop\Core\ViewConfig::class => \OxidEsales\SecurityModule\Shared\Core\ViewConfig::class
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\Core\Registry;
13+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAUserSettingsServiceInterface;
14+
15+
/**
16+
* todo-critical: remove it if we go with new Security section
17+
* @mixin \OxidEsales\Eshop\Application\Controller\AccountPasswordController
18+
* @eshopExtension
19+
*/
20+
class AccountPasswordController extends AccountPasswordController_parent
21+
{
22+
public function saveTwoFactorAuth(): void
23+
{
24+
$user = $this->getUser();
25+
if (!$user) {
26+
return;
27+
}
28+
29+
$enabled = (bool) Registry::getRequest()->getRequestParameter('twofa_enabled');
30+
$this->getService(TwoFAUserSettingsServiceInterface::class)->setEnabledForUser($user->getId(), $enabled);
31+
}
32+
33+
public function isTwoFAEnabled(): bool
34+
{
35+
$user = $this->getUser();
36+
if (!$user) {
37+
return false;
38+
}
39+
40+
return $this->getService(TwoFAUserSettingsServiceInterface::class)->isEnabledForUser($user->getId());
41+
}
42+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
42+
public function isTwoFAEnabled(): bool
43+
{
44+
$user = $this->getUser();
45+
if (!$user) {
46+
return false;
47+
}
48+
49+
return $this->userSettingsService->isEnabledForUser($user->getId());
50+
}
51+
}

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

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
);

0 commit comments

Comments
 (0)