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

Skip to content

[Security] added SecurityContextInterface::getUser() #2816

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 1 commit into from
Dec 8, 2011
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions src/Symfony/Component/Security/Core/SecurityContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Copy link
Member

Choose a reason for hiding this comment

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

or the most common case: an object implementing the UserInterface (which does not enforce the __toString method anymore)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just copied this comment from elsewhere.

On Dec 8, 2011, at 8:58 AM, Christophe Coevoet
[email protected]
wrote:

@@ -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 the most common case: an object implementing the UserInterface (which does not enforce the __toString method anymore)


Reply to this email directly or view it on GitHub:
https://github.com/symfony/symfony/pull/2816/files#r280409

Copy link
Member

Choose a reason for hiding this comment

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

then this place is outdated.

Copy link
Contributor

Choose a reason for hiding this comment

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

@stof: a plain string is still possible for the user correct? UserInterface is just the only object alloewd?

Copy link
Member

Choose a reason for hiding this comment

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

yes, they are allowed. this is done for anonymous ones for instance. Both described solutions described in the phpdoc here are right but the third one (and most common) is missing

objects with a __toString method can still be used in the token too. But this can only be used with some weird way to get the user. An implementation of UserProviderInterface must return an instance of UserInterface.

* or a primitive string if there is a token, otherwise
* returns null.
*/
public function getUser()
{
if ($this->token) {
return $this->token->getUser();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}