Skip to content

Commit b00f40f

Browse files
committed
OXDEV-9078 Remove isTwoFAEnabled method from controller
Signed-off-by: Anton Fedurtsya <anton@fedurtsya.com>
1 parent 4bc54e9 commit b00f40f

3 files changed

Lines changed: 76 additions & 16 deletions

File tree

src/Authentication/TwoFactorAuth/Controller/AccountSecurityController.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,32 @@
1515

1616
class AccountSecurityController extends AccountController
1717
{
18-
/**
19-
* @var string
20-
* @SuppressWarnings("PHPMD.CamelCasePropertyName")
21-
*/
22-
protected $_sThisTemplate = '@oe_security_module/templates/account_security';
23-
2418
public function __construct(
2519
private readonly TwoFAUserSettingsInterface $userSettingsService,
2620
) {
21+
$this->setTemplateName('@oe_security_module/templates/account_security');
2722
parent::__construct();
2823
}
2924

30-
public function saveTwoFactorAuth(): void
25+
public function render(): string
3126
{
27+
$parentResult = parent::render();
28+
3229
$user = $this->getUser();
33-
if (!$user) {
34-
return;
35-
}
3630

37-
$enabled = (bool) Registry::getRequest()->getRequestParameter('twofa_enabled');
38-
$this->userSettingsService->setEnabledForUser($user->getId(), $enabled);
31+
$this->addTplParam('twoFAEnabledForUser', $this->userSettingsService->isEnabledForUser($user->getId()));
32+
33+
return $parentResult;
3934
}
4035

41-
public function isTwoFAEnabled(): bool
36+
public function saveTwoFactorAuth(): void
4237
{
4338
$user = $this->getUser();
4439
if (!$user) {
45-
return false;
40+
return;
4641
}
4742

48-
return $this->userSettingsService->isEnabledForUser($user->getId());
43+
$enabled = (bool)Registry::getRequest()->getRequestParameter('twofa_enabled');
44+
$this->userSettingsService->setEnabledForUser($user->getId(), $enabled);
4945
}
5046
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\Tests\Integration\Authentication\TwoFactorAuth\Controller;
11+
12+
use Generator;
13+
use OxidEsales\Eshop\Application\Controller\AccountController;
14+
use OxidEsales\Eshop\Application\Model\User;
15+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Controller\AccountSecurityController;
16+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Settings\TwoFAUserSettingsInterface;
17+
use OxidEsales\SecurityModule\Tests\Integration\IntegrationTestCase;
18+
use PHPUnit\Framework\Attributes\DataProvider;
19+
use PHPUnit\Framework\Attributes\Test;
20+
21+
class AccountSecurityControllerTest extends IntegrationTestCase
22+
{
23+
#[Test]
24+
public function extendsAccountController(): void
25+
{
26+
$this->assertInstanceOf(AccountController::class, $this->getSut());
27+
}
28+
29+
#[Test]
30+
#[DataProvider('renderSetsTwoFAEnabledDataProvider')]
31+
public function renderSetsTwoFAEnabled(bool $userSettingEnabled): void
32+
{
33+
$userId = uniqid();
34+
35+
$userStub = $this->createStub(User::class);
36+
$userStub->method('getId')->willReturn($userId);
37+
38+
$userSettingsStub = $this->createStub(TwoFAUserSettingsInterface::class);
39+
$userSettingsStub->method('isEnabledForUser')->with($userId)->willReturn($userSettingEnabled);
40+
41+
$sut = $this->getSut(userSettingsService: $userSettingsStub);
42+
$sut->method('getUser')->willReturn($userStub);
43+
$sut->render();
44+
45+
$this->assertSame($userSettingEnabled, $sut->getViewDataElement('twoFAEnabledForUser'));
46+
}
47+
48+
public static function renderSetsTwoFAEnabledDataProvider(): Generator
49+
{
50+
yield 'user has 2FA enabled' => ['userSettingEnabled' => true];
51+
yield 'user has 2FA disabled' => ['userSettingEnabled' => false];
52+
}
53+
54+
private function getSut(
55+
TwoFAUserSettingsInterface $userSettingsService = null,
56+
): AccountSecurityController {
57+
return $this->getMockBuilder(AccountSecurityController::class)
58+
->setConstructorArgs([
59+
'userSettingsService' => $userSettingsService ?? $this->createStub(TwoFAUserSettingsInterface::class),
60+
])
61+
->onlyMethods(['getUser'])
62+
->getMock();
63+
}
64+
}

views/twig/form/two_factor_auth_settings.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
id="twofa_enabled"
1414
name="twofa_enabled"
1515
value="1"
16-
{% if oViewConf.isTwoFAEnabled() %}checked{% endif %}
16+
{% if twoFAEnabledForUser %}checked{% endif %}
1717
>
1818
<label class="form-check-label" for="twofa_enabled">
1919
{{ translate({ ident: "OE_SECURITY_TWO_FACTOR_ENABLE" }) }}

0 commit comments

Comments
 (0)