src/Entity/User.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use App\Repository\UserRepository;
  5. use Doctrine\DBAL\Types\Types;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Gedmo\Mapping\Annotation as Gedmo;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. /**
  11. * @ORM\Entity(repositoryClass=UserRepository::class)
  12. *
  13. * @ORM\Table(name="User",indexes={@ORM\Index(name="user_tokens", columns={"token"})})
  14. *
  15. * @ORM\HasLifecycleCallbacks
  16. */
  17. class User implements UserInterface, PasswordAuthenticatedUserInterface
  18. {
  19. /**
  20. * @ORM\Id
  21. *
  22. * @ORM\GeneratedValue
  23. *
  24. * @ORM\Column(type="integer")
  25. */
  26. private $id;
  27. /**
  28. * @ORM\Column(type="string", length=180, unique=true)
  29. */
  30. private $username;
  31. /**
  32. * @ORM\Column(type="string", length=180, unique=true)
  33. */
  34. protected $email;
  35. /**
  36. * @ORM\Column(type="boolean", name="enabled", options={"default":"1"})
  37. */
  38. protected $enabled = true;
  39. /**
  40. * The salt to use for hashing.
  41. *
  42. * @ORM\Column(type="string", nullable=true)
  43. */
  44. protected $salt;
  45. /**
  46. * Plain password. Used for model validation. Must not be persisted.
  47. *
  48. * @var string
  49. */
  50. protected $plainPassword;
  51. /**
  52. * @ORM\Column(type="datetime", name="last_login", nullable=true)
  53. */
  54. protected $lastLogin;
  55. /**
  56. * Random string sent to the user email address in order to verify it.
  57. *
  58. * @ORM\Column(type="string", length=180, nullable=true)
  59. */
  60. protected $confirmationToken;
  61. /**
  62. * @ORM\Column(type="datetime", name="password_requested_at", nullable=true)
  63. */
  64. protected $passwordRequestedAt;
  65. /**
  66. * @ORM\OneToOne(targetEntity="Structure", mappedBy="user")
  67. *
  68. **/
  69. private $structure;
  70. /**
  71. * @ORM\ManyToOne(targetEntity="Region", inversedBy="users")
  72. *
  73. * @ORM\JoinColumn(name="region_id", referencedColumnName="id", nullable=true)
  74. */
  75. private $region;
  76. /** @ORM\Column(type="boolean", name="is_admin") */
  77. private $isAdmin = false;
  78. /** @ORM\Column(type="boolean", name="is_super_admin") */
  79. private $isSuperAdmin = false;
  80. /**
  81. * @ORM\Column(type="string", length=36, nullable=true)
  82. */
  83. private $token;
  84. /**
  85. * @ORM\Column(type="datetime", name="created_at")
  86. *
  87. * @Gedmo\Timestampable(on="create")
  88. */
  89. private \DateTime $createdAt;
  90. /**
  91. * @ORM\Column(type="datetime", name="valideChart_at", nullable=true)
  92. */
  93. private $valideChartAt;
  94. /**
  95. * @ORM\Column(type="datetime", name="updated_at")
  96. *
  97. * @Gedmo\Timestampable(on="update")
  98. */
  99. private \DateTime $updatedAt;
  100. /**
  101. * @ORM\Column(type="array")
  102. */
  103. private $roles = [];
  104. /**
  105. * @var string The hashed password
  106. *
  107. * @ORM\Column(type="string")
  108. */
  109. private $password;
  110. /**
  111. * Construct.
  112. */
  113. public function __construct()
  114. {
  115. // parent::__construct();
  116. $this->createdAt = new \DateTime();
  117. $this->updatedAt = new \DateTime();
  118. }
  119. #[\Override]
  120. public function __toString(): string
  121. {
  122. return $this->getStructure() ? $this->getStructure()->getNom() : $this->getUsername();
  123. }
  124. public function getId(): ?int
  125. {
  126. return $this->id;
  127. }
  128. /**
  129. * @deprecated since Symfony 5.3, use getUserIdentifier instead
  130. */
  131. #[\Override]
  132. public function getUsername(): string
  133. {
  134. return (string) $this->username;
  135. }
  136. public function setUsername(string $username): self
  137. {
  138. $this->username = $username;
  139. return $this;
  140. }
  141. /**
  142. * A visual identifier that represents this user.
  143. *
  144. * @see UserInterface
  145. */
  146. public function getUserIdentifier(): string
  147. {
  148. return (string) $this->username;
  149. }
  150. /**
  151. * @see UserInterface
  152. */
  153. #[\Override]
  154. public function getRoles(): array
  155. {
  156. $roles = $this->roles;
  157. // guarantee every user at least has ROLE_USER
  158. $roles[] = 'ROLE_USER';
  159. return array_unique($roles);
  160. }
  161. public function setRoles(array $roles): self
  162. {
  163. $this->roles = $roles;
  164. return $this;
  165. }
  166. /**
  167. * @see PasswordAuthenticatedUserInterface
  168. */
  169. #[\Override]
  170. public function getPassword(): string
  171. {
  172. return $this->password;
  173. }
  174. public function setPassword(string $password): self
  175. {
  176. $this->password = $password;
  177. return $this;
  178. }
  179. /**
  180. * Returning a salt is only needed, if you are not using a modern
  181. * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  182. *
  183. * @see UserInterface
  184. */
  185. #[\Override]
  186. public function getSalt(): ?string
  187. {
  188. return null;
  189. }
  190. /**
  191. * @see UserInterface
  192. */
  193. #[\Override]
  194. public function eraseCredentials(): void
  195. {
  196. // If you store any temporary, sensitive data on the user, clear it here
  197. // $this->plainPassword = null;
  198. }
  199. /**
  200. * Set isAdmin.
  201. *
  202. * @param bool $isAdmin
  203. */
  204. public function setIsAdmin($isAdmin): static
  205. {
  206. $this->isAdmin = $isAdmin;
  207. return $this;
  208. }
  209. /**
  210. * Get isAdmin.
  211. *
  212. * @return bool
  213. */
  214. public function getIsAdmin()
  215. {
  216. return $this->isAdmin;
  217. }
  218. /**
  219. * Set isSuperAdmin.
  220. *
  221. * @param bool $isSuperAdmin
  222. */
  223. public function setIsSuperAdmin($isSuperAdmin): static
  224. {
  225. $this->isSuperAdmin = $isSuperAdmin;
  226. return $this;
  227. }
  228. /**
  229. * Get isSuperAdmin.
  230. *
  231. * @return bool
  232. */
  233. public function getIsSuperAdmin()
  234. {
  235. return $this->isSuperAdmin;
  236. }
  237. /**
  238. * Set createdAt.
  239. */
  240. public function setCreatedAt(\DateTime $createdAt): static
  241. {
  242. $this->createdAt = $createdAt;
  243. return $this;
  244. }
  245. /**
  246. * set email.
  247. */
  248. public function getEmail(): string
  249. {
  250. return $this->email;
  251. }
  252. public function setEmail(string $email): self
  253. {
  254. $this->email = $email;
  255. return $this;
  256. }
  257. /**
  258. * set enabled.
  259. */
  260. public function getEnabled()
  261. {
  262. return $this->enabled;
  263. }
  264. public function setEnabled($enabled): self
  265. {
  266. $this->enabled = $enabled;
  267. return $this;
  268. }
  269. /**
  270. * Get createdAt.
  271. */
  272. public function getCreatedAt(): \DateTime
  273. {
  274. return $this->createdAt;
  275. }
  276. /**
  277. * Set valideChartAt.
  278. *
  279. * @param \DateTime $valideChartAt
  280. */
  281. public function setValideChartAt($valideChartAt): static
  282. {
  283. $this->valideChartAt = $valideChartAt;
  284. return $this;
  285. }
  286. /**
  287. * Get valideChartAt.
  288. *
  289. * @return \DateTime
  290. */
  291. public function getValideChartAt()
  292. {
  293. return $this->valideChartAt;
  294. }
  295. /**
  296. * Set updatedAt.
  297. */
  298. public function setUpdatedAt(\DateTime $updatedAt): static
  299. {
  300. $this->updatedAt = $updatedAt;
  301. return $this;
  302. }
  303. /**
  304. * Get updatedAt.
  305. */
  306. public function getUpdatedAt(): \DateTime
  307. {
  308. return $this->updatedAt;
  309. }
  310. /**
  311. * Set region.
  312. */
  313. public function setRegion(?Region $region = null): static
  314. {
  315. $this->region = $region;
  316. return $this;
  317. }
  318. /**
  319. * Get region.
  320. *
  321. * @return Region
  322. */
  323. public function getRegion()
  324. {
  325. return $this->region;
  326. }
  327. /**
  328. * @ORM\PostPersist()
  329. *
  330. * @ORM\PostUpdate()
  331. */
  332. public function postUpdate(): void
  333. {
  334. }
  335. /**
  336. * Set token.
  337. *
  338. * @param string $token
  339. */
  340. public function setToken($token): static
  341. {
  342. $this->token = $token;
  343. return $this;
  344. }
  345. /**
  346. * Get token.
  347. *
  348. * @return string
  349. */
  350. public function getToken()
  351. {
  352. return $this->token;
  353. }
  354. /**
  355. * Set structure.
  356. */
  357. public function setStructure(?Structure $structure = null): static
  358. {
  359. $this->structure = $structure;
  360. return $this;
  361. }
  362. /**
  363. * Get structure.
  364. *
  365. * @return Structure
  366. */
  367. public function getStructure()
  368. {
  369. return $this->structure;
  370. }
  371. /**
  372. * Set lastLogin.
  373. *
  374. * @param \DateTime $lastLogin
  375. */
  376. public function setLastLogin($lastLogin): static
  377. {
  378. $this->lastLogin = $lastLogin;
  379. return $this;
  380. }
  381. /**
  382. * Get lastLogin.
  383. *
  384. * @return \DateTime
  385. */
  386. public function getLastLogin()
  387. {
  388. return $this->lastLogin;
  389. }
  390. /* retourne le mail, retourne le mail de la structure pour un CSO et le mail du user pour les autres rĂ´les. Retourne null si ce n'est pas un email */
  391. public function getEmailValid()
  392. {
  393. $email = null;
  394. if ($this->getStructure() && $this->getStructure()->getEmail() && filter_var($this->getStructure()->getEmail(), FILTER_VALIDATE_EMAIL)) {
  395. $email = $this->getStructure()->getEmail();
  396. } else {
  397. $email = $this->getEmail();
  398. }
  399. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  400. $email = null;
  401. }
  402. return $email;
  403. }
  404. public function isEnabled(): ?bool
  405. {
  406. return $this->enabled;
  407. }
  408. public function setSalt(?string $salt): static
  409. {
  410. $this->salt = $salt;
  411. return $this;
  412. }
  413. public function getConfirmationToken(): ?string
  414. {
  415. return $this->confirmationToken;
  416. }
  417. public function setConfirmationToken(?string $confirmationToken): static
  418. {
  419. $this->confirmationToken = $confirmationToken;
  420. return $this;
  421. }
  422. public function getPasswordRequestedAt(): ?\DateTimeInterface
  423. {
  424. return $this->passwordRequestedAt;
  425. }
  426. public function setPasswordRequestedAt(?\DateTimeInterface $passwordRequestedAt): static
  427. {
  428. $this->passwordRequestedAt = $passwordRequestedAt;
  429. return $this;
  430. }
  431. public function isIsAdmin(): ?bool
  432. {
  433. return $this->isAdmin;
  434. }
  435. public function isIsSuperAdmin(): ?bool
  436. {
  437. return $this->isSuperAdmin;
  438. }
  439. }