diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php b/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php index f9e27d25b2..833f1c3650 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php @@ -165,6 +165,24 @@ class Controller extends ContainerAware return $this->container->get('doctrine'); } + /** + * Get a user from the Security Context + * + * @return mixed + * + * @throws \LogicException If SecurityBundle is not available + */ + public function getUser() + { + if (!$this->container->has('security.context')) { + throw new \LogicException('The SecurityBundle is not installed in your application.'); + } + + $token = $this->container->get('security.context')->getToken(); + + return null === $token ? null : $token->getUser(); + } + /** * Returns true if the service id is defined. *