src/Entity/User.php line 484

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Oberon\Presentation\Annotations\Presentation;
  5. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  6. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. /**
  9. * @Presentation(class="App\Presentation\UserPresentation")
  10. * User
  11. *
  12. * @ORM\Table("users")
  13. * @ORM\Entity
  14. * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  15. * @UniqueEntity(fields={"username"}, message="There is already an account with this username")
  16. */
  17. class User implements UserInterface, PasswordAuthenticatedUserInterface
  18. {
  19. const ROLE_DEFAULT = 'ROLE_USER';
  20. const ROLE_SUPER_ADMIN = 'ROLE_SUPER_ADMIN';
  21. /**
  22. * @var integer
  23. *
  24. * @ORM\Column(name="id", type="integer")
  25. * @ORM\Id
  26. * @ORM\GeneratedValue(strategy="AUTO")
  27. */
  28. protected $id;
  29. /**
  30. * @var bool
  31. *
  32. * @ORM\Column(name="survey_monkey_sent", type="boolean", nullable=true, options={"default"=false})
  33. */
  34. protected $surveyMonkeySent;
  35. /**
  36. * Get id
  37. *
  38. * @return integer
  39. */
  40. public function getId()
  41. {
  42. return $this->id;
  43. }
  44. /**
  45. * Get surveyMonkeySent
  46. *
  47. * @return bool
  48. */
  49. public function isSurveyMonkeySent()
  50. {
  51. return $this->surveyMonkeySent;
  52. }
  53. /**
  54. * @param bool $surveyMonkeySent
  55. */
  56. public function setSurveyMonkeySent($surveyMonkeySent)
  57. {
  58. $this->surveyMonkeySent = $surveyMonkeySent;
  59. }
  60. /**
  61. * @var string
  62. * @ORM\Column(type="string", length=180)
  63. */
  64. protected $username;
  65. /**
  66. * @var string
  67. * @ORM\Column(type="string", length=180, unique=true)
  68. */
  69. protected $usernameCanonical;
  70. /**
  71. * @var string
  72. * @ORM\Column(type="string", length=180)
  73. */
  74. protected $email;
  75. /**
  76. * @var string
  77. * @ORM\Column(type="string", length=180, unique=true)
  78. */
  79. protected $emailCanonical;
  80. /**
  81. * @var bool
  82. * @ORM\Column(type="boolean")
  83. */
  84. protected $enabled;
  85. /**
  86. * The salt to use for hashing.
  87. *
  88. * @var string
  89. * @ORM\Column(type="string", nullable=true)
  90. */
  91. protected $salt;
  92. /**
  93. * Encrypted password. Must be persisted.
  94. *
  95. * @var string
  96. * @ORM\Column(type="string", nullable=true)
  97. */
  98. protected $password;
  99. /**
  100. * Plain password. Used for model validation. Must not be persisted.
  101. *
  102. * @var string
  103. */
  104. protected $plainPassword;
  105. /**
  106. * @var \DateTime|null
  107. * @ORM\Column(type="datetime", nullable=true)
  108. */
  109. protected $lastLogin;
  110. /**
  111. * Random string sent to the user email address in order to verify it.
  112. *
  113. * @var string|null
  114. * @ORM\Column(type="string", length=180, nullable=true, unique=true)
  115. */
  116. protected $confirmationToken;
  117. /**
  118. * @var \DateTime|null
  119. * @ORM\Column(type="datetime", nullable=true)
  120. */
  121. protected $passwordRequestedAt;
  122. /**
  123. * @var array
  124. * @ORM\Column(type="array")
  125. */
  126. protected $roles;
  127. /**
  128. * @ORM\Column(type="boolean")
  129. */
  130. private $isVerified = false;
  131. /**
  132. * User constructor.
  133. */
  134. public function __construct()
  135. {
  136. $this->enabled = false;
  137. $this->roles = array();
  138. }
  139. /**
  140. * @return string
  141. */
  142. public function __toString()
  143. {
  144. return (string) $this->getUsername();
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function addRole($role)
  150. {
  151. $role = strtoupper($role);
  152. if ($role === static::ROLE_DEFAULT) {
  153. return $this;
  154. }
  155. if (!in_array($role, $this->roles, true)) {
  156. $this->roles[] = $role;
  157. }
  158. return $this;
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function serialize()
  164. {
  165. return serialize(array(
  166. $this->password,
  167. $this->salt,
  168. $this->usernameCanonical,
  169. $this->username,
  170. $this->enabled,
  171. $this->id,
  172. $this->email,
  173. $this->emailCanonical,
  174. ));
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. public function unserialize($serialized)
  180. {
  181. $data = unserialize($serialized);
  182. if (13 === count($data)) {
  183. // Unserializing a User object from 1.3.x
  184. unset($data[4], $data[5], $data[6], $data[9], $data[10]);
  185. $data = array_values($data);
  186. } elseif (11 === count($data)) {
  187. // Unserializing a User from a dev version somewhere between 2.0-alpha3 and 2.0-beta1
  188. unset($data[4], $data[7], $data[8]);
  189. $data = array_values($data);
  190. }
  191. list(
  192. $this->password,
  193. $this->salt,
  194. $this->usernameCanonical,
  195. $this->username,
  196. $this->enabled,
  197. $this->id,
  198. $this->email,
  199. $this->emailCanonical
  200. ) = $data;
  201. }
  202. /**
  203. * {@inheritdoc}
  204. */
  205. public function eraseCredentials()
  206. {
  207. $this->plainPassword = null;
  208. }
  209. /**
  210. * {@inheritdoc}
  211. */
  212. public function getUsername()
  213. {
  214. return $this->username;
  215. }
  216. /**
  217. * {@inheritdoc}
  218. */
  219. public function getUsernameCanonical()
  220. {
  221. return $this->usernameCanonical;
  222. }
  223. /**
  224. * {@inheritdoc}
  225. */
  226. public function getSalt()
  227. {
  228. return $this->salt;
  229. }
  230. /**
  231. * {@inheritdoc}
  232. */
  233. public function getEmail()
  234. {
  235. return $this->email;
  236. }
  237. /**
  238. * {@inheritdoc}
  239. */
  240. public function getEmailCanonical()
  241. {
  242. return $this->emailCanonical;
  243. }
  244. /**
  245. * {@inheritdoc}
  246. */
  247. public function getPassword(): string
  248. {
  249. return $this->password;
  250. }
  251. /**
  252. * {@inheritdoc}
  253. */
  254. public function getPlainPassword()
  255. {
  256. return $this->plainPassword;
  257. }
  258. /**
  259. * Gets the last login time.
  260. *
  261. * @return \DateTime|null
  262. */
  263. public function getLastLogin()
  264. {
  265. return $this->lastLogin;
  266. }
  267. /**
  268. * {@inheritdoc}
  269. */
  270. public function getConfirmationToken()
  271. {
  272. return $this->confirmationToken;
  273. }
  274. /**
  275. * {@inheritdoc}
  276. */
  277. public function getRoles()
  278. {
  279. $roles = $this->roles;
  280. // we need to make sure to have at least one role
  281. $roles[] = static::ROLE_DEFAULT;
  282. return array_unique($roles);
  283. }
  284. /**
  285. * {@inheritdoc}
  286. */
  287. public function hasRole($role)
  288. {
  289. return in_array(strtoupper($role), $this->getRoles(), true);
  290. }
  291. /**
  292. * {@inheritdoc}
  293. */
  294. public function isAccountNonExpired()
  295. {
  296. return true;
  297. }
  298. /**
  299. * {@inheritdoc}
  300. */
  301. public function isAccountNonLocked()
  302. {
  303. return true;
  304. }
  305. /**
  306. * {@inheritdoc}
  307. */
  308. public function isCredentialsNonExpired()
  309. {
  310. return true;
  311. }
  312. public function isEnabled()
  313. {
  314. return $this->enabled;
  315. }
  316. /**
  317. * {@inheritdoc}
  318. */
  319. public function isSuperAdmin()
  320. {
  321. return $this->hasRole(static::ROLE_SUPER_ADMIN);
  322. }
  323. /**
  324. * {@inheritdoc}
  325. */
  326. public function removeRole($role)
  327. {
  328. if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
  329. unset($this->roles[$key]);
  330. $this->roles = array_values($this->roles);
  331. }
  332. return $this;
  333. }
  334. /**
  335. * {@inheritdoc}
  336. */
  337. public function setUsername($username)
  338. {
  339. $this->username = $username;
  340. return $this;
  341. }
  342. /**
  343. * {@inheritdoc}
  344. */
  345. public function setUsernameCanonical($usernameCanonical)
  346. {
  347. $this->usernameCanonical = $usernameCanonical;
  348. return $this;
  349. }
  350. /**
  351. * {@inheritdoc}
  352. */
  353. public function setSalt($salt)
  354. {
  355. $this->salt = $salt;
  356. return $this;
  357. }
  358. /**
  359. * {@inheritdoc}
  360. */
  361. public function setEmail($email)
  362. {
  363. $this->email = $email;
  364. return $this;
  365. }
  366. /**
  367. * {@inheritdoc}
  368. */
  369. public function setEmailCanonical($emailCanonical)
  370. {
  371. $this->emailCanonical = $emailCanonical;
  372. return $this;
  373. }
  374. /**
  375. * {@inheritdoc}
  376. */
  377. public function setEnabled($boolean)
  378. {
  379. $this->enabled = (bool) $boolean;
  380. return $this;
  381. }
  382. /**
  383. * {@inheritdoc}
  384. */
  385. public function setPassword($password)
  386. {
  387. $this->password = $password;
  388. return $this;
  389. }
  390. /**
  391. * {@inheritdoc}
  392. */
  393. public function setSuperAdmin($boolean)
  394. {
  395. if (true === $boolean) {
  396. $this->addRole(static::ROLE_SUPER_ADMIN);
  397. } else {
  398. $this->removeRole(static::ROLE_SUPER_ADMIN);
  399. }
  400. return $this;
  401. }
  402. /**
  403. * {@inheritdoc}
  404. */
  405. public function setPlainPassword($password)
  406. {
  407. $this->plainPassword = $password;
  408. return $this;
  409. }
  410. /**
  411. * {@inheritdoc}
  412. */
  413. public function setLastLogin(\DateTime $time = null)
  414. {
  415. $this->lastLogin = $time;
  416. return $this;
  417. }
  418. /**
  419. * {@inheritdoc}
  420. */
  421. public function setConfirmationToken($confirmationToken)
  422. {
  423. $this->confirmationToken = $confirmationToken;
  424. return $this;
  425. }
  426. /**
  427. * {@inheritdoc}
  428. */
  429. public function setPasswordRequestedAt(\DateTime $date = null)
  430. {
  431. $this->passwordRequestedAt = $date;
  432. return $this;
  433. }
  434. /**
  435. * Gets the timestamp that the user requested a password reset.
  436. *
  437. * @return null|\DateTime
  438. */
  439. public function getPasswordRequestedAt()
  440. {
  441. return $this->passwordRequestedAt;
  442. }
  443. /**
  444. * {@inheritdoc}
  445. */
  446. public function isPasswordRequestNonExpired($ttl)
  447. {
  448. return $this->getPasswordRequestedAt() instanceof \DateTime &&
  449. $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
  450. }
  451. /**
  452. * {@inheritdoc}
  453. */
  454. public function setRoles(array $roles)
  455. {
  456. $this->roles = array();
  457. foreach ($roles as $role) {
  458. $this->addRole($role);
  459. }
  460. return $this;
  461. }
  462. public function getSurveyMonkeySent(): ?bool
  463. {
  464. return $this->surveyMonkeySent;
  465. }
  466. public function getEnabled(): ?bool
  467. {
  468. return $this->enabled;
  469. }
  470. public function isVerified(): bool
  471. {
  472. return $this->isVerified;
  473. }
  474. public function setIsVerified(bool $isVerified): self
  475. {
  476. $this->isVerified = $isVerified;
  477. return $this;
  478. }
  479. }