2010-12-14 15:43:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-12-14 15:43:40 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-12-14 15:43:40 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Security\Acl\Domain;
|
|
|
|
|
2011-01-25 19:28:26 +00:00
|
|
|
use Symfony\Component\Security\Authentication\Token\TokenInterface;
|
2011-01-15 13:29:43 +00:00
|
|
|
use Symfony\Component\Security\User\AccountInterface;
|
|
|
|
use Symfony\Component\Security\Acl\Model\SecurityIdentityInterface;
|
|
|
|
|
2010-12-14 15:43:40 +00:00
|
|
|
/**
|
|
|
|
* A SecurityIdentity implementation used for actual users
|
|
|
|
*
|
|
|
|
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
|
|
|
*/
|
|
|
|
class UserSecurityIdentity implements SecurityIdentityInterface
|
|
|
|
{
|
|
|
|
protected $username;
|
2011-01-02 01:33:00 +00:00
|
|
|
protected $class;
|
2010-12-14 15:43:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2011-01-02 01:33:00 +00:00
|
|
|
* @param string $username the username representation
|
|
|
|
* @param string $class the user's fully qualified class name
|
2010-12-14 15:43:40 +00:00
|
|
|
*/
|
2011-01-02 01:33:00 +00:00
|
|
|
public function __construct($username, $class)
|
2010-12-14 15:43:40 +00:00
|
|
|
{
|
2011-01-02 01:33:00 +00:00
|
|
|
if (empty($username)) {
|
2010-12-14 15:43:40 +00:00
|
|
|
throw new \InvalidArgumentException('$username must not be empty.');
|
|
|
|
}
|
2011-01-02 01:33:00 +00:00
|
|
|
if (empty($class)) {
|
|
|
|
throw new \InvalidArgumentException('$class must not be empty.');
|
|
|
|
}
|
2010-12-14 15:43:40 +00:00
|
|
|
|
|
|
|
$this->username = $username;
|
2011-01-02 01:33:00 +00:00
|
|
|
$this->class = $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a user security identity from an AccountInterface
|
|
|
|
*
|
|
|
|
* @param AccountInterface $user
|
|
|
|
* @return UserSecurityIdentity
|
|
|
|
*/
|
|
|
|
public static function fromAccount(AccountInterface $user)
|
|
|
|
{
|
2011-01-25 19:28:26 +00:00
|
|
|
return new self($user->getUsername(), get_class($user));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a user security identity from a TokenInterface
|
|
|
|
*
|
|
|
|
* @param TokenInterface $token
|
|
|
|
* @return UserSecurityIdentity
|
|
|
|
*/
|
|
|
|
public static function fromToken(TokenInterface $token)
|
|
|
|
{
|
|
|
|
$user = $token->getUser();
|
|
|
|
|
|
|
|
if ($user instanceof AccountInterface) {
|
|
|
|
return self::fromAccount($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new self((string) $user, is_object($user)? get_class($user) : get_class($token));
|
2010-12-14 15:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the username
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUsername()
|
|
|
|
{
|
|
|
|
return $this->username;
|
|
|
|
}
|
|
|
|
|
2011-01-02 01:33:00 +00:00
|
|
|
/**
|
|
|
|
* Returns the user's class name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getClass()
|
|
|
|
{
|
|
|
|
return $this->class;
|
|
|
|
}
|
|
|
|
|
2010-12-14 15:43:40 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function equals(SecurityIdentityInterface $sid)
|
|
|
|
{
|
|
|
|
if (!$sid instanceof UserSecurityIdentity) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-02 01:33:00 +00:00
|
|
|
return $this->username === $sid->getUsername()
|
|
|
|
&& $this->class === $sid->getClass();
|
2010-12-14 15:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A textual representation of this security identity.
|
|
|
|
*
|
|
|
|
* This is not used for equality comparison, but only for debugging.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
2011-01-02 01:33:00 +00:00
|
|
|
return sprintf('UserSecurityIdentity(%s, %s)', $this->username, $this->class);
|
2010-12-14 15:43:40 +00:00
|
|
|
}
|
|
|
|
}
|