I am currently coding a custom console command on Symfony2.
My command call one service that use the security context with dependency injection (check role).
In order to keep that security check in my service, i would like to create a specific user and log this user in my console command.
How can i simulate that login and have a usable security context in my command ?
My service check :
if ($this->securityContext->getToken() == null ||
You can programatically authenticate a user in Symfony2 like this:
// create the authentication token $token = new UsernamePasswordToken( $user, null, 'main', $user->getRoles()); // give it to the security context $this->container->get('security.context')->setToken($token);
EDIT based on comment: