Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redirect to the last page visited if a back end session expires.
- Loading branch information
Showing
9 changed files
with
210 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* Copyright (c) 2005-2017 Leo Feyer | ||
* | ||
* @license LGPL-3.0+ | ||
*/ | ||
|
||
namespace Contao\CoreBundle\Security\Authentication; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; | ||
use Symfony\Component\Security\Http\HttpUtils; | ||
|
||
class AuthenticationEntryPoint implements AuthenticationEntryPointInterface | ||
{ | ||
/** | ||
* @var HttpUtils | ||
*/ | ||
private $httpUtils; | ||
|
||
/** | ||
* @var RouterInterface | ||
*/ | ||
private $router; | ||
|
||
/** | ||
* @param HttpUtils $httpUtils | ||
* @param RouterInterface $router | ||
*/ | ||
public function __construct(HttpUtils $httpUtils, RouterInterface $router) | ||
{ | ||
$this->httpUtils = $httpUtils; | ||
$this->router = $router; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function start(Request $request, AuthenticationException $authException = null) | ||
{ | ||
if ($request->query->count() < 1) { | ||
return $this->httpUtils->createRedirectResponse($request, 'contao_backend_login'); | ||
} | ||
|
||
$url = $this->router->generate( | ||
'contao_backend_login', | ||
['referer' => base64_encode($request->getRequestUri())], | ||
UrlGeneratorInterface::ABSOLUTE_URL | ||
); | ||
|
||
return $this->httpUtils->createRedirectResponse($request, $url); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
tests/Security/Authentication/AuthenticationEntryPointTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* Copyright (c) 2005-2017 Leo Feyer | ||
* | ||
* @license LGPL-3.0+ | ||
*/ | ||
|
||
namespace Contao\CoreBundle\Tests\Security\Authentication; | ||
|
||
use Contao\CoreBundle\Security\Authentication\AuthenticationEntryPoint; | ||
use Contao\CoreBundle\Tests\TestCase; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Symfony\Component\Security\Http\HttpUtils; | ||
|
||
class AuthenticationEntryPointTest extends TestCase | ||
{ | ||
public function testCanBeInstantiated(): void | ||
{ | ||
$entryPoint = new AuthenticationEntryPoint( | ||
$this->createMock(HttpUtils::class), | ||
$this->createMock(RouterInterface::class) | ||
); | ||
|
||
$this->assertInstanceOf('Contao\CoreBundle\Security\Authentication\AuthenticationEntryPoint', $entryPoint); | ||
} | ||
|
||
public function testAddsTheRefererToTheRedirectUrl(): void | ||
{ | ||
$request = new Request(); | ||
$request->server->set('REQUEST_URI', '/contao?do=page'); | ||
$request->query->add(['do' => 'page']); | ||
|
||
$httpUtils = $this->createMock(HttpUtils::class); | ||
|
||
$httpUtils | ||
->expects($this->once()) | ||
->method('createRedirectResponse') | ||
->willReturnCallback( | ||
function (Request $request, string $url): RedirectResponse { | ||
return new RedirectResponse($url); | ||
} | ||
) | ||
; | ||
|
||
$url = 'http://localhost/contao/login?referer='.base64_encode('/contao?do=page'); | ||
|
||
$router = $this->createMock(RouterInterface::class); | ||
|
||
$router | ||
->expects($this->once()) | ||
->method('generate') | ||
->with('contao_backend_login', ['referer' => base64_encode('/contao?do=page')]) | ||
->willReturn($url) | ||
; | ||
|
||
$entryPoint = new AuthenticationEntryPoint($httpUtils, $router); | ||
$response = $entryPoint->start($request); | ||
|
||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); | ||
$this->assertSame($url, $response->getTargetUrl()); | ||
} | ||
|
||
public function testDoesNotAddARefererToTheRedirectUrlIfTheQueryIsEmpty(): void | ||
{ | ||
$request = new Request(); | ||
$httpUtils = $this->createMock(HttpUtils::class); | ||
|
||
$httpUtils | ||
->expects($this->once()) | ||
->method('createRedirectResponse') | ||
->with($request, 'contao_backend_login') | ||
->willReturn(new RedirectResponse('http://localhost/contao/login')) | ||
; | ||
|
||
$router = $this->createMock(RouterInterface::class); | ||
|
||
$router | ||
->expects($this->never()) | ||
->method('generate') | ||
; | ||
|
||
$entryPoint = new AuthenticationEntryPoint($httpUtils, $router); | ||
$response = $entryPoint->start($request); | ||
|
||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); | ||
$this->assertSame('http://localhost/contao/login', $response->getTargetUrl()); | ||
} | ||
} |