src/Entity/User.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Contest\Team;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
  10. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  11. /**
  12. * @ORM\Entity()
  13. * @UniqueEntity(fields={"email"})
  14. * @UniqueEntity(fields={"username"})
  15. */
  16. class User implements UserInterface, PasswordAuthenticatedUserInterface, TwoFactorInterface
  17. {
  18. /**
  19. * @ORM\Id()
  20. * @ORM\GeneratedValue()
  21. * @ORM\Column(type="integer")
  22. */
  23. private $id;
  24. /**
  25. * @ORM\Column(type="string", length=180, unique=true)
  26. */
  27. private $username;
  28. /**
  29. * @ORM\Column(type="string", length=180, nullable=true)
  30. */
  31. private $firstname;
  32. /**
  33. * @ORM\Column(type="string", length=180, nullable=true)
  34. */
  35. private $lastname;
  36. /**
  37. * @ORM\Column(type="string", length=180, unique=true)
  38. */
  39. private $email;
  40. /**
  41. * @ORM\Column(type="json")
  42. */
  43. private $roles = [];
  44. /**
  45. * @var string The hashed password
  46. * @ORM\Column(type="string")
  47. */
  48. private $password;
  49. /**
  50. * @ORM\Column(name="enabled", type="boolean")
  51. */
  52. protected $enabled = false;
  53. /**
  54. * @ORM\OneToMany(targetEntity=Answear::class, mappedBy="user", orphanRemoval=true)
  55. */
  56. private $answears;
  57. /**
  58. * @ORM\Column(type="string", length=180, nullable=true)
  59. */
  60. private ?string $authCode = null;
  61. public function __construct()
  62. {
  63. $this->answears = new ArrayCollection();
  64. }
  65. public function getId(): ?int
  66. {
  67. return $this->id;
  68. }
  69. public function getEmail(): ?string
  70. {
  71. return $this->email;
  72. }
  73. public function setEmail(string $email): self
  74. {
  75. $this->email = $email;
  76. return $this;
  77. }
  78. /**
  79. * @see UserInterface
  80. */
  81. public function getRoles(): array
  82. {
  83. $roles = $this->roles;
  84. // guarantee every user at least has ROLE_USER
  85. $roles[] = 'ROLE_USER';
  86. return array_unique($roles);
  87. }
  88. public function setRoles(array $roles): self
  89. {
  90. $this->roles = $roles;
  91. return $this;
  92. }
  93. /**
  94. * @see UserInterface
  95. */
  96. public function getPassword(): string
  97. {
  98. return (string)$this->password;
  99. }
  100. public function setPassword(string $password): self
  101. {
  102. $this->password = $password;
  103. return $this;
  104. }
  105. /**
  106. * @see UserInterface
  107. */
  108. public function getSalt()
  109. {
  110. // not needed when using the "bcrypt" algorithm in security.yaml
  111. }
  112. /**
  113. * @see UserInterface
  114. */
  115. public function eraseCredentials()
  116. {
  117. // If you store any temporary, sensitive data on the user, clear it here
  118. // $this->plainPassword = null;
  119. }
  120. /**
  121. * @return bool
  122. */
  123. public function isEnabled(): bool
  124. {
  125. return (bool)$this->enabled;
  126. }
  127. /**
  128. * @param bool $enabled
  129. * @return User
  130. */
  131. public function setEnabled(bool $enabled): self
  132. {
  133. $this->enabled = $enabled;
  134. return $this;
  135. }
  136. public function __toString()
  137. {
  138. return "".$this->getUsername();
  139. }
  140. /**
  141. * @return mixed
  142. */
  143. public function getUsername()
  144. {
  145. return $this->username;
  146. }
  147. /**
  148. * @param mixed $username
  149. * @return User
  150. */
  151. public function setUsername($username)
  152. {
  153. $this->username = $username;
  154. return $this;
  155. }
  156. /**
  157. * @return mixed
  158. */
  159. public function getFirstname()
  160. {
  161. return $this->firstname;
  162. }
  163. /**
  164. * @param mixed $firstname
  165. * @return User
  166. */
  167. public function setFirstname($firstname)
  168. {
  169. $this->firstname = $firstname;
  170. return $this;
  171. }
  172. /**
  173. * @return mixed
  174. */
  175. public function getLastname()
  176. {
  177. return $this->lastname;
  178. }
  179. /**
  180. * @param mixed $lastname
  181. * @return User
  182. */
  183. public function setLastname($lastname)
  184. {
  185. $this->lastname = $lastname;
  186. return $this;
  187. }
  188. /**
  189. * @return Collection|Answear[]
  190. */
  191. public function getAnswears(): Collection
  192. {
  193. return $this->answears;
  194. }
  195. public function addAnswear(Answear $answear): self
  196. {
  197. if (!$this->answears->contains($answear)) {
  198. $this->answears[] = $answear;
  199. $answear->setUser($this);
  200. }
  201. return $this;
  202. }
  203. public function removeAnswear(Answear $answear): self
  204. {
  205. if ($this->answears->removeElement($answear)) {
  206. // set the owning side to null (unless already changed)
  207. if ($answear->getUser() === $this) {
  208. $answear->setUser(null);
  209. }
  210. }
  211. return $this;
  212. }
  213. public function isEmailAuthEnabled(): bool
  214. {
  215. return (in_array('ROLE_ADMIN', $this->getRoles(), true) || in_array('ROLE_SUPER_ADMIN', $this->getRoles(), true));
  216. // Tutaj decydujesz, czy 2FA jest zawsze włączone, czy zależy od flagi w bazie danych
  217. //return true;
  218. }
  219. public function getEmailAuthRecipient(): string
  220. {
  221. return $this->email; // Zwraca adres e-mail użytkownika
  222. }
  223. public function getEmailAuthCode(): ?string
  224. {
  225. return $this->authCode;
  226. }
  227. public function setEmailAuthCode(?string $authCode): void
  228. {
  229. $this->authCode = $authCode;
  230. }
  231. }