src/Entity/WidgetUser.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\WidgetUserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Ramsey\Uuid\Uuid;
  8. use Ramsey\Uuid\UuidInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  11. use Symfony\Component\Serializer\Annotation\Groups;
  12. use OpenApi\Annotations as OA;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. /**
  15. * @ORM\Entity(repositoryClass=WidgetUserRepository::class)
  16. * @method string getUserIdentifier()
  17. */
  18. class WidgetUser implements UserInterface, PasswordAuthenticatedUserInterface
  19. {
  20. public const ROLES = [
  21. 'Клиент' => 'ROLE_CLIENT',
  22. 'Сотрудник саппорта' => 'ROLE_MERCHANT',
  23. 'Менеджер' => 'ROLE_MANAGER',
  24. 'Администратор' => 'ROLE_SUPERADMINISTRATOR',
  25. ];
  26. /**
  27. * @Groups({"user","user_uuid","widget"})
  28. * @var \Ramsey\Uuid\UuidInterface
  29. * @ORM\Id
  30. * @ORM\Column(type="uuid", unique=true)
  31. * @ORM\GeneratedValue(strategy="CUSTOM")
  32. * @ORM\CustomIdGenerator(class="\Ramsey\Uuid\Doctrine\UuidGenerator")
  33. * @OA\Property(
  34. * type="string"
  35. * )
  36. * @Assert\NotBlank()
  37. */
  38. private $id;
  39. /**
  40. * @Groups({"user","widget"})
  41. * @ORM\Column(type="string", length=255)
  42. */
  43. private $name;
  44. /**
  45. * @Groups({"user","widget"})
  46. * @ORM\Column(type="string", length=255)
  47. * @Assert\NotBlank()
  48. */
  49. private $email;
  50. /**
  51. * @Groups({"user"})
  52. * @ORM\Column(type="string", length=255)
  53. * @Assert\NotBlank()
  54. */
  55. private $password;
  56. /**
  57. * @Groups({"user","widget"})
  58. * @ORM\Column(type="boolean", nullable=true)
  59. * @OA\Property(
  60. * type="boolean",
  61. * format="string"
  62. * )
  63. */
  64. private $isActive;
  65. /**
  66. * @ORM\Column(type="datetime_immutable", nullable=true)
  67. */
  68. private $lastLogin;
  69. /**
  70. * @ORM\OneToMany(targetEntity=Widget::class, mappedBy="Client", cascade={"persist"})
  71. */
  72. private $widgets;
  73. /**
  74. * @Groups({"user"})
  75. * @ORM\Column(type="array")
  76. * @OA\Property(
  77. * type="string[]",
  78. * )
  79. */
  80. private $roles = [];
  81. /**
  82. * @ORM\OneToMany(targetEntity=HistorySend::class, mappedBy="Client")
  83. */
  84. private $historySends;
  85. /**
  86. * @ORM\ManyToMany(targetEntity=Widget::class, inversedBy="widgetUsers")
  87. */
  88. private $availableWidgets;
  89. public function __toString()
  90. {
  91. return $this->getName();
  92. }
  93. public function __construct()
  94. {
  95. $this->widgets = new ArrayCollection();
  96. $this->roles = ['ROLE_MERCHANT'];
  97. $this->id = Uuid::uuid4();
  98. $this->historySends = new ArrayCollection();
  99. $this->availableWidgets = new ArrayCollection();
  100. }
  101. public function hasRole(string $role)
  102. {
  103. return in_array($role, $this->getRoles());
  104. }
  105. public function getId(): ?UuidInterface
  106. {
  107. return $this->id;
  108. }
  109. public function getName(): ?string
  110. {
  111. return $this->name;
  112. }
  113. public function setName(string $name): self
  114. {
  115. $this->name = $name;
  116. return $this;
  117. }
  118. public function getEmail(): ?string
  119. {
  120. return $this->email;
  121. }
  122. public function setEmail(string $email): self
  123. {
  124. $this->email = $email;
  125. return $this;
  126. }
  127. public function getPassword(): ?string
  128. {
  129. return $this->password;
  130. }
  131. public function setPassword(string $password): self
  132. {
  133. $this->password = $password;
  134. return $this;
  135. }
  136. public function getIsActive(): ?bool
  137. {
  138. return $this->isActive;
  139. }
  140. public function setIsActive(?bool $isActive): self
  141. {
  142. $this->isActive = $isActive;
  143. return $this;
  144. }
  145. public function getLastLogin(): ?\DateTimeImmutable
  146. {
  147. return $this->lastLogin;
  148. }
  149. public function setLastLogin(?\DateTimeImmutable $lastLogin): self
  150. {
  151. $this->lastLogin = $lastLogin;
  152. return $this;
  153. }
  154. /**
  155. * @return Collection|Widget[]
  156. */
  157. public function getWidgets(): Collection
  158. {
  159. return $this->widgets;
  160. }
  161. public function addWidget(Widget $widget): self
  162. {
  163. if (!$this->widgets->contains($widget)) {
  164. $this->widgets[] = $widget;
  165. $widget->setClient($this);
  166. }
  167. return $this;
  168. }
  169. public function removeWidget(Widget $widget): self
  170. {
  171. if ($this->widgets->removeElement($widget)) {
  172. // set the owning side to null (unless already changed)
  173. if ($widget->getClient() === $this) {
  174. $widget->setClient(null);
  175. }
  176. }
  177. return $this;
  178. }
  179. /**
  180. * @return string[]|null
  181. */
  182. public function getRoles(): ?array
  183. {
  184. return $this->roles ?? [];
  185. }
  186. public function setRoles(array $roles): self
  187. {
  188. $this->roles = $roles;//array_unique(array_merge(['ROLE_MERCHANT'], $roles));
  189. return $this;
  190. }
  191. public function getSalt():?string
  192. {
  193. return '';
  194. }
  195. public function eraseCredentials()
  196. {
  197. // TODO: Implement eraseCredentials() method.
  198. }
  199. /**
  200. * @return string
  201. */
  202. public function getUsername():string
  203. {
  204. return $this->getEmail();
  205. }
  206. public function __call($name, $arguments)
  207. {
  208. // TODO: Implement @method string getUserIdentifier()
  209. }
  210. /**
  211. * @return Collection|HistorySend[]
  212. */
  213. public function getHistorySends(): Collection
  214. {
  215. return $this->historySends;
  216. }
  217. public function addHistorySend(HistorySend $historySend): self
  218. {
  219. if (!$this->historySends->contains($historySend)) {
  220. $this->historySends[] = $historySend;
  221. $historySend->setClient($this);
  222. }
  223. return $this;
  224. }
  225. public function removeHistorySend(HistorySend $historySend): self
  226. {
  227. if ($this->historySends->removeElement($historySend)) {
  228. // set the owning side to null (unless already changed)
  229. if ($historySend->getClient() === $this) {
  230. $historySend->setClient(null);
  231. }
  232. }
  233. return $this;
  234. }
  235. /**
  236. * @return Collection<int, Widget>
  237. */
  238. public function getAvailableWidgets(): Collection
  239. {
  240. return $this->availableWidgets;
  241. }
  242. public function addAvailableWidgets(Widget $widget): self
  243. {
  244. if (!$this->availableWidgets->contains($widget)) {
  245. $this->availableWidgets[] = $widget;
  246. }
  247. return $this;
  248. }
  249. public function removeAvailableWidgets(Widget $widget): self
  250. {
  251. $this->availableWidgets->removeElement($widget);
  252. return $this;
  253. }
  254. }