Skip to content

Commit

Permalink
re-added a __toString method for debugging purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
schmittjoh authored and fabpot committed Mar 12, 2011
1 parent 1314d6f commit 70867f0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
Expand Up @@ -204,4 +204,20 @@ public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
}

/**
* {@inheritDoc}
*/
public function __toString()
{
$class = get_class($this);
$class = substr($class, strrpos($class, '\\')+1);

$roles = array();
foreach ($this->roles as $role) {
$roles[] = $role->getRole();
}

return sprintf('%s(user="%s", authenticated=%s, roles="%s")', $class, $this->getUsername(), json_encode($this->authenticated), implode(', ', $roles));
}
}
Expand Up @@ -21,6 +21,15 @@
*/
interface TokenInterface extends \Serializable
{
/**
* Returns a string representation ofthe Token.
*
* This is only to be used for debugging purposes.
*
* @return string
*/
function __toString();

/**
* Returns the user roles.
*
Expand Down
Expand Up @@ -55,7 +55,7 @@ public function handle(EventInterface $event)
$request = $event->get('request');

if (null !== $this->logger) {
$this->logger->debug(sprintf('Checking secure context token: %s', $this->securityContext->getToken()->getUserName()));
$this->logger->debug(sprintf('Checking secure context token: %s', $this->securityContext->getToken()));
}

list($user, $credentials) = $this->getPreAuthenticatedData($request);
Expand All @@ -74,7 +74,7 @@ public function handle(EventInterface $event)
$token = $this->authenticationManager->authenticate(new PreAuthenticatedToken($user, $credentials, $this->providerKey));

if (null !== $this->logger) {
$this->logger->debug(sprintf('Authentication success: %s', $token->getUserName()));
$this->logger->debug(sprintf('Authentication success: %s', $token));
}
$this->securityContext->setToken($token);

Expand Down
Expand Up @@ -49,4 +49,10 @@ public function testEraseCredentials()
$token->eraseCredentials();
$this->assertEquals('', $token->getCredentials());
}

public function testToString()
{
$token = new UsernamePasswordToken('foo', '', 'foo', array('A', 'B'));
$this->assertEquals('UsernamePasswordToken(user="foo", authenticated=true, roles="A, B")', (string) $token);
}
}

2 comments on commit 70867f0

@abdeltiflouardi
Copy link

Choose a reason for hiding this comment

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

I have a problem in AbstractToken.php

Notice: serialize() [function.serialize]: "id" returned as member variable from __sleep() but does not exist in PATH/vendor/symfony/src/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php on line 136

Notice: serialize() [function.serialize]: "name" returned as member variable from __sleep() but does not exist in PATH/vendor/symfony/src/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php on line 136

Notice: serialize() [function.serialize]: "members" returned as member variable from __sleep() but does not exist in PATH/vendor/symfony/src/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php on line 136

@abdeltiflouardi
Copy link

Choose a reason for hiding this comment

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

I resolved the problem here

abdeltiflouardi@0f660c8

Please sign in to comment.