15
15
use Symfony \Component \HttpKernel \Event \GetResponseEvent ;
16
16
use Symfony \Component \HttpKernel \Event \FilterResponseEvent ;
17
17
use Symfony \Component \HttpKernel \KernelEvents ;
18
+ use Symfony \Component \Security \Core \Authentication \AuthenticationTrustResolver ;
19
+ use Symfony \Component \Security \Core \Authentication \AuthenticationTrustResolverInterface ;
18
20
use Symfony \Component \Security \Core \Authentication \Token \AnonymousToken ;
19
21
use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorageInterface ;
20
22
use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
@@ -39,8 +41,9 @@ class ContextListener implements ListenerInterface
39
41
private $ userProviders ;
40
42
private $ dispatcher ;
41
43
private $ registered ;
44
+ private $ trustResolver ;
42
45
43
- public function __construct (TokenStorageInterface $ tokenStorage , array $ userProviders , $ contextKey , LoggerInterface $ logger = null , EventDispatcherInterface $ dispatcher = null )
46
+ public function __construct (TokenStorageInterface $ tokenStorage , array $ userProviders , $ contextKey , LoggerInterface $ logger = null , EventDispatcherInterface $ dispatcher = null , AuthenticationTrustResolverInterface $ trustResolver = null )
44
47
{
45
48
if (empty ($ contextKey )) {
46
49
throw new \InvalidArgumentException ('$contextKey must not be empty. ' );
@@ -58,6 +61,7 @@ public function __construct(TokenStorageInterface $tokenStorage, array $userProv
58
61
$ this ->sessionKey = '_security_ ' .$ contextKey ;
59
62
$ this ->logger = $ logger ;
60
63
$ this ->dispatcher = $ dispatcher ;
64
+ $ this ->trustResolver = $ trustResolver ?: new AuthenticationTrustResolver ('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken ' , 'Symfony\Component\Security\Core\Authentication\Token\RememberMeToken ' );
61
65
}
62
66
63
67
/**
@@ -121,7 +125,7 @@ public function onKernelResponse(FilterResponseEvent $event)
121
125
$ request = $ event ->getRequest ();
122
126
$ session = $ request ->getSession ();
123
127
124
- if ((null === $ token = $ this ->tokenStorage ->getToken ()) || ($ token instanceof AnonymousToken )) {
128
+ if ((null === $ token = $ this ->tokenStorage ->getToken ()) || ($ this -> trustResolver -> isAnonymous ( $ token) )) {
125
129
if ($ request ->hasPreviousSession ()) {
126
130
$ session ->remove ($ this ->sessionKey );
127
131
}
0 commit comments