Skip to content

Commit eda014b

Browse files
committed
Merge pull request #10
2 parents f96e7bd + 3f77c96 commit eda014b

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ interactive login core event will be dispatched with the authenticated
6464

6565
If a token parameter is present in the request, but the user is already
6666
authenticated, a custom event will be dispatched, which includes the token's
67-
value. After dispatching this event, the listener will return immediately, since
68-
there is no work to be done.
67+
value. After dispatching this event, the listener's default behavior is to
68+
return immediately, since there is likely no work to be done.
6969

70-
A practical use for this event would be to mark user's email addresses as
70+
A practical use for this event would be to mark a user's email addresses as
7171
confirmed, assuming the auto-login link with the token was only delivered via
7272
email. As a business requirement, the confirmation service might also listen to
7373
the interactive login core event and operate when the authenticated token was an
@@ -80,3 +80,17 @@ may be helpful to inject this library's provider class.
8080

8181
[Antonio Trapani]: https://github.com/TwistedLogic
8282
[PR #9]: https://github.com/jmikola/AutoLogin/pull/9
83+
84+
#### Overriding Already Authenticated Users
85+
86+
*This feature was contributed by [Mathieu Gauthier-Lafaye][] in [PR #10][].*
87+
88+
By default, the listener will only dispatch an event if the user is already
89+
authenticated; it does not override the existing authenticated user. In some
90+
cases, it may be desirable to allow an auto-login link to override an existing
91+
authenticated user. Otherwise, the user would first need to log out before using
92+
the auto-login link. Setting the listener's `override_already_authenticated`
93+
boolean option to `true` will enable this behavior.
94+
95+
[Mathieu Gauthier-Lafaye]: https://github.com/gauthierl
96+
[PR #10]: https://github.com/jmikola/AutoLogin/pull/10

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"extra": {
2222
"branch-alias": {
23-
"dev-master": "1.1.x-dev"
23+
"dev-master": "1.2.x-dev"
2424
}
2525
}
2626
}

src/Jmikola/AutoLogin/Http/Firewall/AutoLoginListener.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,31 @@ class AutoLoginListener implements ListenerInterface
2424
private $providerKey;
2525
private $securityContext;
2626
private $tokenParam;
27+
private $options;
2728

2829
/**
29-
* Constructor
30+
* Constructor.
3031
*
3132
* @param SecurityContextInterface $securityContext
3233
* @param AuthenticationManagerInterface $authenticationManager
3334
* @param string $providerKey
3435
* @param string $tokenParam
3536
* @param LoggerInterface $logger
3637
* @param EventDispatcherInterface $dispatcher
38+
* @param array $options
3739
*/
38-
public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, $providerKey, $tokenParam, LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null)
40+
public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, $providerKey, $tokenParam, LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null, array $options = array())
3941
{
4042
$this->securityContext = $securityContext;
4143
$this->authenticationManager = $authenticationManager;
4244
$this->providerKey = $providerKey;
4345
$this->tokenParam = $tokenParam;
4446
$this->logger = $logger;
4547
$this->dispatcher = $dispatcher;
48+
49+
$this->options = $options = array_merge(array(
50+
'override_already_authenticated' => false,
51+
), $options);
4652
}
4753

4854
/**
@@ -58,17 +64,24 @@ public function handle(GetResponseEvent $event)
5864

5965
$tokenParam = $request->get($this->tokenParam);
6066

61-
/* If the security context has a token, a user is already authenticated
62-
* and there is nothing to do. Before returning, dispatch an event with
63-
* the token parameter so that a listener may track its usage.
67+
/* If the security context has a token, a user is already authenticated.
68+
* We will dispatch an event with the token parameter so that a listener
69+
* may track its usage.
6470
*/
6571
if (null !== $this->securityContext->getToken()) {
6672
if (null !== $this->dispatcher) {
6773
$event = new AlreadyAuthenticatedEvent($tokenParam);
6874
$this->dispatcher->dispatch(AutoLoginEvents::ALREADY_AUTHENTICATED, $event);
6975
}
7076

71-
return;
77+
/* By default, ignore the token and return; however, in some cases
78+
* it may be useful to override the existing token and allow the
79+
* AutoLogin token to be used to switch users (without requiring
80+
* the user to first log out).
81+
*/
82+
if ( ! $this->options['override_already_authenticated']) {
83+
return;
84+
}
7285
}
7386

7487
try {

0 commit comments

Comments
 (0)