src/Entity/User.php line 14

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\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. #[UniqueEntity(fields: ["username", "email"], message: "El nombre de usuario y/o email ya existe")]
  9. #[ORM\Entity(repositoryClass: UserRepository::class)]
  10. #[ORM\Table(name: '`user`')]
  11. class User implements UserInterface, PasswordAuthenticatedUserInterface
  12. {
  13. #[ORM\Id]
  14. #[ORM\GeneratedValue]
  15. #[ORM\Column(type: "integer")]
  16. private ?int $id = null;
  17. #[ORM\Column(length: 180, unique: true)]
  18. private ?string $email = null;
  19. #[ORM\Column(length: 180, unique: true)]
  20. private ?string $username = null;
  21. #[ORM\Column(length: 180, unique: true)]
  22. private ?string $usernameCanonical = null;
  23. #[ORM\Column(length: 180, unique: true)]
  24. private ?string $emailCanonical = null;
  25. #[ORM\Column(length: 255, nullable: true)]
  26. private ?string $salt = null;
  27. #[ORM\Column(type:"array")]
  28. private array $roles = [];
  29. #[ORM\Column]
  30. private ?string $password = null;
  31. #[ORM\OneToOne(inversedBy: "user", targetEntity: "Employee", cascade: ['persist', 'remove'])]
  32. #[ORM\JoinColumn(name: "employee_id", referencedColumnName: "id", onDelete: "CASCADE")]
  33. protected $employee;
  34. #[ORM\OneToOne(targetEntity:"Supplier", mappedBy:"user", cascade: ['persist', 'remove'])]
  35. #[ORM\JoinColumn(name: "supplier_id", referencedColumnName: "id", onDelete: "CASCADE", nullable: true)]
  36. protected $supplier;
  37. #[ORM\Column(type: "boolean")]
  38. protected $enabled;
  39. #[ORM\Column(type: "boolean")]
  40. protected $secured = false;
  41. #[ORM\Column(type: "boolean", options: ["default" => false])]
  42. protected $passwordUpdatedForNewPolicy = false;
  43. public function getId(): ?int
  44. {
  45. return $this->id;
  46. }
  47. public function getEmail(): ?string
  48. {
  49. return $this->email;
  50. }
  51. public function setEmail(string $email): self
  52. {
  53. $this->email = $email;
  54. return $this;
  55. }
  56. public function getEmailCanonical(): ?string
  57. {
  58. return $this->emailCanonical;
  59. }
  60. public function setEmailCanonical(string $emailCanonical): self
  61. {
  62. $this->emailCanonical = $emailCanonical;
  63. return $this;
  64. }
  65. /**
  66. * A visual identifier that represents this user.
  67. *
  68. * @see UserInterface
  69. */
  70. public function getUserIdentifier(): string
  71. {
  72. return (string) $this->username;
  73. }
  74. public function getUsername(): string
  75. {
  76. return (string) $this->username;
  77. }
  78. public function setUsername($username): self
  79. {
  80. $this->username = $username;
  81. return $this;
  82. }
  83. public function getUsernameCanonical(): string
  84. {
  85. return (string) $this->usernameCanonical;
  86. }
  87. public function setUsernameCanonical($usernameCanonical): self
  88. {
  89. $this->usernameCanonical = $usernameCanonical;
  90. return $this;
  91. }
  92. /**
  93. * @see UserInterface
  94. */
  95. public function getRoles(): array
  96. {
  97. $roles = $this->roles;
  98. // guarantee every user at least has ROLE_USER
  99. $roles[] = 'ROLE_USER';
  100. return array_unique($roles);
  101. }
  102. public function setRoles(array $roles): self
  103. {
  104. $this->roles = $roles;
  105. return $this;
  106. }
  107. /**
  108. * @see PasswordAuthenticatedUserInterface
  109. */
  110. public function getPassword(): string
  111. {
  112. return $this->password;
  113. }
  114. public function setPassword(string $password): self
  115. {
  116. $this->password = $password;
  117. return $this;
  118. }
  119. /**
  120. * Returning a salt is only needed, if you are not using a modern
  121. * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  122. *
  123. * @see UserInterface
  124. */
  125. public function getSalt(): ?string
  126. {
  127. return $this->salt;
  128. }
  129. /**
  130. * @see UserInterface
  131. */
  132. public function eraseCredentials()
  133. {
  134. // If you store any temporary, sensitive data on the user, clear it here
  135. // $this->plainPassword = null;
  136. }
  137. public function getEmployee()
  138. {
  139. return $this->employee;
  140. }
  141. public function setEmployee($employee)
  142. {
  143. $this->employee = $employee;
  144. $this->employee->setUser($this);
  145. }
  146. public function isEmployee()
  147. {
  148. return !is_null($this->employee);
  149. }
  150. public function getIsEmployee()
  151. {
  152. return $this->isEmployee();
  153. }
  154. public function getSupplier()
  155. {
  156. return $this->supplier;
  157. }
  158. public function setSupplier($supplier)
  159. {
  160. $this->supplier = $supplier;
  161. $this->supplier->setUser($this);
  162. }
  163. public function isSupplier()
  164. {
  165. return !is_null($this->supplier);
  166. }
  167. public function getIsSupplier()
  168. {
  169. return $this->isSupplier();
  170. }
  171. public function isSecured(): bool
  172. {
  173. return $this->secured;
  174. }
  175. public function setSecured(bool $secured)
  176. {
  177. $this->secured = $secured;
  178. }
  179. public function isPasswordUpdatedForNewPolicy(): bool
  180. {
  181. return $this->passwordUpdatedForNewPolicy;
  182. }
  183. public function setPasswordUpdatedForNewPolicy(bool $passwordUpdatedForNewPolicy): self
  184. {
  185. $this->passwordUpdatedForNewPolicy = $passwordUpdatedForNewPolicy;
  186. return $this;
  187. }
  188. public function isEnabled()
  189. {
  190. return $this->enabled;
  191. }
  192. public function setEnabled($boolean)
  193. {
  194. $this->enabled = (bool) $boolean;
  195. return $this;
  196. }
  197. public function addRole($role)
  198. {
  199. $role = strtoupper($role);
  200. if ($role === 'ROLE_USER') {
  201. return $this;
  202. }
  203. if (!in_array($role, $this->roles, true)) {
  204. $this->roles[] = $role;
  205. }
  206. return $this;
  207. }
  208. public function removeRole($role)
  209. {
  210. if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
  211. unset($this->roles[$key]);
  212. $this->roles = array_values($this->roles);
  213. }
  214. return $this;
  215. }
  216. public function __toString(): string
  217. {
  218. if ($this->employee) {
  219. return $this->employee->getFullName();
  220. }
  221. if ($this->supplier) {
  222. return $this->supplier->getName();
  223. }
  224. return $this->username ?? '';
  225. }
  226. }