UserSecurityIdentity.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Acl\Domain;
  11. use Symfony\Component\Security\Acl\Model\SecurityIdentityInterface;
  12. use Symfony\Component\Security\Acl\Util\ClassUtils;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. /**
  16. * A SecurityIdentity implementation used for actual users.
  17. *
  18. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  19. */
  20. final class UserSecurityIdentity implements SecurityIdentityInterface
  21. {
  22. private $username;
  23. private $class;
  24. /**
  25. * Constructor.
  26. *
  27. * @param string $username the username representation
  28. * @param string $class the user's fully qualified class name
  29. *
  30. * @throws \InvalidArgumentException
  31. */
  32. public function __construct($username, $class)
  33. {
  34. if ('' === $username || null === $username) {
  35. throw new \InvalidArgumentException('$username must not be empty.');
  36. }
  37. if (empty($class)) {
  38. throw new \InvalidArgumentException('$class must not be empty.');
  39. }
  40. $this->username = (string) $username;
  41. $this->class = $class;
  42. }
  43. /**
  44. * Creates a user security identity from a UserInterface.
  45. *
  46. * @param UserInterface $user
  47. *
  48. * @return UserSecurityIdentity
  49. */
  50. public static function fromAccount(UserInterface $user)
  51. {
  52. return new self($user->getUsername(), ClassUtils::getRealClass($user));
  53. }
  54. /**
  55. * Creates a user security identity from a TokenInterface.
  56. *
  57. * @param TokenInterface $token
  58. *
  59. * @return UserSecurityIdentity
  60. */
  61. public static function fromToken(TokenInterface $token)
  62. {
  63. $user = $token->getUser();
  64. if ($user instanceof UserInterface) {
  65. return self::fromAccount($user);
  66. }
  67. return new self((string) $user, is_object($user) ? ClassUtils::getRealClass($user) : ClassUtils::getRealClass($token));
  68. }
  69. /**
  70. * Returns the username.
  71. *
  72. * @return string
  73. */
  74. public function getUsername()
  75. {
  76. return $this->username;
  77. }
  78. /**
  79. * Returns the user's class name.
  80. *
  81. * @return string
  82. */
  83. public function getClass()
  84. {
  85. return $this->class;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function equals(SecurityIdentityInterface $sid)
  91. {
  92. if (!$sid instanceof self) {
  93. return false;
  94. }
  95. return $this->username === $sid->getUsername()
  96. && $this->class === $sid->getClass();
  97. }
  98. /**
  99. * A textual representation of this security identity.
  100. *
  101. * This is not used for equality comparison, but only for debugging.
  102. *
  103. * @return string
  104. */
  105. public function __toString()
  106. {
  107. return sprintf('UserSecurityIdentity(%s, %s)', $this->username, $this->class);
  108. }
  109. }