User.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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\User;
  11. /**
  12. * User is the user implementation used by the in-memory user provider.
  13. *
  14. * This should not be used for anything else.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. final class User implements UserInterface, EquatableInterface
  19. {
  20. private $username;
  21. private $password;
  22. private $enabled;
  23. private $accountNonExpired;
  24. private $credentialsNonExpired;
  25. private $accountNonLocked;
  26. private $roles;
  27. private $extraFields;
  28. public function __construct(?string $username, ?string $password, array $roles = [], bool $enabled = true, bool $userNonExpired = true, bool $credentialsNonExpired = true, bool $userNonLocked = true, array $extraFields = [])
  29. {
  30. if ('' === $username || null === $username) {
  31. throw new \InvalidArgumentException('The username cannot be empty.');
  32. }
  33. $this->username = $username;
  34. $this->password = $password;
  35. $this->enabled = $enabled;
  36. $this->accountNonExpired = $userNonExpired;
  37. $this->credentialsNonExpired = $credentialsNonExpired;
  38. $this->accountNonLocked = $userNonLocked;
  39. $this->roles = $roles;
  40. $this->extraFields = $extraFields;
  41. }
  42. public function __toString(): string
  43. {
  44. return $this->getUsername();
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getRoles(): array
  50. {
  51. return $this->roles;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getPassword(): ?string
  57. {
  58. return $this->password;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function getSalt(): ?string
  64. {
  65. return null;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getUsername(): string
  71. {
  72. return $this->username;
  73. }
  74. /**
  75. * Checks whether the user's account has expired.
  76. *
  77. * Internally, if this method returns false, the authentication system
  78. * will throw an AccountExpiredException and prevent login.
  79. *
  80. * @return bool true if the user's account is non expired, false otherwise
  81. *
  82. * @see AccountExpiredException
  83. */
  84. public function isAccountNonExpired(): bool
  85. {
  86. return $this->accountNonExpired;
  87. }
  88. /**
  89. * Checks whether the user is locked.
  90. *
  91. * Internally, if this method returns false, the authentication system
  92. * will throw a LockedException and prevent login.
  93. *
  94. * @return bool true if the user is not locked, false otherwise
  95. *
  96. * @see LockedException
  97. */
  98. public function isAccountNonLocked(): bool
  99. {
  100. return $this->accountNonLocked;
  101. }
  102. /**
  103. * Checks whether the user's credentials (password) has expired.
  104. *
  105. * Internally, if this method returns false, the authentication system
  106. * will throw a CredentialsExpiredException and prevent login.
  107. *
  108. * @return bool true if the user's credentials are non expired, false otherwise
  109. *
  110. * @see CredentialsExpiredException
  111. */
  112. public function isCredentialsNonExpired(): bool
  113. {
  114. return $this->credentialsNonExpired;
  115. }
  116. /**
  117. * Checks whether the user is enabled.
  118. *
  119. * Internally, if this method returns false, the authentication system
  120. * will throw a DisabledException and prevent login.
  121. *
  122. * @return bool true if the user is enabled, false otherwise
  123. *
  124. * @see DisabledException
  125. */
  126. public function isEnabled(): bool
  127. {
  128. return $this->enabled;
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function eraseCredentials()
  134. {
  135. }
  136. public function getExtraFields(): array
  137. {
  138. return $this->extraFields;
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function isEqualTo(UserInterface $user): bool
  144. {
  145. if (!$user instanceof self) {
  146. return false;
  147. }
  148. if ($this->getPassword() !== $user->getPassword()) {
  149. return false;
  150. }
  151. if ($this->getSalt() !== $user->getSalt()) {
  152. return false;
  153. }
  154. $currentRoles = array_map('strval', (array) $this->getRoles());
  155. $newRoles = array_map('strval', (array) $user->getRoles());
  156. $rolesChanged = \count($currentRoles) !== \count($newRoles) || \count($currentRoles) !== \count(array_intersect($currentRoles, $newRoles));
  157. if ($rolesChanged) {
  158. return false;
  159. }
  160. if ($this->getUsername() !== $user->getUsername()) {
  161. return false;
  162. }
  163. if ($this->isAccountNonExpired() !== $user->isAccountNonExpired()) {
  164. return false;
  165. }
  166. if ($this->isAccountNonLocked() !== $user->isAccountNonLocked()) {
  167. return false;
  168. }
  169. if ($this->isCredentialsNonExpired() !== $user->isCredentialsNonExpired()) {
  170. return false;
  171. }
  172. if ($this->isEnabled() !== $user->isEnabled()) {
  173. return false;
  174. }
  175. return true;
  176. }
  177. public function setPassword(string $password)
  178. {
  179. $this->password = $password;
  180. }
  181. }