merged branch aerialls/getuser (PR #1990)

Commits
-------

f7bf7b5 fixed condition
181332b added a Controller:getUser() shortcut to recover the current user

Discussion
----------

[2.1] added a Controller:getUser() shortcut to recover the current user
This commit is contained in:
Fabien Potencier 2011-09-28 09:13:48 +02:00
commit bcc7357e55

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