User.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  10. #[ORM\Table(name: "user")]
  11. #[ORM\Entity(repositoryClass: UserRepository::class)]
  12. #[UniqueEntity(fields: "username", message: "Le username est déjà utilisé")]
  13. #[UniqueEntity(fields: "mail", message: "L'email est déjà utilisé")]
  14. #[UniqueEntity(fields: "token", message: "Erreur, token non unique")]
  15. class User implements UserInterface,PasswordAuthenticatedUserInterface
  16. {
  17. #[ORM\Column(name: "id", type: "integer")]
  18. #[ORM\Id]
  19. #[ORM\GeneratedValue(strategy: "AUTO")]
  20. private ?int $id = null;
  21. #[ORM\Column(name: "username", type: "string", length: 191, unique: true)]
  22. private ?string $username = null;
  23. #[ORM\Column(name: "prenom", type: "string", length: 191, nullable: true)]
  24. private ?string $prenom = null;
  25. #[ORM\Column(name: "nom", type: "string", length: 191, nullable: true)]
  26. private ?string $nom = null;
  27. #[ORM\Column(name: "mail", type: "string", length: 191, unique: true)]
  28. #[Assert\Email()]
  29. private ?string $mail = null;
  30. #[ORM\Column(name: "password", type: "string", length: 191)]
  31. private ?string $password = null;
  32. #[ORM\Column(name: "token", type: "string", length: 191, unique: true)]
  33. private ?string $token = null;
  34. #[ORM\Column(name: "token_validity", type: "datetime")]
  35. #[Assert\Type("DateTime")]
  36. private ?\DateTimeInterface $tokenValidity = null;
  37. #[ORM\Column(name: "salt", type: "string", length: 191, nullable: true)]
  38. private ?string $salt = null;
  39. #[ORM\Column(name: "is_active", type: "boolean")]
  40. private ?bool $isActive = null;
  41. #[ORM\Column(name: "roles", type: "array")]
  42. private ?array $roles = array();
  43. #[ORM\ManyToMany(targetEntity: Film::class, mappedBy: "usersWantToView")]
  44. private ?Collection $films = null;
  45. #[ORM\ManyToMany(targetEntity: Film::class, mappedBy: "usersWhoSeen")]
  46. private ?Collection $filmsVus = null;
  47. #[ORM\OneToOne(targetEntity: Profile::class, mappedBy: "user", cascade: ["persist", "remove"])]
  48. private ?Profile $profile = null;
  49. #[ORM\Column(name: "last_activity", type: "datetime")]
  50. #[Assert\Type("DateTime")]
  51. private ?\DateTimeInterface $lastActivity = null;
  52. public function getLastActivity(): ?\DateTimeInterface
  53. {
  54. return $this->lastActivity;
  55. }
  56. public function setLastActivity(?\DateTimeInterface $lastActivity): self
  57. {
  58. $this->lastActivity = $lastActivity;
  59. return $this;
  60. }
  61. public function isActiveNow(): ?bool
  62. {
  63. $delay = new \DateTime('5 minutes ago');
  64. return ( $this->getLastActivity() > $delay );
  65. }
  66. public function getId(): ?int
  67. {
  68. return $this->id;
  69. }
  70. public function setUsername(?string $username): self
  71. {
  72. $this->username = $username;
  73. return $this;
  74. }
  75. public function getUsername(): ?string
  76. {
  77. return (string) $this->username;
  78. }
  79. public function getUserIdentifier(): string
  80. {
  81. return $this->username;
  82. }
  83. public function setActivated(?bool $activated): self
  84. {
  85. $this->isActive = $activated;
  86. return $this;
  87. }
  88. public function getActivated(): ?bool
  89. {
  90. return $this->isActive;
  91. }
  92. public function getPrenom(): ?string
  93. {
  94. return $this->prenom;
  95. }
  96. public function setPrenom(?string $prenom): self
  97. {
  98. $this->prenom = $prenom;
  99. return $this;
  100. }
  101. public function getNom(): ?string
  102. {
  103. return $this->nom;
  104. }
  105. public function setNom(?string $nom): self
  106. {
  107. $this->nom = $nom;
  108. return $this;
  109. }
  110. public function getMail(): ?string
  111. {
  112. return $this->mail;
  113. }
  114. public function setMail(?string $mail): self
  115. {
  116. $this->mail = $mail;
  117. return $this;
  118. }
  119. public function setPassword(?string $password): self
  120. {
  121. $this->password = $password;
  122. return $this;
  123. }
  124. public function getPassword() : ?string
  125. {
  126. return $this->password;
  127. }
  128. public function setSalt(?string $salt): self
  129. {
  130. $this->salt = $salt;
  131. return $this;
  132. }
  133. public function getSalt(): ?string
  134. {
  135. return $this->salt;
  136. }
  137. public function getToken(): ?string
  138. {
  139. return $this->token;
  140. }
  141. public function setToken(?string $token): self
  142. {
  143. //$this->token = hash("sha512", uniqid());
  144. $this->token = $token;
  145. $this->setTokenValidity(new \DateTime());
  146. return $this;
  147. }
  148. public function getTokenValidity(): ?\DateTimeInterface
  149. {
  150. return $this->tokenValidity;
  151. }
  152. private function setTokenValidity(?\DateTimeInterface $tokenValidity): self
  153. {
  154. $this->tokenValidity = $tokenValidity;
  155. return $this;
  156. }
  157. public function isValidToken(): ?bool
  158. {
  159. $expire = (new \DateTime($this->getTokenValidity()->format("Y-m-d H:i:s")))->modify('+1 hour');
  160. //dump($expire);
  161. //dump(new \DateTime('now'));
  162. return ( $expire > new \DateTime('now') );
  163. }
  164. public function setRoles(?array $roles): self
  165. {
  166. $this->roles = $roles;
  167. return $this;
  168. }
  169. /**
  170. * @see UserInterface
  171. */
  172. public function getRoles(): array
  173. {
  174. $roles = $this->roles;
  175. return array_unique($roles);
  176. }
  177. public function eraseCredentials(): void
  178. {
  179. }
  180. public function __construct()
  181. {
  182. $this->films = new \Doctrine\Common\Collections\ArrayCollection();
  183. //$this->setToken();
  184. $this->setLastActivity(new \DateTime('now'));
  185. $this->setActivated(true);
  186. }
  187. ///////////////////////////////////////////////////////////////
  188. public function addFilm(Film $film): self
  189. {
  190. $this->films[] = $film;
  191. return $this;
  192. }
  193. public function removeFilm(Film $film): self
  194. {
  195. $this->films->removeElement($film);
  196. return $this;
  197. }
  198. public function getFilms(): Collection
  199. {
  200. return $this->films;
  201. }
  202. ///////////////////////////////////////////////////////////////////////////
  203. public function addFilmVu(Film $film): self
  204. {
  205. $this->filmsVus[] = $film;
  206. return $this;
  207. }
  208. public function removeFilmVu(Film $film): self
  209. {
  210. $this->filmsVus->removeElement($film);
  211. return $this;
  212. }
  213. public function getFilmsVus(): Collection
  214. {
  215. return $this->filmsVus;
  216. }
  217. public function getNomComplet(): ?string
  218. {
  219. if ($this->prenom == null && $this->nom == null)
  220. {
  221. return $this->getUserIdentifier();
  222. } else
  223. return $this->getPrenom()." ".$this->getNom();
  224. }
  225. public function isAccountNonExpired(): ?bool
  226. {
  227. return true;
  228. }
  229. public function isAccountNonLocked(): ?bool
  230. {
  231. return true;
  232. }
  233. public function isCredentialsNonExpired(): ?bool
  234. {
  235. return true;
  236. }
  237. public function isEnabled(): ?bool
  238. {
  239. return $this->isActive;
  240. }
  241. public function serialize(): ?string
  242. {
  243. return serialize(array(
  244. $this->id,
  245. $this->username,
  246. $this->password,
  247. $this->isActive,
  248. // see section on salt below
  249. // $this->salt,
  250. ));
  251. }
  252. public function unserialize(?string $serialized):void
  253. {
  254. list (
  255. $this->id,
  256. $this->username,
  257. $this->password,
  258. $this->isActive,
  259. // see section on salt below
  260. // $this->salt
  261. ) = unserialize($serialized, array('allowed_classes' => false));
  262. }
  263. public function wantToSee(Film $film): ?bool
  264. {
  265. return $this->getFilms()->contains($film);
  266. }
  267. public function haveSeen(Film $film): ?bool
  268. {
  269. return $this->getFilmsVus()->contains($film);
  270. }
  271. public function getProfile(): ?Profile
  272. {
  273. return $this->profile;
  274. }
  275. public function setProfile(?Profile $profile): self
  276. {
  277. // set the owning side of the relation if necessary
  278. if ($profile->getUser() !== $this) {
  279. $profile->setUser($this);
  280. }
  281. $this->profile = $profile;
  282. return $this;
  283. }
  284. }