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