src/Entity/User.php line 18

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