UserSecurityIdentity.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. use Symfony\Component\Security\Core\Util\ClassUtils;
  14. use Symfony\Component\Security\Acl\Model\SecurityIdentityInterface;
  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 (empty($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. * @return UserSecurityIdentity
  48. */
  49. public static function fromAccount(UserInterface $user)
  50. {
  51. return new self($user->getUsername(), ClassUtils::getRealClass($user));
  52. }
  53. /**
  54. * Creates a user security identity from a TokenInterface
  55. *
  56. * @param TokenInterface $token
  57. * @return UserSecurityIdentity
  58. */
  59. public static function fromToken(TokenInterface $token)
  60. {
  61. $user = $token->getUser();
  62. if ($user instanceof UserInterface) {
  63. return self::fromAccount($user);
  64. }
  65. return new self((string) $user, is_object($user) ? ClassUtils::getRealClass($user) : ClassUtils::getRealClass($token));
  66. }
  67. /**
  68. * Returns the username
  69. *
  70. * @return string
  71. */
  72. public function getUsername()
  73. {
  74. return $this->username;
  75. }
  76. /**
  77. * Returns the user's class name
  78. *
  79. * @return string
  80. */
  81. public function getClass()
  82. {
  83. return $this->class;
  84. }
  85. /**
  86. * {@inheritDoc}
  87. */
  88. public function equals(SecurityIdentityInterface $sid)
  89. {
  90. if (!$sid instanceof UserSecurityIdentity) {
  91. return false;
  92. }
  93. return $this->username === $sid->getUsername()
  94. && $this->class === $sid->getClass();
  95. }
  96. /**
  97. * A textual representation of this security identity.
  98. *
  99. * This is not used for equality comparison, but only for debugging.
  100. *
  101. * @return string
  102. */
  103. public function __toString()
  104. {
  105. return sprintf('UserSecurityIdentity(%s, %s)', $this->username, $this->class);
  106. }
  107. }