/vendor_full/symfony/src/Symfony/Component/Security/Core/Authentication/Token/Token.php

https://github.com/l3l0/BehatExamples · PHP · 269 lines · 136 code · 35 blank · 98 comment · 15 complexity · 31897d51850d2554379fa7df51acaeab MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Core\Authentication\Token;
  11. use Symfony\Component\Security\Core\Role\RoleInterface;
  12. use Symfony\Component\Security\Core\Role\Role;
  13. use Symfony\Component\Security\Core\User\AccountInterface;
  14. /**
  15. * Base class for Token instances.
  16. *
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  19. */
  20. abstract class Token implements TokenInterface
  21. {
  22. protected $roles;
  23. protected $authenticated;
  24. protected $user;
  25. protected $credentials;
  26. protected $immutable;
  27. protected $providerKey;
  28. protected $attributes;
  29. /**
  30. * Constructor.
  31. *
  32. * @param Role[] $roles An array of roles
  33. */
  34. public function __construct(array $roles = array())
  35. {
  36. $this->setRoles($roles);
  37. $this->authenticated = false;
  38. $this->immutable = false;
  39. $this->attributes = array();
  40. }
  41. /**
  42. * Adds a Role to the token.
  43. *
  44. * @param RoleInterface $role A RoleInterface instance
  45. */
  46. public function addRole(RoleInterface $role)
  47. {
  48. if ($this->immutable) {
  49. throw new \LogicException('This token is considered immutable.');
  50. }
  51. $this->roles[] = $role;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getRoles()
  57. {
  58. return $this->roles;
  59. }
  60. /**
  61. * {@inheritDoc}
  62. */
  63. public function setRoles(array $roles)
  64. {
  65. $this->roles = array();
  66. foreach ($roles as $role) {
  67. if (is_string($role)) {
  68. $role = new Role($role);
  69. }
  70. $this->addRole($role);
  71. }
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function __toString()
  77. {
  78. if ($this->user instanceof AccountInterface) {
  79. return $this->user->getUsername();
  80. }
  81. return (string) $this->user;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function isAuthenticated()
  87. {
  88. return $this->authenticated;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function setAuthenticated($authenticated)
  94. {
  95. if ($this->immutable) {
  96. throw new \LogicException('This token is considered immutable.');
  97. }
  98. $this->authenticated = (Boolean) $authenticated;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function getCredentials()
  104. {
  105. return $this->credentials;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function getUser()
  111. {
  112. return $this->user;
  113. }
  114. /**
  115. * {@inheritDoc}
  116. */
  117. public function setUser($user)
  118. {
  119. if ($this->immutable) {
  120. throw new \LogicException('This token is considered immutable.');
  121. }
  122. if (!is_string($user) && !is_object($user)) {
  123. throw new \InvalidArgumentException('$user must be an object, or a primitive string.');
  124. } else if (is_object($user) && !$user instanceof AccountInterface && !method_exists($user, '__toString')) {
  125. throw new \InvalidArgumentException('If $user is an object, it must implement __toString().');
  126. }
  127. $this->user = $user;
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function eraseCredentials()
  133. {
  134. if ($this->immutable) {
  135. throw new \LogicException('This token is considered immutable.');
  136. }
  137. if ($this->getCredentials() instanceof AccountInterface) {
  138. $this->getCredentials()->eraseCredentials();
  139. }
  140. if ($this->getUser() instanceof AccountInterface) {
  141. $this->getUser()->eraseCredentials();
  142. }
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function isImmutable()
  148. {
  149. return $this->immutable;
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function setImmutable()
  155. {
  156. $this->immutable = true;
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function getProviderKey()
  162. {
  163. return $this->providerKey;
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function serialize()
  169. {
  170. return serialize(array($this->user, $this->credentials, $this->authenticated, $this->roles, $this->immutable, $this->providerKey, $this->attributes));
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function unserialize($serialized)
  176. {
  177. list($this->user, $this->credentials, $this->authenticated, $this->roles, $this->immutable, $this->providerKey, $this->attributes) = unserialize($serialized);
  178. }
  179. /**
  180. * Returns the token attributes.
  181. *
  182. * @return array The token attributes
  183. */
  184. public function getAttributes()
  185. {
  186. return $this->attributes;
  187. }
  188. /**
  189. * Sets the token attributes.
  190. *
  191. * @param array $attributes The token attributes
  192. */
  193. public function setAttributes(array $attributes)
  194. {
  195. $this->attributes = $attributes;
  196. }
  197. /**
  198. * Returns true if the attribute exists.
  199. *
  200. * @param string $name The attribute name
  201. *
  202. * @return Boolean true if the attribute exists, false otherwise
  203. */
  204. public function hasAttribute($name)
  205. {
  206. return array_key_exists($name, $this->attributes);
  207. }
  208. /**
  209. * Returns a attribute value.
  210. *
  211. * @param string $name The attribute name
  212. *
  213. * @return mixed The attribute value
  214. *
  215. * @throws \InvalidArgumentException When attribute doesn't exist for this token
  216. */
  217. public function getAttribute($name)
  218. {
  219. if (!array_key_exists($name, $this->attributes)) {
  220. throw new \InvalidArgumentException(sprintf('This token has no "%s" attribute.', $name));
  221. }
  222. return $this->attributes[$name];
  223. }
  224. /**
  225. * Sets a attribute.
  226. *
  227. * @param string $name The attribute name
  228. * @param mixed $value The attribute value
  229. */
  230. public function setAttribute($name, $value)
  231. {
  232. $this->attributes[$name] = $value;
  233. }
  234. }