Skip to content

Instantly share code, notes, and snippets.

@tobalsan
Last active August 29, 2015 14:04
Show Gist options
  • Save tobalsan/d355ade3c0a6ba555d6a to your computer and use it in GitHub Desktop.
Save tobalsan/d355ade3c0a6ba555d6a to your computer and use it in GitHub Desktop.
Symfony2: Manage user roles and authentication state
$token = $this->get('security.context')->getToken();
// do things that change user's roles
$token->setAuthenticated(false);
/* OR */
// Add new role
$user->addRole('ROLE_NEW');
// persist the user and it's new role to database
// ...
// Generate new token with new roles
$token = new \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken(
$user,
null,
'main',
$user->getRoles()
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment