vendor/friendsofsymfony/user-bundle/src/Model/User.php line 23

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\UserBundle\Model;
  11. use Symfony\Component\Security\Core\User\EquatableInterface;
  12. use Symfony\Component\Security\Core\User\UserInterface as BaseUserInterface;
  13. /**
  14. * Storage agnostic user object.
  15. *
  16. * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  17. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  18. */
  19. abstract class User implements UserInterface, EquatableInterface, \Serializable
  20. {
  21. /**
  22. * @var mixed
  23. */
  24. protected $id;
  25. /**
  26. * @var string
  27. */
  28. protected $username;
  29. /**
  30. * @var string
  31. */
  32. protected $usernameCanonical;
  33. /**
  34. * @var string
  35. */
  36. protected $email;
  37. /**
  38. * @var string
  39. */
  40. protected $emailCanonical;
  41. /**
  42. * @var bool
  43. */
  44. protected $enabled;
  45. /**
  46. * The salt to use for hashing.
  47. *
  48. * @var string|null
  49. */
  50. protected $salt;
  51. /**
  52. * Encrypted password. Must be persisted.
  53. *
  54. * @var string
  55. */
  56. protected $password;
  57. /**
  58. * Plain password. Used for model validation. Must not be persisted.
  59. *
  60. * @var string|null
  61. */
  62. protected $plainPassword;
  63. /**
  64. * @var \DateTime|null
  65. */
  66. protected $lastLogin;
  67. /**
  68. * Random string sent to the user email address in order to verify it.
  69. *
  70. * @var string|null
  71. */
  72. protected $confirmationToken;
  73. /**
  74. * @var \DateTime|null
  75. */
  76. protected $passwordRequestedAt;
  77. /**
  78. * @var string[]
  79. */
  80. protected $roles;
  81. /**
  82. * User constructor.
  83. */
  84. public function __construct()
  85. {
  86. $this->enabled = false;
  87. $this->roles = [];
  88. }
  89. /**
  90. * @return string
  91. */
  92. public function __toString()
  93. {
  94. return (string) $this->getUsername();
  95. }
  96. public function __serialize(): array
  97. {
  98. return [
  99. $this->password,
  100. $this->salt,
  101. $this->usernameCanonical,
  102. $this->username,
  103. $this->enabled,
  104. $this->id,
  105. $this->email,
  106. $this->emailCanonical,
  107. ];
  108. }
  109. /**
  110. * @param list<mixed> $data
  111. */
  112. public function __unserialize(array $data): void
  113. {
  114. if (13 === count($data)) {
  115. // Unserializing a User object from 1.3.x
  116. unset($data[4], $data[5], $data[6], $data[9], $data[10]);
  117. $data = array_values($data);
  118. } elseif (11 === count($data)) {
  119. // Unserializing a User from a dev version somewhere between 2.0-alpha3 and 2.0-beta1
  120. unset($data[4], $data[7], $data[8]);
  121. $data = array_values($data);
  122. }
  123. [
  124. $this->password,
  125. $this->salt,
  126. $this->usernameCanonical,
  127. $this->username,
  128. $this->enabled,
  129. $this->id,
  130. $this->email,
  131. $this->emailCanonical
  132. ] = $data;
  133. }
  134. /**
  135. * @internal
  136. */
  137. final public function serialize()
  138. {
  139. return serialize($this->__serialize());
  140. }
  141. /**
  142. * @internal
  143. */
  144. final public function unserialize($serialized)
  145. {
  146. $this->__unserialize(unserialize($serialized));
  147. }
  148. public function addRole($role)
  149. {
  150. $role = strtoupper($role);
  151. if ($role === static::ROLE_DEFAULT) {
  152. return $this;
  153. }
  154. if (!in_array($role, $this->roles, true)) {
  155. $this->roles[] = $role;
  156. }
  157. return $this;
  158. }
  159. /**
  160. * @return void
  161. */
  162. public function eraseCredentials()
  163. {
  164. $this->plainPassword = null;
  165. }
  166. public function getId()
  167. {
  168. return $this->id;
  169. }
  170. public function getUserIdentifier(): string
  171. {
  172. return $this->username;
  173. }
  174. /**
  175. * @return string
  176. */
  177. public function getUsername()
  178. {
  179. return $this->username;
  180. }
  181. public function getUsernameCanonical()
  182. {
  183. return $this->usernameCanonical;
  184. }
  185. public function getSalt(): ?string
  186. {
  187. return $this->salt;
  188. }
  189. public function getEmail()
  190. {
  191. return $this->email;
  192. }
  193. public function getEmailCanonical()
  194. {
  195. return $this->emailCanonical;
  196. }
  197. public function getPassword(): ?string
  198. {
  199. return $this->password;
  200. }
  201. public function getPlainPassword()
  202. {
  203. return $this->plainPassword;
  204. }
  205. /**
  206. * Gets the last login time.
  207. *
  208. * @return \DateTime|null
  209. */
  210. public function getLastLogin()
  211. {
  212. return $this->lastLogin;
  213. }
  214. public function getConfirmationToken()
  215. {
  216. return $this->confirmationToken;
  217. }
  218. public function getRoles(): array
  219. {
  220. $roles = $this->roles;
  221. // we need to make sure to have at least one role
  222. $roles[] = static::ROLE_DEFAULT;
  223. return array_values(array_unique($roles));
  224. }
  225. public function hasRole($role)
  226. {
  227. return in_array(strtoupper($role), $this->getRoles(), true);
  228. }
  229. public function isEnabled()
  230. {
  231. return $this->enabled;
  232. }
  233. public function isSuperAdmin()
  234. {
  235. return $this->hasRole(static::ROLE_SUPER_ADMIN);
  236. }
  237. public function removeRole($role)
  238. {
  239. if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
  240. unset($this->roles[$key]);
  241. $this->roles = array_values($this->roles);
  242. }
  243. return $this;
  244. }
  245. public function setUsername($username)
  246. {
  247. $this->username = $username;
  248. return $this;
  249. }
  250. public function setUsernameCanonical($usernameCanonical)
  251. {
  252. $this->usernameCanonical = $usernameCanonical;
  253. return $this;
  254. }
  255. public function setSalt($salt)
  256. {
  257. $this->salt = $salt;
  258. return $this;
  259. }
  260. public function setEmail($email)
  261. {
  262. $this->email = $email;
  263. return $this;
  264. }
  265. public function setEmailCanonical($emailCanonical)
  266. {
  267. $this->emailCanonical = $emailCanonical;
  268. return $this;
  269. }
  270. public function setEnabled($boolean)
  271. {
  272. $this->enabled = (bool) $boolean;
  273. return $this;
  274. }
  275. public function setPassword($password)
  276. {
  277. $this->password = $password;
  278. return $this;
  279. }
  280. public function setSuperAdmin($boolean)
  281. {
  282. if (true === $boolean) {
  283. $this->addRole(static::ROLE_SUPER_ADMIN);
  284. } else {
  285. $this->removeRole(static::ROLE_SUPER_ADMIN);
  286. }
  287. return $this;
  288. }
  289. public function setPlainPassword($password)
  290. {
  291. $this->plainPassword = $password;
  292. return $this;
  293. }
  294. public function setLastLogin(?\DateTime $time = null)
  295. {
  296. $this->lastLogin = $time;
  297. return $this;
  298. }
  299. public function setConfirmationToken($confirmationToken)
  300. {
  301. $this->confirmationToken = $confirmationToken;
  302. return $this;
  303. }
  304. public function setPasswordRequestedAt(?\DateTime $date = null)
  305. {
  306. $this->passwordRequestedAt = $date;
  307. return $this;
  308. }
  309. /**
  310. * Gets the timestamp that the user requested a password reset.
  311. *
  312. * @return \DateTime|null
  313. */
  314. public function getPasswordRequestedAt()
  315. {
  316. return $this->passwordRequestedAt;
  317. }
  318. public function isPasswordRequestNonExpired($ttl)
  319. {
  320. return $this->getPasswordRequestedAt() instanceof \DateTime
  321. && $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
  322. }
  323. public function setRoles(array $roles)
  324. {
  325. $this->roles = [];
  326. foreach ($roles as $role) {
  327. $this->addRole($role);
  328. }
  329. return $this;
  330. }
  331. public function isEqualTo(BaseUserInterface $user): bool
  332. {
  333. if (!$user instanceof self) {
  334. return false;
  335. }
  336. if ($this->password !== $user->getPassword()) {
  337. return false;
  338. }
  339. if ($this->salt !== $user->getSalt()) {
  340. return false;
  341. }
  342. if ($this->username !== $user->getUsername()) {
  343. return false;
  344. }
  345. return true;
  346. }
  347. }