src/Entity/User.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  5. use Symfony\Component\Security\Core\User\AdvancedUserInterface;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. /**
  10. * User
  11. *
  12. * @ORM\Table(name="User")
  13. * @ORM\Entity(repositoryClass="App\Entity\UserRepository")
  14. * @UniqueEntity(
  15. * fields="email",
  16. * message="Cette adresse email est déjà utilisée.",
  17. * groups={"registration", "update"}
  18. * )
  19. */
  20. class User implements UserInterface, PasswordAuthenticatedUserInterface
  21. {
  22. public const ROLE_ADMIN = 'ROLE_ADMIN';
  23. public const ROLE_PRESTATAIRE = 'ROLE_PRESTATAIRE';
  24. public static function createWithId($id)
  25. {
  26. $instance = new self();
  27. $instance->id = $id;
  28. return $instance;
  29. }
  30. /**
  31. * @var integer
  32. *
  33. * @ORM\Column(name="id", type="integer")
  34. * @ORM\Id
  35. * @ORM\GeneratedValue(strategy="AUTO")
  36. */
  37. private $id;
  38. /**
  39. * @var string
  40. *
  41. * @ORM\Column(name="email", type="string", length=255, unique=true)
  42. * @Assert\Email(
  43. * message="Veuillez entrer une adresse email valide."
  44. * )
  45. * @Assert\NotBlank(
  46. * message="Veuillez renseigner ce champ.",
  47. * groups={"registration","request_reset", "update"}
  48. * )
  49. */
  50. private $email;
  51. /**
  52. * @var boolean
  53. *
  54. * @ORM\Column(name="is_email_valid", type="boolean")
  55. */
  56. private $emailValid;
  57. /**
  58. * @var string
  59. *
  60. * @ORM\Column(name="email_validation_code", type="string", length=255, nullable=true)
  61. */
  62. private $emailValidationCode;
  63. /**
  64. * @var string
  65. */
  66. private $rawEmailValidationCode;
  67. /**
  68. * @var string
  69. *
  70. * @ORM\Column(name="password", type="string", length=255)
  71. */
  72. private $password;
  73. /**
  74. * @var string
  75. * @Assert\NotBlank(
  76. * message="Veuillez renseigner ce champ.",
  77. * groups={"registration", "reset"}
  78. * )
  79. */
  80. private $rawPassword;
  81. /**
  82. * @var string
  83. *
  84. * @ORM\Column(name="reset_code", type="string", length=255, nullable=true)
  85. */
  86. private $resetCode;
  87. /**
  88. * @var string
  89. *
  90. * @ORM\Column(name="roles", type="string", type="simple_array")
  91. */
  92. private $roles;
  93. /**
  94. * @var Prestataire
  95. *
  96. * @ORM\OneToOne(targetEntity="Prestataire", mappedBy="user", cascade={"remove"})
  97. */
  98. private $prestataire;
  99. /**
  100. * @ORM\Column(type="boolean")
  101. */
  102. private $isVerified = false;
  103. public function __construct()
  104. {
  105. $this->roles = array(self::ROLE_PRESTATAIRE);
  106. $this->emailValid = false;
  107. }
  108. public function getUserIdentifier(): string
  109. {
  110. return (string) $this->id;
  111. }
  112. /**
  113. * Get id
  114. *
  115. * @return integer
  116. */
  117. public function getId()
  118. {
  119. return $this->id;
  120. }
  121. /**
  122. * Set email
  123. *
  124. * @param string $email
  125. * @return User
  126. */
  127. public function setEmail($email)
  128. {
  129. $this->email = $email;
  130. return $this;
  131. }
  132. /**
  133. * Get email
  134. *
  135. * @return string
  136. */
  137. public function getEmail()
  138. {
  139. return $this->email;
  140. }
  141. /**
  142. * @return boolean
  143. */
  144. public function isEmailValid()
  145. {
  146. return $this->emailValid;
  147. }
  148. /**
  149. * @param boolean $emailValid
  150. */
  151. public function setEmailValid($emailValid)
  152. {
  153. $this->emailValid = $emailValid;
  154. }
  155. /**
  156. * Set emailValidationCode
  157. *
  158. * @param string $emailValidationCode
  159. *
  160. * @return Prestataire
  161. */
  162. public function setEmailValidationCode($emailValidationCode)
  163. {
  164. $this->emailValidationCode = $emailValidationCode;
  165. return $this;
  166. }
  167. /**
  168. * Get emailValidationCode
  169. *
  170. * @return string
  171. */
  172. public function getEmailValidationCode()
  173. {
  174. return $this->emailValidationCode;
  175. }
  176. /**
  177. * @return string
  178. */
  179. public function getRawEmailValidationCode()
  180. {
  181. return $this->rawEmailValidationCode;
  182. }
  183. /**
  184. * @param string $rawEmailValidationCode
  185. */
  186. public function setRawEmailValidationCode($rawEmailValidationCode)
  187. {
  188. $this->rawEmailValidationCode = $rawEmailValidationCode;
  189. }
  190. /**
  191. * Returns the username used to authenticate the user.
  192. *
  193. * @return string The username
  194. */
  195. public function getUsername()
  196. {
  197. return $this->getEmail();
  198. }
  199. /**
  200. * Set pass
  201. *
  202. * @param string $password
  203. *
  204. * @return User
  205. */
  206. public function setPassword($password)
  207. {
  208. $this->password = $password;
  209. return $this;
  210. }
  211. /**
  212. * Get pass
  213. *
  214. * @return string|null
  215. */
  216. public function getPassword(): ?string
  217. {
  218. return $this->password;
  219. }
  220. /**
  221. * Set resetCode
  222. *
  223. * @param string $resetCode
  224. * @return User
  225. */
  226. public function setResetCode($resetCode)
  227. {
  228. $this->resetCode = $resetCode;
  229. return $this;
  230. }
  231. /**
  232. * Get resetCode
  233. *
  234. * @return string
  235. */
  236. public function getResetCode()
  237. {
  238. return $this->resetCode;
  239. }
  240. public function isGranted(string $role): bool
  241. {
  242. if (in_array($role, $this->getRoles())):
  243. return true;
  244. else: return false; endif;
  245. }
  246. /**
  247. * Set role
  248. *
  249. * @param string $roles
  250. *
  251. * @return User
  252. */
  253. public function setRoles($roles)
  254. {
  255. $this->roles = $roles;
  256. return $this;
  257. }
  258. /**
  259. * Get role
  260. *
  261. * @return array
  262. */
  263. public function getRoles()
  264. {
  265. return $this->roles;
  266. }
  267. public function hasRole($role)
  268. {
  269. return array_search($role, $this->getRoles()) !== false;
  270. }
  271. public function addRole($role)
  272. {
  273. $roles = $this->getRoles();
  274. $roles[] = $role;
  275. $this->setRoles(array_unique($roles));
  276. }
  277. public function removeRole($role)
  278. {
  279. $roles = $this->getRoles();
  280. $offset = array_search($role, $roles);
  281. if ($offset !== false) {
  282. unset($roles[$offset]);
  283. $this->setRoles($roles);
  284. }
  285. }
  286. public function isPrestataire()
  287. {
  288. return $this->hasRole(self::ROLE_PRESTATAIRE);
  289. }
  290. public function isAdmin()
  291. {
  292. return $this->hasRole(self::ROLE_ADMIN);
  293. }
  294. /**
  295. * @return Prestataire
  296. */
  297. public function getPrestataire()
  298. {
  299. return $this->prestataire;
  300. }
  301. /**
  302. * @param Prestataire $prestataire
  303. */
  304. public function setPrestataire($prestataire)
  305. {
  306. $this->prestataire = $prestataire;
  307. return $this;
  308. }
  309. /**
  310. * Checks whether the user's account has expired.
  311. *
  312. * Internally, if this method returns false, the authentication system
  313. * will throw an AccountExpiredException and prevent login.
  314. *
  315. * @return bool true if the user's account is non expired, false otherwise
  316. *
  317. * @see AccountExpiredException
  318. */
  319. public function isAccountNonExpired()
  320. {
  321. return true;
  322. }
  323. /**
  324. * Checks whether the user is locked.
  325. *
  326. * Internally, if this method returns false, the authentication system
  327. * will throw a LockedException and prevent login.
  328. *
  329. * @return bool true if the user is not locked, false otherwise
  330. *
  331. * @see LockedException
  332. */
  333. public function isAccountNonLocked()
  334. {
  335. return true;
  336. }
  337. /**
  338. * Checks whether the user's credentials (password) has expired.
  339. *
  340. * Internally, if this method returns false, the authentication system
  341. * will throw a CredentialsExpiredException and prevent login.
  342. *
  343. * @return bool true if the user's credentials are non expired, false otherwise
  344. *
  345. * @see CredentialsExpiredException
  346. */
  347. public function isCredentialsNonExpired()
  348. {
  349. return true;
  350. }
  351. /**
  352. * Checks whether the user is enabled.
  353. *
  354. * Internally, if this method returns false, the authentication system
  355. * will throw a DisabledException and prevent login.
  356. *
  357. * @return bool true if the user is enabled, false otherwise
  358. *
  359. * @see DisabledException
  360. */
  361. public function isEnabled()
  362. {
  363. if ($this->isPrestataire() && $this->getPrestataire()) {
  364. return $this->getPrestataire()->isValidated();
  365. }
  366. return true;
  367. }
  368. /**
  369. * Returns the salt that was originally used to encode the password.
  370. *
  371. * This can return null if the password was not encoded using a salt.
  372. *
  373. * @return string|null The salt
  374. */
  375. public function getSalt()
  376. {
  377. return $this->getPassword();
  378. }
  379. /**
  380. * Removes sensitive data from the user.
  381. *
  382. * This is important if, at any given point, sensitive information like
  383. * the plain-text password is stored on this object.
  384. */
  385. public function eraseCredentials()
  386. {
  387. }
  388. /**
  389. * Get emailValid
  390. *
  391. * @return boolean
  392. */
  393. public function getEmailValid()
  394. {
  395. return $this->emailValid;
  396. }
  397. public function getRawPassword()
  398. {
  399. return $this->rawPassword;
  400. }
  401. public function setRawPassword($password)
  402. {
  403. $this->rawPassword = $password;
  404. return $this;
  405. }
  406. public function __toString()
  407. {
  408. return $this->email;
  409. }
  410. public function __serialize(): array
  411. {
  412. return [
  413. 'id' => $this->id,
  414. 'email' => $this->email,
  415. 'password' => $this->password,
  416. ];
  417. }
  418. public function __unserialize(array $data): void
  419. {
  420. $this->id = $data['id'];
  421. $this->email = $data['email'];
  422. $this->password = $data['password'];
  423. }
  424. public function isVerified(): bool
  425. {
  426. return $this->isVerified;
  427. }
  428. public function setIsVerified(bool $isVerified): self
  429. {
  430. $this->isVerified = $isVerified;
  431. return $this;
  432. }
  433. }