User.php 8.5 KB

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