Skip to content

Instantly share code, notes, and snippets.

@wpottier
Created July 11, 2012 12:25
Show Gist options
  • Save wpottier/3090063 to your computer and use it in GitHub Desktop.
Save wpottier/3090063 to your computer and use it in GitHub Desktop.
/**
* Get a user from the Security Context
*
* @return mixed
*
* @throws \LogicException If SecurityBundle is not available
*
* @see Symfony\Component\Security\Core\Authentication\Token\TokenInterface::getUser()
*/
public function getUser()
{
if (!$this->container->has('security.context')) {
throw new \LogicException('The SecurityBundle is not registered in your application.');
}
if (null === $token = $this->container->get('security.context')->getToken()) {
return null;
}
if (!is_object($user = $token->getUser())) {
return null;
}
return $user;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment