added a Controller:getUser() shortcut to recover the current user

This commit is contained in:
Julien Brochet 2011-08-19 16:43:43 +02:00
parent b66eb4db57
commit 181332b4d4

View File

@ -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 ?: $token->getUser();
}
/**
* Returns true if the service id is defined.
*