PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php

https://bitbucket.org/tippycracker/autokraitis
PHP | 286 lines | 172 code | 26 blank | 88 comment | 13 complexity | 90c2033173c365914aebe334ecbdd23d MD5 | raw file
Possible License(s): BSD-2-Clause, GPL-2.0, GPL-3.0, BSD-3-Clause, Apache-2.0
  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\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\UserInterface;
  14. use Symfony\Component\Security\Core\User\AdvancedUserInterface;
  15. use Symfony\Component\Security\Core\User\EquatableInterface;
  16. /**
  17. * Base class for Token instances.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  21. */
  22. abstract class AbstractToken implements TokenInterface
  23. {
  24. private $user;
  25. private $roles = array();
  26. private $authenticated = false;
  27. private $attributes = array();
  28. /**
  29. * @param (RoleInterface|string)[] $roles An array of roles
  30. *
  31. * @throws \InvalidArgumentException
  32. */
  33. public function __construct(array $roles = array())
  34. {
  35. foreach ($roles as $role) {
  36. if (is_string($role)) {
  37. $role = new Role($role);
  38. } elseif (!$role instanceof RoleInterface) {
  39. throw new \InvalidArgumentException(sprintf('$roles must be an array of strings, or RoleInterface instances, but got %s.', gettype($role)));
  40. }
  41. $this->roles[] = $role;
  42. }
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getRoles()
  48. {
  49. return $this->roles;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getUsername()
  55. {
  56. if ($this->user instanceof UserInterface) {
  57. return $this->user->getUsername();
  58. }
  59. return (string) $this->user;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getUser()
  65. {
  66. return $this->user;
  67. }
  68. /**
  69. * Sets the user in the token.
  70. *
  71. * The user can be a UserInterface instance, or an object implementing
  72. * a __toString method or the username as a regular string.
  73. *
  74. * @param string|object $user The user
  75. *
  76. * @throws \InvalidArgumentException
  77. */
  78. public function setUser($user)
  79. {
  80. if (!($user instanceof UserInterface || (is_object($user) && method_exists($user, '__toString')) || is_string($user))) {
  81. throw new \InvalidArgumentException('$user must be an instanceof UserInterface, an object implementing a __toString method, or a primitive string.');
  82. }
  83. if (null === $this->user) {
  84. $changed = false;
  85. } elseif ($this->user instanceof UserInterface) {
  86. if (!$user instanceof UserInterface) {
  87. $changed = true;
  88. } else {
  89. $changed = $this->hasUserChanged($user);
  90. }
  91. } elseif ($user instanceof UserInterface) {
  92. $changed = true;
  93. } else {
  94. $changed = (string) $this->user !== (string) $user;
  95. }
  96. if ($changed) {
  97. $this->setAuthenticated(false);
  98. }
  99. $this->user = $user;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function isAuthenticated()
  105. {
  106. return $this->authenticated;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function setAuthenticated($authenticated)
  112. {
  113. $this->authenticated = (bool) $authenticated;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function eraseCredentials()
  119. {
  120. if ($this->getUser() instanceof UserInterface) {
  121. $this->getUser()->eraseCredentials();
  122. }
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function serialize()
  128. {
  129. return serialize(
  130. array(
  131. is_object($this->user) ? clone $this->user : $this->user,
  132. $this->authenticated,
  133. array_map(function ($role) { return clone $role; }, $this->roles),
  134. $this->attributes,
  135. )
  136. );
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function unserialize($serialized)
  142. {
  143. list($this->user, $this->authenticated, $this->roles, $this->attributes) = unserialize($serialized);
  144. }
  145. /**
  146. * Returns the token attributes.
  147. *
  148. * @return array The token attributes
  149. */
  150. public function getAttributes()
  151. {
  152. return $this->attributes;
  153. }
  154. /**
  155. * Sets the token attributes.
  156. *
  157. * @param array $attributes The token attributes
  158. */
  159. public function setAttributes(array $attributes)
  160. {
  161. $this->attributes = $attributes;
  162. }
  163. /**
  164. * Returns true if the attribute exists.
  165. *
  166. * @param string $name The attribute name
  167. *
  168. * @return bool true if the attribute exists, false otherwise
  169. */
  170. public function hasAttribute($name)
  171. {
  172. return array_key_exists($name, $this->attributes);
  173. }
  174. /**
  175. * Returns an attribute value.
  176. *
  177. * @param string $name The attribute name
  178. *
  179. * @return mixed The attribute value
  180. *
  181. * @throws \InvalidArgumentException When attribute doesn't exist for this token
  182. */
  183. public function getAttribute($name)
  184. {
  185. if (!array_key_exists($name, $this->attributes)) {
  186. throw new \InvalidArgumentException(sprintf('This token has no "%s" attribute.', $name));
  187. }
  188. return $this->attributes[$name];
  189. }
  190. /**
  191. * Sets an attribute.
  192. *
  193. * @param string $name The attribute name
  194. * @param mixed $value The attribute value
  195. */
  196. public function setAttribute($name, $value)
  197. {
  198. $this->attributes[$name] = $value;
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function __toString()
  204. {
  205. $class = get_class($this);
  206. $class = substr($class, strrpos($class, '\\') + 1);
  207. $roles = array();
  208. foreach ($this->roles as $role) {
  209. $roles[] = $role->getRole();
  210. }
  211. return sprintf('%s(user="%s", authenticated=%s, roles="%s")', $class, $this->getUsername(), json_encode($this->authenticated), implode(', ', $roles));
  212. }
  213. private function hasUserChanged(UserInterface $user)
  214. {
  215. if (!($this->user instanceof UserInterface)) {
  216. throw new \BadMethodCallException('Method "hasUserChanged" should be called when current user class is instance of "UserInterface".');
  217. }
  218. if ($this->user instanceof EquatableInterface) {
  219. return !(bool) $this->user->isEqualTo($user);
  220. }
  221. if ($this->user->getPassword() !== $user->getPassword()) {
  222. return true;
  223. }
  224. if ($this->user->getSalt() !== $user->getSalt()) {
  225. return true;
  226. }
  227. if ($this->user->getUsername() !== $user->getUsername()) {
  228. return true;
  229. }
  230. if ($this->user instanceof AdvancedUserInterface && $user instanceof AdvancedUserInterface) {
  231. if ($this->user->isAccountNonExpired() !== $user->isAccountNonExpired()) {
  232. return true;
  233. }
  234. if ($this->user->isAccountNonLocked() !== $user->isAccountNonLocked()) {
  235. return true;
  236. }
  237. if ($this->user->isCredentialsNonExpired() !== $user->isCredentialsNonExpired()) {
  238. return true;
  239. }
  240. if ($this->user->isEnabled() !== $user->isEnabled()) {
  241. return true;
  242. }
  243. } elseif ($this->user instanceof AdvancedUserInterface xor $user instanceof AdvancedUserInterface) {
  244. return true;
  245. }
  246. return false;
  247. }
  248. }