Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 76ba2bc

Browse files
committed
merged branch kriswallsmith/security/demeter-fix (PR #2816)
Commits ------- 41872cd [Security] added SecurityContextInterface::getUser() Discussion ---------- [Security] added SecurityContextInterface::getUser() This changes helps the common use case of fetching the current user and better complies with the [Law of Demeter][1]. Before (still works): $token = $context->getToken(); $user = $token ? $token->getUser() : null; After: $user = $context->getUser(); The fine print: ``` Bug fix: no Feature addition: yes Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: ~ Todo: ~ ``` [1]: http://en.wikipedia.org/wiki/Law_of_Demeter
2 parents 4730f43 + 41872cd commit 76ba2bc

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/Symfony/Component/Security/Core/SecurityContext.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,18 @@ public function setToken(TokenInterface $token = null)
8989
{
9090
$this->token = $token;
9191
}
92+
93+
/**
94+
* Returns the current user, if one exists.
95+
*
96+
* @return mixed Returns either an object which implements __toString(),
97+
* or a primitive string if there is a token, otherwise
98+
* returns null.
99+
*/
100+
public function getUser()
101+
{
102+
if ($this->token) {
103+
return $this->token->getUser();
104+
}
105+
}
92106
}

src/Symfony/Component/Security/Core/SecurityContextInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ function getToken();
3838
*/
3939
function setToken(TokenInterface $token = null);
4040

41+
/**
42+
* Returns the current user, if one exists.
43+
*
44+
* @return mixed Returns either an object which implements __toString(),
45+
* or a primitive string if there is a token, otherwise
46+
* returns null.
47+
*/
48+
function getUser();
49+
4150
/**
4251
* Checks if the attributes are granted against the current authentication token and optionally supplied object.
4352
*

tests/Symfony/Tests/Component/Security/Core/SecurityContextTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,22 @@ public function testGetSetToken()
8989
$context->setToken($token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'));
9090
$this->assertSame($token, $context->getToken());
9191
}
92+
93+
public function testGetUser()
94+
{
95+
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
96+
$token->expects($this->once())
97+
->method('getUser')
98+
->will($this->returnValue('foo'));
99+
100+
$context = new SecurityContext(
101+
$this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface'),
102+
$this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')
103+
);
104+
105+
$this->assertNull($context->getUser(), '->getUser() returns null when there is no token');
106+
107+
$context->setToken($token);
108+
$this->assertEquals('foo', $context->getUser(), '->getUser() return the token user');
109+
}
92110
}

0 commit comments

Comments
 (0)