Skip to content

Commit 391c182

Browse files
feature #40153 [Security] LoginLink with specific locale (roromix)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [Security] LoginLink with specific locale | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT | Doc PR | I have added the possibility to create a login link for a specific locale. It's useful when we want generate a link for an other user who isn't in the same locale of us. Commits ------- 50673c5321 [Security] LoginLink with specific locale
2 parents c829751 + 562286d commit 391c182

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

LoginLink/LoginLinkHandler.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1616
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
17+
use Symfony\Component\Routing\RequestContext;
1718
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
1819
use Symfony\Component\Security\Core\User\UserInterface;
1920
use Symfony\Component\Security\Core\User\UserProviderInterface;
@@ -49,7 +50,7 @@ public function __construct(UrlGeneratorInterface $urlGenerator, UserProviderInt
4950
$this->expiredStorage = $expiredStorage;
5051
}
5152

52-
public function createLoginLink(UserInterface $user): LoginLinkDetails
53+
public function createLoginLink(UserInterface $user, Request $request = null): LoginLinkDetails
5354
{
5455
$expiresAt = new \DateTimeImmutable(sprintf('+%d seconds', $this->options['lifetime']));
5556

@@ -60,11 +61,22 @@ public function createLoginLink(UserInterface $user): LoginLinkDetails
6061
'hash' => $this->computeSignatureHash($user, $expires),
6162
];
6263

63-
$url = $this->urlGenerator->generate(
64-
$this->options['route_name'],
65-
$parameters,
66-
UrlGeneratorInterface::ABSOLUTE_URL
67-
);
64+
if ($request) {
65+
$currentRequestContext = $this->urlGenerator->getContext();
66+
$this->urlGenerator->setContext((new RequestContext())->fromRequest($request));
67+
}
68+
69+
try {
70+
$url = $this->urlGenerator->generate(
71+
$this->options['route_name'],
72+
$parameters,
73+
UrlGeneratorInterface::ABSOLUTE_URL
74+
);
75+
} finally {
76+
if ($request) {
77+
$this->urlGenerator->setContext($currentRequestContext);
78+
}
79+
}
6880

6981
return new LoginLinkDetails($url, $expiresAt);
7082
}

LoginLink/LoginLinkHandlerInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface LoginLinkHandlerInterface
2525
/**
2626
* Generate a link that can be used to authenticate as the given user.
2727
*/
28-
public function createLoginLink(UserInterface $user): LoginLinkDetails;
28+
public function createLoginLink(UserInterface $user, Request $request = null): LoginLinkDetails;
2929

3030
/**
3131
* Validates if this request contains a login link and returns the associated User.

Tests/LoginLink/LoginLinkHandlerTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function setUp(): void
4848
* @dataProvider provideCreateLoginLinkData
4949
* @group time-sensitive
5050
*/
51-
public function testCreateLoginLink($user, array $extraProperties)
51+
public function testCreateLoginLink($user, array $extraProperties, Request $request = null)
5252
{
5353
$this->router->expects($this->once())
5454
->method('generate')
@@ -65,12 +65,23 @@ public function testCreateLoginLink($user, array $extraProperties)
6565
)
6666
->willReturn('https://example.com/login/verify?user=weaverryan&hash=abchash&expires=1601235000');
6767

68-
$loginLink = $this->createLinker([], array_keys($extraProperties))->createLoginLink($user);
68+
$loginLink = $this->createLinker([], array_keys($extraProperties))->createLoginLink($user, $request);
6969
$this->assertSame('https://example.com/login/verify?user=weaverryan&hash=abchash&expires=1601235000', $loginLink->getUrl());
7070
}
7171

7272
public function provideCreateLoginLinkData()
7373
{
74+
yield [
75+
new TestLoginLinkHandlerUser('weaverryan', '[email protected]', 'pwhash'),
76+
['emailProperty' => '[email protected]', 'passwordProperty' => 'pwhash'],
77+
Request::create('https://example.com'),
78+
];
79+
80+
yield [
81+
new TestLoginLinkHandlerUser('weaverryan', '[email protected]', 'pwhash'),
82+
['emailProperty' => '[email protected]', 'passwordProperty' => 'pwhash'],
83+
];
84+
7485
yield [
7586
new TestLoginLinkHandlerUser('weaverryan', '[email protected]', 'pwhash'),
7687
['emailProperty' => '[email protected]', 'passwordProperty' => 'pwhash'],

0 commit comments

Comments
 (0)