diff options
author | Kris Wallsmith <kris.wallsmith@gmail.com> | 2011-12-08 08:53:01 -0800 |
---|---|---|
committer | Kris Wallsmith <kris.wallsmith@gmail.com> | 2011-12-08 08:53:01 -0800 |
commit | 571b855fb77836023b28fc3bfd5d5a3b8459e847 (patch) | |
tree | 5929eff8b929f7af11bb2f41055f81d25ec10b01 | |
parent | 223aaf86a0173c799556c935afa9144d41c76fa2 (diff) | |
download | symfony-security-571b855fb77836023b28fc3bfd5d5a3b8459e847.zip symfony-security-571b855fb77836023b28fc3bfd5d5a3b8459e847.tar.gz symfony-security-571b855fb77836023b28fc3bfd5d5a3b8459e847.tar.bz2 |
[Security] added SecurityContextInterface::getUser()
This changes helps the common use case of fetching the current user and better complies with the Law of Demeter (http://en.wikipedia.org/wiki/Law_of_Demeter).
Before (still works):
$token = $context->getToken();
$user = $token ? $token->getUser() : null;
After:
$user = $context->getUser();
-rw-r--r-- | Core/SecurityContext.php | 14 | ||||
-rw-r--r-- | Core/SecurityContextInterface.php | 9 |
2 files changed, 23 insertions, 0 deletions
diff --git a/Core/SecurityContext.php b/Core/SecurityContext.php index 6d11850..fc39407 100644 --- a/Core/SecurityContext.php +++ b/Core/SecurityContext.php @@ -89,4 +89,18 @@ class SecurityContext implements SecurityContextInterface { $this->token = $token; } + + /** + * Returns the current user, if one exists. + * + * @return mixed Returns either an object which implements __toString(), + * or a primitive string if there is a token, otherwise + * returns null. + */ + public function getUser() + { + if ($this->token) { + return $this->token->getUser(); + } + } } diff --git a/Core/SecurityContextInterface.php b/Core/SecurityContextInterface.php index d0c7214..600f7b4 100644 --- a/Core/SecurityContextInterface.php +++ b/Core/SecurityContextInterface.php @@ -39,6 +39,15 @@ interface SecurityContextInterface function setToken(TokenInterface $token = null); /** + * Returns the current user, if one exists. + * + * @return mixed Returns either an object which implements __toString(), + * or a primitive string if there is a token, otherwise + * returns null. + */ + function getUser(); + + /** * Checks if the attributes are granted against the current authentication token and optionally supplied object. * * @param array $attributes |