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