<?php
namespace App\Entity;
use App\Repository\WidgetUserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use OpenApi\Annotations as OA;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=WidgetUserRepository::class)
* @method string getUserIdentifier()
*/
class WidgetUser implements UserInterface, PasswordAuthenticatedUserInterface
{
public const ROLES = [
'Клиент' => 'ROLE_CLIENT',
'Сотрудник саппорта' => 'ROLE_MERCHANT',
'Менеджер' => 'ROLE_MANAGER',
'Администратор' => 'ROLE_SUPERADMINISTRATOR',
];
/**
* @Groups({"user","user_uuid","widget"})
* @var \Ramsey\Uuid\UuidInterface
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="\Ramsey\Uuid\Doctrine\UuidGenerator")
* @OA\Property(
* type="string"
* )
* @Assert\NotBlank()
*/
private $id;
/**
* @Groups({"user","widget"})
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @Groups({"user","widget"})
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
*/
private $email;
/**
* @Groups({"user"})
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
*/
private $password;
/**
* @Groups({"user","widget"})
* @ORM\Column(type="boolean", nullable=true)
* @OA\Property(
* type="boolean",
* format="string"
* )
*/
private $isActive;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $lastLogin;
/**
* @ORM\OneToMany(targetEntity=Widget::class, mappedBy="Client", cascade={"persist"})
*/
private $widgets;
/**
* @Groups({"user"})
* @ORM\Column(type="array")
* @OA\Property(
* type="string[]",
* )
*/
private $roles = [];
/**
* @ORM\OneToMany(targetEntity=HistorySend::class, mappedBy="Client")
*/
private $historySends;
/**
* @ORM\ManyToMany(targetEntity=Widget::class, inversedBy="widgetUsers")
*/
private $availableWidgets;
public function __toString()
{
return $this->getName();
}
public function __construct()
{
$this->widgets = new ArrayCollection();
$this->roles = ['ROLE_MERCHANT'];
$this->id = Uuid::uuid4();
$this->historySends = new ArrayCollection();
$this->availableWidgets = new ArrayCollection();
}
public function hasRole(string $role)
{
return in_array($role, $this->getRoles());
}
public function getId(): ?UuidInterface
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(?bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getLastLogin(): ?\DateTimeImmutable
{
return $this->lastLogin;
}
public function setLastLogin(?\DateTimeImmutable $lastLogin): self
{
$this->lastLogin = $lastLogin;
return $this;
}
/**
* @return Collection|Widget[]
*/
public function getWidgets(): Collection
{
return $this->widgets;
}
public function addWidget(Widget $widget): self
{
if (!$this->widgets->contains($widget)) {
$this->widgets[] = $widget;
$widget->setClient($this);
}
return $this;
}
public function removeWidget(Widget $widget): self
{
if ($this->widgets->removeElement($widget)) {
// set the owning side to null (unless already changed)
if ($widget->getClient() === $this) {
$widget->setClient(null);
}
}
return $this;
}
/**
* @return string[]|null
*/
public function getRoles(): ?array
{
return $this->roles ?? [];
}
public function setRoles(array $roles): self
{
$this->roles = $roles;//array_unique(array_merge(['ROLE_MERCHANT'], $roles));
return $this;
}
public function getSalt():?string
{
return '';
}
public function eraseCredentials()
{
// TODO: Implement eraseCredentials() method.
}
/**
* @return string
*/
public function getUsername():string
{
return $this->getEmail();
}
public function __call($name, $arguments)
{
// TODO: Implement @method string getUserIdentifier()
}
/**
* @return Collection|HistorySend[]
*/
public function getHistorySends(): Collection
{
return $this->historySends;
}
public function addHistorySend(HistorySend $historySend): self
{
if (!$this->historySends->contains($historySend)) {
$this->historySends[] = $historySend;
$historySend->setClient($this);
}
return $this;
}
public function removeHistorySend(HistorySend $historySend): self
{
if ($this->historySends->removeElement($historySend)) {
// set the owning side to null (unless already changed)
if ($historySend->getClient() === $this) {
$historySend->setClient(null);
}
}
return $this;
}
/**
* @return Collection<int, Widget>
*/
public function getAvailableWidgets(): Collection
{
return $this->availableWidgets;
}
public function addAvailableWidgets(Widget $widget): self
{
if (!$this->availableWidgets->contains($widget)) {
$this->availableWidgets[] = $widget;
}
return $this;
}
public function removeAvailableWidgets(Widget $widget): self
{
$this->availableWidgets->removeElement($widget);
return $this;
}
}