Skip to content

Commit b081150

Browse files
committed
OXDEV-10012 Exclude logout from stred urls
1 parent 232dc03 commit b081150

4 files changed

Lines changed: 114 additions & 5 deletions

File tree

src/Authentication/TwoFactorAuth/Subscriber/StoreCurrentUrlSubscriber.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class StoreCurrentUrlSubscriber implements EventSubscriberInterface
2020
'twofactorauth',
2121
];
2222

23+
private const EXCLUDED_FUNCTIONS = [
24+
'logout',
25+
];
26+
2327
public static function getSubscribedEvents(): array
2428
{
2529
return [
@@ -34,9 +38,14 @@ public function onViewRendered(ViewRenderedEvent $event): void
3438
}
3539

3640
$currentController = $this->getCurrentController();
37-
38-
// Skip widgets (they start with 'oxw') and excluded controllers
39-
if ($this->isWidget($currentController) || $this->shouldExcludeController($currentController)) {
41+
$currentFunction = $this->getCurrentFunction();
42+
43+
// Skip widgets, excluded controllers, and excluded functions (like logout)
44+
if (
45+
$this->isWidget($currentController)
46+
|| $this->shouldExcludeController($currentController)
47+
|| $this->shouldExcludeFunction($currentFunction)
48+
) {
4049
return;
4150
}
4251

@@ -71,6 +80,16 @@ private function shouldExcludeController(string $controller): bool
7180
return in_array($controller, self::EXCLUDED_CONTROLLERS, true);
7281
}
7382

83+
private function getCurrentFunction(): string
84+
{
85+
return strtolower((string) Registry::getRequest()->getRequestParameter('fnc'));
86+
}
87+
88+
private function shouldExcludeFunction(string $function): bool
89+
{
90+
return in_array($function, self::EXCLUDED_FUNCTIONS, true);
91+
}
92+
7493
private function getCurrentPageUrl(): ?string
7594
{
7695
$activeView = Registry::getConfig()->getTopActiveView();

tests/Unit/Authentication/TwoFactorAuth/Subscriber/StoreCurrentUrlSubscriberTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use OxidEsales\Eshop\Core\Config;
1313
use OxidEsales\Eshop\Core\Registry;
14+
use OxidEsales\Eshop\Core\Request;
1415
use OxidEsales\Eshop\Core\Session;
1516
use OxidEsales\EshopCommunity\Application\Controller\FrontendController;
1617
use OxidEsales\EshopCommunity\Internal\Transition\ShopEvents\ViewRenderedEvent;
@@ -41,6 +42,10 @@ public function testOnViewRenderedStoresCurrentUrl(): void
4142
$configStub->method('getTopActiveView')->willReturn($viewStub);
4243
Registry::set(Config::class, $configStub);
4344

45+
$requestStub = $this->createStub(Request::class);
46+
$requestStub->method('getRequestParameter')->with('fnc')->willReturn(null);
47+
Registry::set(Request::class, $requestStub);
48+
4449
$sessionMock = $this->createMock(Session::class);
4550
$sessionMock->expects($this->once())
4651
->method('setVariable')
@@ -99,6 +104,10 @@ public function testOnViewRenderedSkipsTwoFactorAuthController(): void
99104
$configStub->method('getTopActiveView')->willReturn($viewStub);
100105
Registry::set(Config::class, $configStub);
101106

107+
$requestStub = $this->createStub(Request::class);
108+
$requestStub->method('getRequestParameter')->with('fnc')->willReturn(null);
109+
Registry::set(Request::class, $requestStub);
110+
102111
$sessionMock = $this->createMock(Session::class);
103112
$sessionMock->expects($this->never())->method('setVariable');
104113
Registry::set(Session::class, $sessionMock);
@@ -137,6 +146,58 @@ public function testOnViewRenderedSkipsWhenGetLinkThrowsException(): void
137146
$configStub->method('getTopActiveView')->willReturn($viewStub);
138147
Registry::set(Config::class, $configStub);
139148

149+
$requestStub = $this->createStub(Request::class);
150+
$requestStub->method('getRequestParameter')->with('fnc')->willReturn(null);
151+
Registry::set(Request::class, $requestStub);
152+
153+
$sessionMock = $this->createMock(Session::class);
154+
$sessionMock->expects($this->never())->method('setVariable');
155+
Registry::set(Session::class, $sessionMock);
156+
157+
$sut = new StoreCurrentUrlSubscriber();
158+
$eventStub = $this->createStub(ViewRenderedEvent::class);
159+
160+
$sut->onViewRendered($eventStub);
161+
}
162+
163+
public function testOnViewRenderedSkipsLogoutFunction(): void
164+
{
165+
$viewStub = $this->createStub(FrontendController::class);
166+
$viewStub->method('getClassKey')->willReturn('start');
167+
168+
$configStub = $this->createStub(Config::class);
169+
$configStub->method('isAdmin')->willReturn(false);
170+
$configStub->method('getTopActiveView')->willReturn($viewStub);
171+
Registry::set(Config::class, $configStub);
172+
173+
$requestStub = $this->createStub(Request::class);
174+
$requestStub->method('getRequestParameter')->with('fnc')->willReturn('logout');
175+
Registry::set(Request::class, $requestStub);
176+
177+
$sessionMock = $this->createMock(Session::class);
178+
$sessionMock->expects($this->never())->method('setVariable');
179+
Registry::set(Session::class, $sessionMock);
180+
181+
$sut = new StoreCurrentUrlSubscriber();
182+
$eventStub = $this->createStub(ViewRenderedEvent::class);
183+
184+
$sut->onViewRendered($eventStub);
185+
}
186+
187+
public function testOnViewRenderedSkipsLogoutFunctionCaseInsensitive(): void
188+
{
189+
$viewStub = $this->createStub(FrontendController::class);
190+
$viewStub->method('getClassKey')->willReturn('start');
191+
192+
$configStub = $this->createStub(Config::class);
193+
$configStub->method('isAdmin')->willReturn(false);
194+
$configStub->method('getTopActiveView')->willReturn($viewStub);
195+
Registry::set(Config::class, $configStub);
196+
197+
$requestStub = $this->createStub(Request::class);
198+
$requestStub->method('getRequestParameter')->with('fnc')->willReturn('Logout');
199+
Registry::set(Request::class, $requestStub);
200+
140201
$sessionMock = $this->createMock(Session::class);
141202
$sessionMock->expects($this->never())->method('setVariable');
142203
Registry::set(Session::class, $sessionMock);
@@ -146,4 +207,33 @@ public function testOnViewRenderedSkipsWhenGetLinkThrowsException(): void
146207

147208
$sut->onViewRendered($eventStub);
148209
}
210+
211+
public function testOnViewRenderedStoresUrlWithOtherFunction(): void
212+
{
213+
$currentUrl = uniqid();
214+
215+
$viewStub = $this->createStub(FrontendController::class);
216+
$viewStub->method('getClassKey')->willReturn('details');
217+
$viewStub->method('getLink')->willReturn($currentUrl);
218+
219+
$configStub = $this->createStub(Config::class);
220+
$configStub->method('isAdmin')->willReturn(false);
221+
$configStub->method('getTopActiveView')->willReturn($viewStub);
222+
Registry::set(Config::class, $configStub);
223+
224+
$requestStub = $this->createStub(Request::class);
225+
$requestStub->method('getRequestParameter')->with('fnc')->willReturn('tobasket');
226+
Registry::set(Request::class, $requestStub);
227+
228+
$sessionMock = $this->createMock(Session::class);
229+
$sessionMock->expects($this->once())
230+
->method('setVariable')
231+
->with(AuthorizeService::OTP_TARGET_URL, $currentUrl);
232+
Registry::set(Session::class, $sessionMock);
233+
234+
$sut = new StoreCurrentUrlSubscriber();
235+
$eventStub = $this->createStub(ViewRenderedEvent::class);
236+
237+
$sut->onViewRendered($eventStub);
238+
}
149239
}

translations/de/oesecuritymodule_lang.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
'TWO_FACTOR_AUTHENTICATION_TITLE' => 'Zwei-Faktor-Authentifizierung',
4646
'TWO_FACTOR_AUTHENTICATION_DESCRIPTION' => 'Ein Code wurde an Ihre E-Mail-Adresse gesendet. Bitte geben Sie ihn unten ein, um fortzufahren.',
4747

48-
'RESENT_CODE' => 'Code erneut senden',
48+
'RESEND_CODE' => 'Code erneut senden',
4949

5050
'ERROR_INVALID_CODE' => 'Der Bestätigungscode ist ungültig. Bitte versuchen Sie es erneut.',
5151
'ERROR_CODE_TIME_EXPIRED' => 'Der Bestätigungscode ist abgelaufen. Bitte fordern Sie einen neuen Code an.',

translations/en/oesecuritymodule_lang.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
'TWO_FACTOR_AUTHENTICATION_TITLE' => 'Two Factor Authentication',
4646
'TWO_FACTOR_AUTHENTICATION_DESCRIPTION' => 'Code has been sent to your email. Please enter it below to proceed.',
4747

48-
'RESENT_CODE' => 'Resend Code',
48+
'RESEND_CODE' => 'Resend Code',
4949

5050
'ERROR_INVALID_CODE' => 'The verification code is invalid. Please try again.',
5151
'ERROR_CODE_TIME_EXPIRED' => 'The verification code has expired. Please request a new code.',

0 commit comments

Comments
 (0)