User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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 Doctrine\Common\Collections\Collection;
  12. use Doctrine\Common\Collections\ArrayCollection;
  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, GroupableInterface
  20. {
  21. protected $id;
  22. /**
  23. * @var string
  24. */
  25. protected $username;
  26. /**
  27. * @var string
  28. */
  29. protected $usernameCanonical;
  30. /**
  31. * @var string
  32. */
  33. protected $email;
  34. /**
  35. * @var string
  36. */
  37. protected $emailCanonical;
  38. /**
  39. * @var boolean
  40. */
  41. protected $enabled;
  42. /**
  43. * The salt to use for hashing
  44. *
  45. * @var string
  46. */
  47. protected $salt;
  48. /**
  49. * Encrypted password. Must be persisted.
  50. *
  51. * @var string
  52. */
  53. protected $password;
  54. /**
  55. * Plain password. Used for model validation. Must not be persisted.
  56. *
  57. * @var string
  58. */
  59. protected $plainPassword;
  60. /**
  61. * @var \DateTime
  62. */
  63. protected $lastLogin;
  64. /**
  65. * Random string sent to the user email address in order to verify it
  66. *
  67. * @var string
  68. */
  69. protected $confirmationToken;
  70. /**
  71. * @var \DateTime
  72. */
  73. protected $passwordRequestedAt;
  74. /**
  75. * @var Collection
  76. */
  77. protected $groups;
  78. /**
  79. * @var boolean
  80. */
  81. protected $locked;
  82. /**
  83. * @var boolean
  84. */
  85. protected $expired;
  86. /**
  87. * @var \DateTime
  88. */
  89. protected $expiresAt;
  90. /**
  91. * @var array
  92. */
  93. protected $roles;
  94. /**
  95. * @var boolean
  96. */
  97. protected $credentialsExpired;
  98. /**
  99. * @var \DateTime
  100. */
  101. protected $credentialsExpireAt;
  102. public function __construct()
  103. {
  104. $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
  105. $this->enabled = false;
  106. $this->locked = false;
  107. $this->expired = false;
  108. $this->roles = array();
  109. $this->credentialsExpired = false;
  110. }
  111. public function addRole($role)
  112. {
  113. $role = strtoupper($role);
  114. if ($role === static::ROLE_DEFAULT) {
  115. return $this;
  116. }
  117. if (!in_array($role, $this->roles, true)) {
  118. $this->roles[] = $role;
  119. }
  120. return $this;
  121. }
  122. /**
  123. * Serializes the user.
  124. *
  125. * The serialized data have to contain the fields used during check for
  126. * changes and the id.
  127. *
  128. * @return string
  129. */
  130. public function serialize()
  131. {
  132. return serialize(array(
  133. $this->password,
  134. $this->salt,
  135. $this->usernameCanonical,
  136. $this->username,
  137. $this->expired,
  138. $this->locked,
  139. $this->credentialsExpired,
  140. $this->enabled,
  141. $this->id,
  142. $this->expiresAt,
  143. $this->credentialsExpireAt,
  144. $this->email,
  145. $this->emailCanonical,
  146. ));
  147. }
  148. /**
  149. * Unserializes the user.
  150. *
  151. * @param string $serialized
  152. */
  153. public function unserialize($serialized)
  154. {
  155. $data = unserialize($serialized);
  156. // add a few extra elements in the array to ensure that we have enough keys when unserializing
  157. // older data which does not include all properties.
  158. $data = array_merge($data, array_fill(0, 2, null));
  159. list(
  160. $this->password,
  161. $this->salt,
  162. $this->usernameCanonical,
  163. $this->username,
  164. $this->expired,
  165. $this->locked,
  166. $this->credentialsExpired,
  167. $this->enabled,
  168. $this->id,
  169. $this->expiresAt,
  170. $this->credentialsExpireAt,
  171. $this->email,
  172. $this->emailCanonical
  173. ) = $data;
  174. }
  175. /**
  176. * Removes sensitive data from the user.
  177. */
  178. public function eraseCredentials()
  179. {
  180. $this->plainPassword = null;
  181. }
  182. /**
  183. * Returns the user unique id.
  184. *
  185. * @return mixed
  186. */
  187. public function getId()
  188. {
  189. return $this->id;
  190. }
  191. public function getUsername()
  192. {
  193. return $this->username;
  194. }
  195. public function getUsernameCanonical()
  196. {
  197. return $this->usernameCanonical;
  198. }
  199. public function getSalt()
  200. {
  201. return $this->salt;
  202. }
  203. public function getEmail()
  204. {
  205. return $this->email;
  206. }
  207. public function getEmailCanonical()
  208. {
  209. return $this->emailCanonical;
  210. }
  211. /**
  212. * Gets the encrypted password.
  213. *
  214. * @return string
  215. */
  216. public function getPassword()
  217. {
  218. return $this->password;
  219. }
  220. public function getPlainPassword()
  221. {
  222. return $this->plainPassword;
  223. }
  224. /**
  225. * Gets the last login time.
  226. *
  227. * @return \DateTime
  228. */
  229. public function getLastLogin()
  230. {
  231. return $this->lastLogin;
  232. }
  233. public function getConfirmationToken()
  234. {
  235. return $this->confirmationToken;
  236. }
  237. /**
  238. * Returns the user roles
  239. *
  240. * @return array The roles
  241. */
  242. public function getRoles()
  243. {
  244. $roles = $this->roles;
  245. foreach ($this->getGroups() as $group) {
  246. $roles = array_merge($roles, $group->getRoles());
  247. }
  248. // we need to make sure to have at least one role
  249. $roles[] = static::ROLE_DEFAULT;
  250. return array_unique($roles);
  251. }
  252. /**
  253. * Never use this to check if this user has access to anything!
  254. *
  255. * Use the SecurityContext, or an implementation of AccessDecisionManager
  256. * instead, e.g.
  257. *
  258. * $securityContext->isGranted('ROLE_USER');
  259. *
  260. * @param string $role
  261. *
  262. * @return boolean
  263. */
  264. public function hasRole($role)
  265. {
  266. return in_array(strtoupper($role), $this->getRoles(), true);
  267. }
  268. public function isAccountNonExpired()
  269. {
  270. if (true === $this->expired) {
  271. return false;
  272. }
  273. if (null !== $this->expiresAt && $this->expiresAt->getTimestamp() < time()) {
  274. return false;
  275. }
  276. return true;
  277. }
  278. public function isAccountNonLocked()
  279. {
  280. return !$this->locked;
  281. }
  282. public function isCredentialsNonExpired()
  283. {
  284. if (true === $this->credentialsExpired) {
  285. return false;
  286. }
  287. if (null !== $this->credentialsExpireAt && $this->credentialsExpireAt->getTimestamp() < time()) {
  288. return false;
  289. }
  290. return true;
  291. }
  292. public function isCredentialsExpired()
  293. {
  294. return !$this->isCredentialsNonExpired();
  295. }
  296. public function isEnabled()
  297. {
  298. return $this->enabled;
  299. }
  300. public function isExpired()
  301. {
  302. return !$this->isAccountNonExpired();
  303. }
  304. public function isLocked()
  305. {
  306. return !$this->isAccountNonLocked();
  307. }
  308. public function isSuperAdmin()
  309. {
  310. return $this->hasRole(static::ROLE_SUPER_ADMIN);
  311. }
  312. public function isUser(UserInterface $user = null)
  313. {
  314. return null !== $user && $this->getId() === $user->getId();
  315. }
  316. public function removeRole($role)
  317. {
  318. if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
  319. unset($this->roles[$key]);
  320. $this->roles = array_values($this->roles);
  321. }
  322. return $this;
  323. }
  324. public function setUsername($username)
  325. {
  326. $this->username = $username;
  327. return $this;
  328. }
  329. public function setUsernameCanonical($usernameCanonical)
  330. {
  331. $this->usernameCanonical = $usernameCanonical;
  332. return $this;
  333. }
  334. /**
  335. * @param \DateTime $date
  336. *
  337. * @return User
  338. */
  339. public function setCredentialsExpireAt(\DateTime $date)
  340. {
  341. $this->credentialsExpireAt = $date;
  342. return $this;
  343. }
  344. /**
  345. * @param boolean $boolean
  346. *
  347. * @return User
  348. */
  349. public function setCredentialsExpired($boolean)
  350. {
  351. $this->credentialsExpired = $boolean;
  352. return $this;
  353. }
  354. public function setEmail($email)
  355. {
  356. $this->email = $email;
  357. return $this;
  358. }
  359. public function setEmailCanonical($emailCanonical)
  360. {
  361. $this->emailCanonical = $emailCanonical;
  362. return $this;
  363. }
  364. public function setEnabled($boolean)
  365. {
  366. $this->enabled = (Boolean) $boolean;
  367. return $this;
  368. }
  369. /**
  370. * Sets this user to expired.
  371. *
  372. * @param Boolean $boolean
  373. *
  374. * @return User
  375. */
  376. public function setExpired($boolean)
  377. {
  378. $this->expired = (Boolean) $boolean;
  379. return $this;
  380. }
  381. /**
  382. * @param \DateTime $date
  383. *
  384. * @return User
  385. */
  386. public function setExpiresAt(\DateTime $date)
  387. {
  388. $this->expiresAt = $date;
  389. return $this;
  390. }
  391. public function setPassword($password)
  392. {
  393. $this->password = $password;
  394. return $this;
  395. }
  396. public function setSuperAdmin($boolean)
  397. {
  398. if (true === $boolean) {
  399. $this->addRole(static::ROLE_SUPER_ADMIN);
  400. } else {
  401. $this->removeRole(static::ROLE_SUPER_ADMIN);
  402. }
  403. return $this;
  404. }
  405. public function setPlainPassword($password)
  406. {
  407. $this->plainPassword = $password;
  408. return $this;
  409. }
  410. public function setLastLogin(\DateTime $time)
  411. {
  412. $this->lastLogin = $time;
  413. return $this;
  414. }
  415. public function setLocked($boolean)
  416. {
  417. $this->locked = $boolean;
  418. return $this;
  419. }
  420. public function setConfirmationToken($confirmationToken)
  421. {
  422. $this->confirmationToken = $confirmationToken;
  423. return $this;
  424. }
  425. public function setPasswordRequestedAt(\DateTime $date = null)
  426. {
  427. $this->passwordRequestedAt = $date;
  428. return $this;
  429. }
  430. /**
  431. * Gets the timestamp that the user requested a password reset.
  432. *
  433. * @return null|\DateTime
  434. */
  435. public function getPasswordRequestedAt()
  436. {
  437. return $this->passwordRequestedAt;
  438. }
  439. public function isPasswordRequestNonExpired($ttl)
  440. {
  441. return $this->getPasswordRequestedAt() instanceof \DateTime &&
  442. $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
  443. }
  444. public function setRoles(array $roles)
  445. {
  446. $this->roles = array();
  447. foreach ($roles as $role) {
  448. $this->addRole($role);
  449. }
  450. return $this;
  451. }
  452. /**
  453. * Gets the groups granted to the user.
  454. *
  455. * @return Collection
  456. */
  457. public function getGroups()
  458. {
  459. return $this->groups ?: $this->groups = new ArrayCollection();
  460. }
  461. public function getGroupNames()
  462. {
  463. $names = array();
  464. foreach ($this->getGroups() as $group) {
  465. $names[] = $group->getName();
  466. }
  467. return $names;
  468. }
  469. public function hasGroup($name)
  470. {
  471. return in_array($name, $this->getGroupNames());
  472. }
  473. public function addGroup(GroupInterface $group)
  474. {
  475. if (!$this->getGroups()->contains($group)) {
  476. $this->getGroups()->add($group);
  477. }
  478. return $this;
  479. }
  480. public function removeGroup(GroupInterface $group)
  481. {
  482. if ($this->getGroups()->contains($group)) {
  483. $this->getGroups()->removeElement($group);
  484. }
  485. return $this;
  486. }
  487. public function __toString()
  488. {
  489. return (string) $this->getUsername();
  490. }
  491. }