@@ -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