User.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. namespace AppBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. /**
  9. * User
  10. *
  11. * @ORM\Table(name="user")
  12. * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
  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. *
  17. */
  18. class User implements UserInterface
  19. {
  20. /**
  21. * @var int
  22. *
  23. * @ORM\Column(name="id", type="integer")
  24. * @ORM\Id
  25. * @ORM\GeneratedValue(strategy="AUTO")
  26. */
  27. private $id;
  28. /**
  29. * @var string
  30. *
  31. * @ORM\Column(name="username", type="string", length=255, unique=true)
  32. */
  33. private $username;
  34. /**
  35. * @var string
  36. *
  37. * @ORM\Column(name="prenom", type="string", length=255)
  38. */
  39. private $prenom;
  40. /**
  41. * @var string
  42. *
  43. * @ORM\Column(name="nom", type="string", length=255)
  44. */
  45. private $nom;
  46. /**
  47. * @var string
  48. *
  49. * @ORM\Column(name="mail", type="string", length=255, unique=true)
  50. */
  51. private $mail;
  52. /**
  53. * @var string
  54. *
  55. * @ORM\Column(name="password", type="string", length=255)
  56. */
  57. private $password;
  58. /**
  59. * @var string
  60. *
  61. * @ORM\Column(name="token", type="string", length=255, unique=true)
  62. */
  63. private $token;
  64. /**
  65. * @var \DateTime
  66. *
  67. * @ORM\Column(name="token_validity", type="datetime")
  68. */
  69. private $tokenValidity;
  70. /**
  71. * @var string
  72. *
  73. * @ORM\Column(name="salt", type="string", length=255, nullable=true)
  74. */
  75. private $salt;
  76. /**
  77. * @var array
  78. *
  79. * @ORM\Column(name="roles", type="array")
  80. */
  81. private $roles = array();
  82. /**
  83. * @var \DateTime
  84. *
  85. * @ORM\Column(name="last_activity", type="datetime")
  86. */
  87. private $lastActivity;
  88. /**
  89. * @return \DateTime
  90. */
  91. public function getLastActivity()
  92. {
  93. return $this->lastActivity;
  94. }
  95. /**
  96. * @param \DateTime $lastActivity
  97. */
  98. public function setLastActivity($lastActivity)
  99. {
  100. $this->lastActivity = $lastActivity;
  101. }
  102. /**
  103. * @return boolean
  104. */
  105. public function isActiveNow()
  106. {
  107. $delay = new \DateTime('2 minutes ago');
  108. return ( $this->getLastActivity() > $delay );
  109. }
  110. /**
  111. * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Film", mappedBy="usersWantToView")
  112. * @var \Doctrine\Common\Collections\Collection
  113. */
  114. private $films;
  115. /**
  116. * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Film", mappedBy="usersWhoSeen")
  117. * @var \Doctrine\Common\Collections\Collection
  118. */
  119. private $filmsVus;
  120. /**
  121. * Get id
  122. *
  123. * @return int
  124. */
  125. public function getId()
  126. {
  127. return $this->id;
  128. }
  129. /**
  130. * Set username
  131. *
  132. * @param string $username
  133. *
  134. * @return User
  135. */
  136. public function setUsername($username)
  137. {
  138. $this->username = $username;
  139. return $this;
  140. }
  141. /**
  142. * Get username
  143. *
  144. * @return string
  145. */
  146. public function getUsername()
  147. {
  148. return $this->username;
  149. }
  150. /**
  151. * @return string
  152. */
  153. public function getPrenom()
  154. {
  155. return $this->prenom;
  156. }
  157. /**
  158. * @param string $prenom
  159. */
  160. public function setPrenom($prenom)
  161. {
  162. $this->prenom = $prenom;
  163. }
  164. /**
  165. * @return string
  166. */
  167. public function getNom()
  168. {
  169. return $this->nom;
  170. }
  171. /**
  172. * @param string $nom
  173. */
  174. public function setNom($nom)
  175. {
  176. $this->nom = $nom;
  177. }
  178. /**
  179. * @return string
  180. */
  181. public function getMail()
  182. {
  183. return $this->mail;
  184. }
  185. /**
  186. * @param string $mail
  187. */
  188. public function setMail($mail)
  189. {
  190. $this->mail = $mail;
  191. }
  192. /**
  193. * Set password
  194. *
  195. * @param string $password
  196. *
  197. * @return User
  198. */
  199. public function setPassword($password)
  200. {
  201. $this->password = $password;
  202. return $this;
  203. }
  204. /**
  205. * Get password
  206. *
  207. * @return string
  208. */
  209. public function getPassword()
  210. {
  211. return $this->password;
  212. }
  213. /**
  214. * Set salt
  215. *
  216. * @param string $salt
  217. *
  218. * @return User
  219. */
  220. public function setSalt($salt)
  221. {
  222. $this->salt = $salt;
  223. return $this;
  224. }
  225. /**
  226. * Get salt
  227. *
  228. * @return string
  229. */
  230. public function getSalt()
  231. {
  232. return $this->salt;
  233. }
  234. /**
  235. * Get token
  236. *
  237. * @return string
  238. */
  239. public function getToken()
  240. {
  241. return $this->token;
  242. }
  243. /**
  244. * Set token
  245. *
  246. * @return User
  247. */
  248. public function setToken()
  249. {
  250. $this->token = hash("sha512", uniqid());
  251. $this->setTokenValidity(new \DateTime());
  252. return $this;
  253. }
  254. /**
  255. * Get token_validity
  256. *
  257. * @return \DateTime
  258. */
  259. public function getTokenValidity()
  260. {
  261. return $this->tokenValidity;
  262. }
  263. /**
  264. * Set token
  265. *
  266. * @param \DateTime $tokenValidity
  267. *
  268. * @return User
  269. */
  270. private function setTokenValidity(\DateTime $tokenValidity)
  271. {
  272. $this->tokenValidity = $tokenValidity;
  273. return $this;
  274. }
  275. /**
  276. * Is ValidToken
  277. *
  278. * @return boolean
  279. *
  280. */
  281. public function isValidToken()
  282. {
  283. $expire = $this->getTokenValidity()->modify('+1 hour');
  284. return ( $expire > new \DateTime('now') );
  285. }
  286. /**
  287. * Set roles
  288. *
  289. * @param array $roles
  290. *
  291. * @return User
  292. */
  293. public function setRoles($roles)
  294. {
  295. $this->roles = $roles;
  296. return $this;
  297. }
  298. /**
  299. * Get roles
  300. *
  301. * @return array
  302. */
  303. public function getRoles()
  304. {
  305. return $this->roles;
  306. }
  307. public function eraseCredentials()
  308. {
  309. }
  310. /**
  311. * Constructor
  312. */
  313. public function __construct()
  314. {
  315. $this->films = new \Doctrine\Common\Collections\ArrayCollection();
  316. $this->setToken();
  317. $this->setLastActivity(new \DateTime('now'));
  318. }
  319. ///////////////////////////////////////////////////////////////
  320. /**
  321. * Add film
  322. *
  323. * @param \AppBundle\Entity\Film $film
  324. *
  325. * @return User
  326. */
  327. public function addFilm(\AppBundle\Entity\Film $film)
  328. {
  329. $this->films[] = $film;
  330. return $this;
  331. }
  332. /**
  333. * Remove film
  334. *
  335. * @param \AppBundle\Entity\Film $film
  336. */
  337. public function removeFilm(\AppBundle\Entity\Film $film)
  338. {
  339. $this->films->removeElement($film);
  340. }
  341. /**
  342. * Get films
  343. *
  344. * @return \Doctrine\Common\Collections\Collection
  345. */
  346. public function getFilms()
  347. {
  348. return $this->films;
  349. }
  350. ///////////////////////////////////////////////////////////////////////////
  351. /**
  352. * Add film
  353. *
  354. * @param \AppBundle\Entity\Film $film
  355. *
  356. * @return User
  357. */
  358. public function addFilmVu(\AppBundle\Entity\Film $film)
  359. {
  360. $this->filmsVus[] = $film;
  361. return $this;
  362. }
  363. /**
  364. * Remove film
  365. *
  366. * @param \AppBundle\Entity\Film $film
  367. */
  368. public function removeFilmVu(\AppBundle\Entity\Film $film)
  369. {
  370. $this->filmsVus->removeElement($film);
  371. }
  372. /**
  373. * Get films
  374. *
  375. * @return \Doctrine\Common\Collections\Collection
  376. */
  377. public function getFilmsVus()
  378. {
  379. return $this->filmsVus;
  380. }
  381. /**
  382. * Get nomComplet
  383. * @return string
  384. */
  385. public function getNomComplet()
  386. {
  387. return $this->getPrenom()." ".$this->getNom();
  388. }
  389. }