Skip to content

[2.1] added a Controller:getUser() shortcut to recover the current user #1990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 28, 2011
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,24 @@ public function getDoctrine()
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.');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to be pedantic, but i would like to suggest to use the term "registered" instead of "installed", as it is the common sf2 terminology.

@fabpot Btw, long time ago there was a discussion about more pedagogy in error messages in the dev group: https://groups.google.com/group/symfony-devs/browse_thread/thread/f8045eab3a780e25/9c1af84f3d9ffac2
Has this been established finally?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kaiwa: we try to improve error messages on case by case basis.

}

$token = $this->container->get('security.context')->getToken();

return null === $token ? null : $token->getUser();
}

/**
* Returns true if the service id is defined.
*
Expand Down