src/Entity/User.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use App\Exception\ValidationException;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Symfony\Component\Validator\Mapping\ClassMetadata;
  11. use App\Validator\Constraints\RegulationClassValidator;
  12. #[ORM\Entity(repositoryClass: UserRepository::class)]
  13. class User implements UserInterface
  14. {
  15. const ROLE_USER = 'ROLE_USER';
  16. const ROLE_CREATOR = 'ROLE_CREATOR';
  17. const ROLE_EDITOR = 'ROLE_EDITOR';
  18. const ROLE_WORKER = 'ROLE_WORKER';
  19. const ROLE_ADMIN = 'ROLE_ADMIN';
  20. #[ORM\Id]
  21. #[ORM\GeneratedValue]
  22. #[ORM\Column(type: 'integer')]
  23. private $id;
  24. #[ORM\Column(type: 'string', length: 180, unique: true)]
  25. private $username;
  26. #[ORM\Column(type: 'string', length: 250)]
  27. private $displayName;
  28. #[ORM\Column(type: 'string', length: 250)]
  29. private $email;
  30. #[ORM\Column(type: 'json')]
  31. private $roles = [];
  32. #[ORM\Column(type: 'string')]
  33. private $password;
  34. #[ORM\Column(name: 'modified', type: 'datetime', nullable: true, options: ['comment' => 'Módosítva'])]
  35. private $modified;
  36. private $passwordRetryA;
  37. private $passwordRetryB;
  38. public static $rolesList = [
  39. 'Dolgozó'=>'ROLE_WORKER',
  40. 'Néző'=>'ROLE_USER',
  41. 'Létrehozó' => 'ROLE_CREATOR',
  42. 'Szerkesztő' => 'ROLE_EDITOR',
  43. 'Adminisztrátor' => 'ROLE_ADMIN'
  44. ];
  45. #[ORM\OneToOne(targetEntity: Workers::class, inversedBy: 'user', cascade: ['persist', 'remove'])]
  46. private $worker;
  47. #[ORM\OneToMany(targetEntity: Attachment::class, mappedBy: 'user')]
  48. private $attachments;
  49. public function __construct()
  50. {
  51. $this->attachments = new ArrayCollection();
  52. }
  53. public static function loadValidatorMetadata(ClassMetadata $metadata)
  54. {
  55. $metadata->addGetterConstraint('checkPassword', new Assert\IsTrue([
  56. 'message' => 'Jelszavak nem egyeznek meg',
  57. ]));
  58. }
  59. public function getCheckPassword(){
  60. return $this->passwordRetryA == $this->passwordRetryB;
  61. }
  62. /**
  63. * A visual identifier that represents this user.
  64. *
  65. * @see UserInterface
  66. */
  67. public function getUserIdentifier(): string
  68. {
  69. return (string) $this->username;
  70. }
  71. public function getId(): ?int
  72. {
  73. return $this->id;
  74. }
  75. /**
  76. * A visual identifier that represents this user.
  77. *
  78. * @see UserInterface
  79. */
  80. public function getUsername(): string
  81. {
  82. return (string) $this->username;
  83. }
  84. public function setUsername(string $username): self
  85. {
  86. $this->username = $username;
  87. return $this;
  88. }
  89. /**
  90. * A visual identifier that represents this user.
  91. *
  92. * @see UserInterface
  93. */
  94. public function getDisplayName(): string
  95. {
  96. return (string) $this->displayName;
  97. }
  98. public function setDisplayName(string $displayName): self
  99. {
  100. $this->displayName = $displayName;
  101. return $this;
  102. }
  103. /**
  104. * A visual identifier that represents this user.
  105. *
  106. * @see UserInterface
  107. */
  108. public function getEmail(): string
  109. {
  110. return (string) $this->email;
  111. }
  112. public function setEmail(string $email): self
  113. {
  114. $this->email = $email;
  115. return $this;
  116. }
  117. /**
  118. * @see UserInterface
  119. */
  120. public function getRoles(): array
  121. {
  122. $roles = $this->roles;
  123. // guarantee every user at least has ROLE_USER
  124. //$roles[] = 'ROLE_USER';
  125. return array_unique($roles);
  126. }
  127. public function setRoles(array $roles): self
  128. {
  129. $this->roles = $roles;
  130. return $this;
  131. }
  132. public function hasRoles(){
  133. foreach (func_get_args() as $k => $role) {
  134. if(in_array($role, $this->roles)){
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. /**
  141. * @see UserInterface
  142. */
  143. public function getPassword(): string
  144. {
  145. return (string) $this->password;
  146. }
  147. public function setPassword(string $password): self
  148. {
  149. $this->password = $password;
  150. return $this;
  151. }
  152. public function getPasswordRetryA(): string
  153. {
  154. return (string) $this->passwordRetryA;
  155. }
  156. public function setPasswordRetryA(?string $password): self
  157. {
  158. $this->passwordRetryA = $password;
  159. return $this;
  160. }
  161. public function getPasswordRetryB(): string
  162. {
  163. return (string) $this->passwordRetryB;
  164. }
  165. public function setPasswordRetryB(?string $password): self
  166. {
  167. $this->passwordRetryB = $password;
  168. return $this;
  169. }
  170. /**
  171. * @see UserInterface
  172. */
  173. public function getSalt()
  174. {
  175. // not needed when using the "bcrypt" algorithm in security.yaml
  176. }
  177. /**
  178. * @see UserInterface
  179. */
  180. public function eraseCredentials()
  181. {
  182. // If you store any temporary, sensitive data on the user, clear it here
  183. // $this->plainPassword = null;
  184. }
  185. public function getModified(): ?\DateTimeInterface
  186. {
  187. return $this->modified;
  188. }
  189. public function getWorker(): ?Workers
  190. {
  191. return $this->worker;
  192. }
  193. public function setWorker(?Workers $worker): self
  194. {
  195. $this->worker = $worker;
  196. return $this;
  197. }
  198. /**
  199. * @return Collection|Attachment[]
  200. */
  201. public function getAttachments(): Collection
  202. {
  203. return $this->attachments;
  204. }
  205. public function addAttachment(Attachment $attachment): self
  206. {
  207. if (!$this->attachments->contains($attachment)) {
  208. $this->attachments[] = $attachment;
  209. $attachment->setUser($this);
  210. }
  211. return $this;
  212. }
  213. public function removeAttachment(Attachment $attachment): self
  214. {
  215. if ($this->attachments->removeElement($attachment)) {
  216. // set the owning side to null (unless already changed)
  217. if ($attachment->getUser() === $this) {
  218. $attachment->setUser(null);
  219. }
  220. }
  221. return $this;
  222. }
  223. public function __toString()
  224. {
  225. return (string)$this->displayName;
  226. }
  227. }