1

If I know who is my user, and I want tell to symfony that is logged in with a ROLE_USER role. Is this code enough? I enough, assign a token to current session?

$request->getSession()->set(                                                                                                               
    '_security_secured_area',                                                                                                              
    serialize(new UsernamePasswordToken(                                                                                                   
        $user = new User(                                                                                                                  
            'foo',                                                                                           
            'bar',                                                                                           
            ['ROLE_USER']                                                                                                                  
        ),                                                                                                                                 
        null,                                                                                                                              
        'secured_area',                                                                                                                    
        $user->getRoles()                                                                                                                  
    ))
);
sensorario
  • 20,262
  • 30
  • 97
  • 159
  • 1
    What happens when you tried your approach? Might want to read this: http://hasin.me/2013/10/27/how-to-login-a-user-programatically-in-symfony2/ because the security component is one of the more complex systems and it's easy to get wrong. – Cerad Nov 06 '15 at 16:02
  • Good point. In my example, I set session. In your link, an event is dispatched. Maybe that event do the same thing. – sensorario Nov 06 '15 at 16:10

0 Answers0