Token.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Symfony\Component\Security\Authentication\Token;
  3. use Symfony\Component\Security\Role\RoleInterface;
  4. use Symfony\Component\Security\Role\Role;
  5. use Symfony\Component\Security\User\AccountInterface;
  6. /*
  7. * This file is part of the Symfony package.
  8. *
  9. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  10. *
  11. * For the full copyright and license information, please view the LICENSE
  12. * file that was distributed with this source code.
  13. */
  14. /**
  15. * Base class for Token instances.
  16. *
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. */
  19. abstract class Token implements TokenInterface
  20. {
  21. protected $roles;
  22. protected $authenticated;
  23. protected $user;
  24. protected $credentials;
  25. protected $immutable;
  26. /**
  27. * Constructor.
  28. *
  29. * @param Role[] $roles An array of roles
  30. */
  31. public function __construct(array $roles = array())
  32. {
  33. $this->roles = array();
  34. foreach ($roles as $role) {
  35. if (is_string($role)) {
  36. $role = new Role((string) $role);
  37. }
  38. $this->addRole($role);
  39. }
  40. $this->authenticated = false;
  41. $this->immutable = false;
  42. }
  43. /**
  44. * Adds a Role to the token.
  45. *
  46. * @param RoleInterface $role A RoleInterface instance
  47. */
  48. public function addRole(RoleInterface $role)
  49. {
  50. $this->roles[] = $role;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getRoles()
  56. {
  57. return $this->roles;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function __toString()
  63. {
  64. if (!is_object($this->user)) {
  65. return (string) $this->user;
  66. } else {
  67. return $this->user->getUsername();
  68. }
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function isAuthenticated()
  74. {
  75. return $this->authenticated;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function setAuthenticated($authenticated)
  81. {
  82. $this->authenticated = (Boolean) $authenticated;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function getCredentials()
  88. {
  89. return $this->credentials;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function getUser()
  95. {
  96. return $this->user;
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function eraseCredentials()
  102. {
  103. if ($this->getCredentials() instanceof AccountInterface) {
  104. $this->getCredentials()->eraseCredentials();
  105. }
  106. if ($this->getUser() instanceof AccountInterface) {
  107. $this->getUser()->eraseCredentials();
  108. }
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function isImmutable()
  114. {
  115. return $this->immutable;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function setImmutable($value)
  121. {
  122. $this->immutable = (Boolean) $value;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function serialize()
  128. {
  129. // FIXME: don't serialize the user object, just the username (see ContextListener)
  130. //return serialize(array((string) $this, $this->credentials, $this->authenticated, $this->roles, $this->immutable));
  131. return serialize(array($this->user, $this->credentials, $this->authenticated, $this->roles, $this->immutable));
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function unserialize($serialized)
  137. {
  138. list($this->user, $this->credentials, $this->authenticated, $this->roles, $this->immutable) = unserialize($serialized);
  139. }
  140. }