php - Symfony 2 - Simulate login and security context in console command -


i coding custom console command on symfony2.

my command call 1 service use security context dependency injection (check role).

in order keep security check in service, create specific user , log user in console command.

how can simulate login , have usable security context in command ?

my service check :

if ($this->securitycontext->gettoken() == null ||     !$this->securitycontext->isgranted('is_authenticated_fully') ) 

my command classic console command extends containerawarecommand

best regards, j

you can programatically authenticate user in symfony2 this:

// create authentication token $token = new usernamepasswordtoken(     $user,     null,     'main',     $user->getroles()); // give security context $this->container->get('security.context')->settoken($token); 

edit based on comment:

  • security context deprecated of symfony 2.6. use instead:

$this->container->get('security.token_storage')->settoken($t‌​oken);

thanks @andrea-sprega


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -