7
7
use Symfony \Component \BrowserKit \Cookie ;
8
8
use Symfony \Component \HttpFoundation \Session \SessionInterface ;
9
9
use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorageInterface ;
10
+ use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
10
11
use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
11
12
use Symfony \Component \Security \Core \User \UserInterface ;
12
13
use Symfony \Component \Security \Guard \Token \PostAuthenticationGuardToken ;
@@ -32,12 +33,21 @@ trait SessionAssertionsTrait
32
33
*/
33
34
public function amLoggedInAs (UserInterface $ user , string $ firewallName = 'main ' , string $ firewallContext = null ): void
34
35
{
35
- $ session = $ this ->getCurrentSession ();
36
36
$ roles = $ user ->getRoles ();
37
-
38
37
$ token = $ this ->createAuthenticationToken ($ user , $ firewallName , $ roles );
38
+ $ this ->loginWithToken ($ token , $ firewallContext , $ firewallName );
39
+ }
40
+
41
+ public function amLoggedInWithToken (TokenInterface $ token , string $ firewallName = 'main ' , string $ firewallContext = null ): void
42
+ {
43
+ $ this ->loginWithToken ($ token , $ firewallName , $ firewallContext );
44
+ }
45
+
46
+ private function loginWithToken (TokenInterface $ token , string $ firewallName = 'main ' , string $ firewallContext = null ): void
47
+ {
39
48
$ this ->getTokenStorage ()->setToken ($ token );
40
49
50
+ $ session = $ this ->getCurrentSession ();
41
51
$ sessionKey = $ firewallContext ? "_security_ {$ firewallContext }" : "_security_ {$ firewallName }" ;
42
52
$ session ->set ($ sessionKey , serialize ($ token ));
43
53
$ session ->save ();
@@ -196,7 +206,7 @@ protected function getSymfonyMajorVersion(): int
196
206
/**
197
207
* @return UsernamePasswordToken|PostAuthenticationGuardToken|PostAuthenticationToken
198
208
*/
199
- protected function createAuthenticationToken (UserInterface $ user , string $ firewallName , array $ roles )
209
+ public function createAuthenticationToken (UserInterface $ user , string $ firewallName , array $ roles )
200
210
{
201
211
if ($ this ->getSymfonyMajorVersion () < 6 ) {
202
212
return $ this ->config ['guard ' ]
0 commit comments