From 41872cd40eef8c2a39a098b33655ee70a18c9ea0 Mon Sep 17 00:00:00 2001 From: Kris Wallsmith Date: Thu, 8 Dec 2011 08:53:01 -0800 Subject: [PATCH] [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(); --- .../Security/Core/SecurityContext.php | 14 ++++++++++++++ .../Security/Core/SecurityContextInterface.php | 9 +++++++++ .../Security/Core/SecurityContextTest.php | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/src/Symfony/Component/Security/Core/SecurityContext.php b/src/Symfony/Component/Security/Core/SecurityContext.php index 6d1185065ea7..fc394073af42 100644 --- a/src/Symfony/Component/Security/Core/SecurityContext.php +++ b/src/Symfony/Component/Security/Core/SecurityContext.php @@ -89,4 +89,18 @@ public function setToken(TokenInterface $token = null) { $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/src/Symfony/Component/Security/Core/SecurityContextInterface.php b/src/Symfony/Component/Security/Core/SecurityContextInterface.php index d0c72142d110..600f7b4c7c91 100644 --- a/src/Symfony/Component/Security/Core/SecurityContextInterface.php +++ b/src/Symfony/Component/Security/Core/SecurityContextInterface.php @@ -38,6 +38,15 @@ function getToken(); */ 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. * diff --git a/tests/Symfony/Tests/Component/Security/Core/SecurityContextTest.php b/tests/Symfony/Tests/Component/Security/Core/SecurityContextTest.php index 0ee57113d40e..04e6d7c4277f 100644 --- a/tests/Symfony/Tests/Component/Security/Core/SecurityContextTest.php +++ b/tests/Symfony/Tests/Component/Security/Core/SecurityContextTest.php @@ -89,4 +89,22 @@ public function testGetSetToken() $context->setToken($token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')); $this->assertSame($token, $context->getToken()); } + + public function testGetUser() + { + $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); + $token->expects($this->once()) + ->method('getUser') + ->will($this->returnValue('foo')); + + $context = new SecurityContext( + $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface'), + $this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface') + ); + + $this->assertNull($context->getUser(), '->getUser() returns null when there is no token'); + + $context->setToken($token); + $this->assertEquals('foo', $context->getUser(), '->getUser() return the token user'); + } }