Skip to content

Commit

Permalink
Merge branch '6.4' into 7.0
Browse files Browse the repository at this point in the history
* 6.4:
  [Messenger] Fix using negative delay
  [Validator] Add missing italian translation
  [Validator] Fix using known option names as field names
  [SecurityBundle] Prevent to login/logout without a request context
  Suppress warnings from is_executable
  • Loading branch information
fabpot committed Dec 24, 2023
2 parents 466107f + 97d4fb6 commit 5c781fc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public function getFirewallConfig(Request $request): ?FirewallConfig
public function login(UserInterface $user, string $authenticatorName = null, string $firewallName = null, array $badges = []): ?Response
{
$request = $this->container->get('request_stack')->getCurrentRequest();
if (null === $request) {
throw new LogicException('Unable to login without a request context.');
}

$firewallName ??= $this->getFirewallConfig($request)?->getName();

if (!$firewallName) {
Expand All @@ -108,15 +112,18 @@ public function login(UserInterface $user, string $authenticatorName = null, str
*/
public function logout(bool $validateCsrfToken = true): ?Response
{
$request = $this->container->get('request_stack')->getMainRequest();
if (null === $request) {
throw new LogicException('Unable to logout without a request context.');
}

/** @var TokenStorageInterface $tokenStorage */
$tokenStorage = $this->container->get('security.token_storage');

if (!($token = $tokenStorage->getToken()) || !$token->getUser()) {
throw new LogicException('Unable to logout as there is no logged-in user.');
}

$request = $this->container->get('request_stack')->getMainRequest();

if (!$firewallConfig = $this->container->get('security.firewall.map')->getFirewallConfig($request)) {
throw new LogicException('Unable to logout as the request is not behind a firewall.');
}
Expand Down
43 changes: 43 additions & 0 deletions Tests/SecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,28 @@ public function testLoginWithoutAuthenticatorThrows()
$security->login($user);
}

public function testLoginWithoutRequestContext()
{
$requestStack = new RequestStack();
$user = $this->createMock(UserInterface::class);

$container = $this->createMock(ContainerInterface::class);
$container
->expects($this->atLeastOnce())
->method('get')
->willReturnMap([
['request_stack', $requestStack],
])
;

$security = new Security($container, ['main' => null]);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Unable to login without a request context.');

$security->login($user);
}

public function testLogout()
{
$request = new Request();
Expand Down Expand Up @@ -458,6 +480,27 @@ public function testLogoutWithValidCsrf()
$this->assertEquals('a custom response', $response->getContent());
}

public function testLogoutWithoutRequestContext()
{
$requestStack = new RequestStack();

$container = $this->createMock(ContainerInterface::class);
$container
->expects($this->atLeastOnce())
->method('get')
->willReturnMap([
['request_stack', $requestStack],
])
;

$security = new Security($container, ['main' => null]);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Unable to logout without a request context.');

$security->logout();
}

private function createContainer(string $serviceId, object $serviceObject): ContainerInterface
{
return new ServiceLocator([$serviceId => fn () => $serviceObject]);
Expand Down

0 comments on commit 5c781fc

Please sign in to comment.