15 Nov 2011 | 00:07
Change user roles during a session in Symfony
Today I encountered a tricky problem with Symfony. If you change properties like the name or the password of your session user the information will be updated in the session automatically. Unfortunately there is one difference: If you change your model in a way that your getRoles method returns a different array of roles these are not updated in the session by default.
Symfony will just use the old roles array until the next real authentication.
So here is the simply solution: Just unauthenticate the user and Symfony will try to authenticate it again with the given credentials. This way the new roles will be loaded into the session.
public function activateAccountAction() { $token = $this->get( 'security.context' )->getToken(); $token->getUser()->setSomethingThatAffectsTheRoleArray( true ); // flush document manager or sth like that $token->setAuthenticated( false ); }
