Skip to content

Commit 32cf6c8

Browse files
committed
OXDEV-9078 Check if 2FA enabled in the shop to show the block in dashboard
Signed-off-by: Anton Fedurtsya <anton@fedurtsya.com>
1 parent b00f40f commit 32cf6c8

4 files changed

Lines changed: 70 additions & 22 deletions

File tree

src/Authentication/TwoFactorAuth/Service/TwoFAResendableInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface TwoFAResendableInterface
1616
/** @throws ResendCooldownException */
1717
public function resend(string $userId): void;
1818

19+
// todo-medium: looks like this mehtod doesnt belong here? maybe retry interface or something?
1920
public function getRemainingAttempts(string $userId): int;
2021

2122
public function getCooldownRemaining(string $userId): int;

src/Shared/Core/ViewConfig.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAServiceInterface;
1515
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAUserServiceInterface;
1616
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAUserSettingsServiceInterface;
17+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Settings\TwoFAShopSettingsInterface;
1718
use OxidEsales\SecurityModule\Captcha\Captcha\Image\Service\ImageCaptchaService;
1819
use OxidEsales\SecurityModule\Captcha\Service\CaptchaServiceInterface;
1920
use OxidEsales\SecurityModule\PasswordPolicy\Service\ModuleSettingsServiceInterface as PasswordSettingsServiceInterface;
@@ -56,13 +57,7 @@ public function getImage(): string
5657
return 'data:image/jpeg;base64,' . base64_encode($images[ImageCaptchaService::CAPTCHA_NAME]);
5758
}
5859

59-
public function getActiveProviders(): iterable
60-
{
61-
$providers = $this->getService(ProviderCollectorInterface::class)->getProviders();
62-
63-
return array_filter($providers, fn($provider) => $provider->isActive());
64-
}
65-
60+
// todo-critical: move to controller
6661
public function getRemainingAttempts(): int
6762
{
6863
$twoFAService = $this->getService(TwoFAServiceInterface::class);
@@ -74,6 +69,7 @@ public function getRemainingAttempts(): int
7469
return $twoFAService->getRemainingAttempts($userId);
7570
}
7671

72+
// todo-critical: move to controller
7773
public function getResendCooldownRemaining(): int
7874
{
7975
$twoFAService = $this->getService(TwoFAServiceInterface::class);
@@ -85,14 +81,10 @@ public function getResendCooldownRemaining(): int
8581
return $twoFAService->getCooldownRemaining($userId);
8682
}
8783

88-
public function isTwoFAEnabled(): bool
84+
// todo-high: questionable if we want this method here at all, its just for one template - controller instead?
85+
public function isTwoFAEnabledForShop(): bool
8986
{
90-
$user = $this->getUser();
91-
if (!$user) {
92-
return false;
93-
}
94-
95-
return $this->getService(TwoFAUserSettingsServiceInterface::class)->isEnabledForUser($user->getId());
87+
return $this->getService(TwoFAShopSettingsInterface::class)->isTwoFactorAuthEnabled();
9688
}
9789

9890
public function isExternalAuthUser(): bool
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\Shared\Core;
11+
12+
use Generator;
13+
use OxidEsales\EshopCommunity\Core\Di\ContainerFacade;
14+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Settings\TwoFAShopSettingsInterface;
15+
use OxidEsales\SecurityModule\Shared\Core\ViewConfig;
16+
use OxidEsales\SecurityModule\Tests\Integration\IntegrationTestCase;
17+
use PHPUnit\Framework\Attributes\DataProvider;
18+
19+
class ViewConfigTest extends IntegrationTestCase
20+
{
21+
#[DataProvider('isTwoFAEnabledForShopDataProvider')]
22+
public function testIsTwoFAEnabledForShop(bool $shopSettingEnabled): void
23+
{
24+
$shopSettingsStub = $this->createStub(TwoFAShopSettingsInterface::class);
25+
$shopSettingsStub->method('isTwoFactorAuthEnabled')->willReturn($shopSettingEnabled);
26+
27+
$sut = $this->getSut([
28+
TwoFAShopSettingsInterface::class => $shopSettingsStub,
29+
]);
30+
31+
$this->assertSame($shopSettingEnabled, $sut->isTwoFAEnabledForShop());
32+
}
33+
34+
public static function isTwoFAEnabledForShopDataProvider(): Generator
35+
{
36+
yield 'shop setting enabled' => ['shopSettingEnabled' => true];
37+
yield 'shop setting disabled' => ['shopSettingEnabled' => false];
38+
}
39+
40+
private function getSut(array $serviceOverrides = []): ViewConfig
41+
{
42+
/** @var ViewConfig $sut */
43+
$sut = $this->getMockBuilder(ViewConfig::class)
44+
->onlyMethods(['getService'])
45+
->getMock();
46+
$sut->method('getService')->willReturnCallback(
47+
fn(string $id) => $serviceOverrides[$id] ?? ContainerFacade::get($id)
48+
);
49+
50+
return $sut;
51+
}
52+
}

views/twig/extensions/themes/default/page/account/dashboard.html.twig

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
{% block account_dashboard_items %}
44
{{ parent() }}
5-
<div class="col mb-3">
6-
<div class="card card-lg h-100">
7-
<h2 class="h4 card-header card-title">{{ translate({ ident: "OE_SECURITY_SECURITY_TITLE" }) }}</h2>
8-
<div class="card-body">
9-
<a class="btn btn-outline-primary stretched-link" href="{{ seo_url({ ident: oViewConf.getSslSelfLink() ~ "cl=account_security" }) }}">
10-
{{ translate({ ident: "MORE" }) }}
11-
</a>
5+
6+
{% if oViewConf.isTwoFAEnabledForShop() %}
7+
<div class="col mb-3">
8+
<div class="card card-lg h-100">
9+
<h2 class="h4 card-header card-title">{{ translate({ ident: "OE_SECURITY_SECURITY_TITLE" }) }}</h2>
10+
<div class="card-body">
11+
<a class="btn btn-outline-primary stretched-link" href="{{ seo_url({ ident: oViewConf.getSslSelfLink() ~ "cl=account_security" }) }}">
12+
{{ translate({ ident: "MORE" }) }}
13+
</a>
14+
</div>
1215
</div>
1316
</div>
14-
</div>
17+
{% endif %}
1518
{% endblock %}

0 commit comments

Comments
 (0)