PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/friendsofsymfony/user-bundle/Model/User.php

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