Created
July 11, 2012 12:25
-
-
Save wpottier/3090063 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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