src/Entity/User.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  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. /**
  10. * @UniqueEntity("username", message="Nom d'utilisateur déjà utilisé")
  11. * @UniqueEntity("mail", message="Adresse email déjà utilisé")
  12. */
  13. #[ORM\Entity(repositoryClass: UserRepository::class)]
  14. class User implements UserInterface, PasswordAuthenticatedUserInterface
  15. {
  16. const ROLES_CHOICE_FORM = [
  17. 'Client' => 'ROLE_CLIENT',
  18. 'Égérie' => 'ROLE_EGERIE',
  19. ];
  20. const NEWSLETTER = [
  21. 'ENABLE' => 0,
  22. 'DISABLE' => -1
  23. ];
  24. const LOGIN_FIELD = [
  25. "mail",
  26. "username"
  27. ];
  28. #[ORM\Id]
  29. #[ORM\GeneratedValue]
  30. #[ORM\Column(type: 'integer')]
  31. private $id;
  32. #[ORM\Column(type: 'string', length: 100, unique: true)]
  33. private $username;
  34. #[ORM\Column(type: 'json')]
  35. private $roles = [];
  36. /**
  37. * @var string The hashed password
  38. */
  39. #[ORM\Column(type: 'string')]
  40. private $password;
  41. #[ORM\Column(type: 'string', length: 255)]
  42. private $lastname;
  43. #[ORM\Column(type: 'string', length: 255, nullable: true)]
  44. private $firstname;
  45. #[ORM\Column(type: 'string', length: 255, unique: true)]
  46. private $mail;
  47. #[ORM\Column(type: 'string', nullable: true)]
  48. private $address;
  49. #[ORM\Column(type: 'string', nullable: true)]
  50. private $phone;
  51. #[ORM\Column(type: 'datetime')]
  52. private $signupDate;
  53. private $plainPassword;
  54. #[ORM\Column(type: 'string', nullable: true)]
  55. private $image;
  56. #[ORM\Column(nullable: true, options: ['default' => 0])]
  57. private ?int $newsletter = null;
  58. #[ORM\Column(type: 'smallint', options: ['default' => 0])]
  59. private int $statut = 0;
  60. #[ORM\ManyToOne(targetEntity: self::class)]
  61. #[ORM\JoinColumn(name: 'parrain_id', referencedColumnName: 'id', nullable: true)]
  62. private ?self $parrain = null;
  63. public function __construct()
  64. {
  65. $this->newsletter = self::NEWSLETTER['ENABLE'];
  66. }
  67. public function getId(): ?int
  68. {
  69. return $this->id;
  70. }
  71. public function getUsername(): ?string
  72. {
  73. return $this->username;
  74. }
  75. public function setUsername(string $username): self
  76. {
  77. $this->username = $username;
  78. return $this;
  79. }
  80. /**
  81. * A visual identifier that represents this user.
  82. *
  83. * @see UserInterface
  84. */
  85. public function getUserIdentifier(): string
  86. {
  87. return (string) $this->username;
  88. }
  89. /**
  90. * @see UserInterface
  91. */
  92. public function getRoles(): array
  93. {
  94. $roles = $this->roles;
  95. // guarantee every user at least has ROLE_USER
  96. $roles[] = 'ROLE_USER';
  97. return array_unique($roles);
  98. }
  99. public function setRoles(array $roles): self
  100. {
  101. $this->roles = $roles;
  102. return $this;
  103. }
  104. /**
  105. * @see PasswordAuthenticatedUserInterface
  106. */
  107. public function getPassword(): string
  108. {
  109. return $this->password;
  110. }
  111. public function setPassword(string $password): self
  112. {
  113. $this->password = $password;
  114. return $this;
  115. }
  116. /**
  117. * Returning a salt is only needed, if you are not using a modern
  118. * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  119. *
  120. * @see UserInterface
  121. */
  122. public function getSalt(): ?string
  123. {
  124. return null;
  125. }
  126. /**
  127. * @see UserInterface
  128. */
  129. public function eraseCredentials()
  130. {
  131. // If you store any temporary, sensitive data on the user, clear it here
  132. // $this->plainPassword = null;
  133. }
  134. public function getLastname(): ?string
  135. {
  136. return $this->lastname;
  137. }
  138. public function setLastname(string $lastname): self
  139. {
  140. $this->lastname = $lastname;
  141. return $this;
  142. }
  143. public function getFirstname(): ?string
  144. {
  145. return $this->firstname;
  146. }
  147. public function setFirstname(?string $firstname): self
  148. {
  149. $this->firstname = $firstname;
  150. return $this;
  151. }
  152. public function getMail(): ?string
  153. {
  154. return $this->mail;
  155. }
  156. public function setMail(string $mail): self
  157. {
  158. $this->mail = $mail;
  159. return $this;
  160. }
  161. public function getAddress(): ?string
  162. {
  163. return $this->address;
  164. }
  165. public function setAddress(?string $address): self
  166. {
  167. $this->address = $address;
  168. return $this;
  169. }
  170. public function getPhone(): ?string
  171. {
  172. return $this->phone;
  173. }
  174. public function setPhone(?string $phone): self
  175. {
  176. $this->phone = $phone;
  177. return $this;
  178. }
  179. public function getSignupDate(): ?\DateTimeInterface
  180. {
  181. return $this->signupDate;
  182. }
  183. public function setSignupDate(\DateTimeInterface $signupDate): self
  184. {
  185. $this->signupDate = $signupDate;
  186. return $this;
  187. }
  188. /**
  189. * Get the value of plainPassword
  190. */
  191. public function getPlainPassword()
  192. {
  193. return $this->plainPassword;
  194. }
  195. /**
  196. * Set the value of plainPassword
  197. *
  198. * @return self
  199. */
  200. public function setPlainPassword($plainPassword)
  201. {
  202. $this->plainPassword = $plainPassword;
  203. return $this;
  204. }
  205. public function getRoleName(): ?string
  206. {
  207. if(in_array('ROLE_ADMIN', $this->getRoles())){
  208. return 'Admin';
  209. } else if(in_array('ROLE_CLIENT', $this->getRoles())){
  210. return 'Client';
  211. }
  212. return null;
  213. }
  214. public function getImage(): ?string
  215. {
  216. return $this->image;
  217. }
  218. public function setImage(?string $image): self
  219. {
  220. $this->image = $image;
  221. return $this;
  222. }
  223. public function getFullName(){
  224. return ($this->getFirstname() ? $this->getFirstname(): '') . ' ' . ($this->getLastname() ? $this->getLastname() : '');
  225. }
  226. public function getToken(){
  227. return sha1($this->getId());
  228. }
  229. public function getNewsletter(): ?int
  230. {
  231. return $this->newsletter;
  232. }
  233. public function setNewsletter(?int $newsletter): self
  234. {
  235. $this->newsletter = $newsletter;
  236. return $this;
  237. }
  238. public function getStatut(): int
  239. {
  240. return $this->statut;
  241. }
  242. public function setStatut(int $statut): self
  243. {
  244. $this->statut = $statut;
  245. return $this;
  246. }
  247. /**
  248. * Get the value of parrain
  249. *
  250. * @return ?self
  251. */
  252. public function getParrain(): ?self
  253. {
  254. return $this->parrain;
  255. }
  256. /**
  257. * Set the value of parrain
  258. *
  259. * @param ?self $parrain
  260. *
  261. * @return self
  262. */
  263. public function setParrain(?self $parrain): self
  264. {
  265. $this->parrain = $parrain;
  266. return $this;
  267. }
  268. }