src/Entity/User.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Component\Serializer\Normalizer\ProxyAwareObjectNormalizer;
  4. use DateTime;
  5. use DateTimeInterface;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\Common\Collections\Criteria;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Symfony\Component\Serializer\Annotation as SA;
  11. /**
  12.  * User
  13.  *
  14.  * @ORM\Table(name="users", indexes={@ORM\Index(name="type", columns={"type"})})
  15.  * @ORM\InheritanceType("JOINED")
  16.  * @ORM\DiscriminatorColumn(name="type", type="string")
  17.  * @ORM\DiscriminatorMap({"api" = "UserApi", "interactive" = "UserInteractive"})
  18.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  19.  */
  20. class User extends Entity
  21. {
  22.     public const TYPE = [
  23.         'interactive' => 'interactive',
  24.         'api' => 'api'
  25.     ];
  26.     public const SENTRY_LOGGING = [
  27.         'default' => 'default',
  28.         'off' => 'off'
  29.     ];
  30.     public const SERIALIZATION_GROUP = [
  31.         User::class,
  32.         UserAllowedClient::class,
  33.     ];
  34.     public const ADMIN_SERIALIZATION_GROUP = [
  35.         User::class,
  36.         UserInteractive::class,
  37.         UserApi::class,
  38.         Account::class,
  39.         Role::class,
  40.         UserAllowedClient::class
  41.     ];
  42.     public const SERIALIZATION_CONTEXT = [
  43.         'read' => ['groups' => ['user_general''user_details'], ProxyAwareObjectNormalizer::RESOLVE_PROXY_CLASSES => self::SERIALIZATION_GROUP],
  44.         'delete' => ['groups' => ['user_general']],
  45.         'search' => ['groups' => ['user_general''user_search'], ProxyAwareObjectNormalizer::RESOLVE_PROXY_CLASSES => self::SEARCH_SERIALIZATION_GROUP],
  46.         'admin_read' => ['groups' => ['user_general''user_details''user_search''user_read'], ProxyAwareObjectNormalizer::RESOLVE_PROXY_CLASSES => self::ADMIN_SERIALIZATION_GROUP]
  47.     ];
  48.     const SEARCH_SERIALIZATION_GROUP = [Role::class, Account::class, UserInteractive::class];
  49.     /**
  50.      * @var int
  51.      *
  52.      * @ORM\Column(name="id", type="bigint", nullable=false, options={"unsigned"=true})
  53.      * @ORM\Id
  54.      * @ORM\GeneratedValue(strategy="IDENTITY")
  55.      * @SA\Groups({"user_general", "account_search"})
  56.      */
  57.     private $id;
  58.     /**
  59.      * @var DateTime
  60.      *
  61.      * @ORM\Column(name="created", type="datetime", nullable=false, options={"default"="CURRENT_TIMESTAMP"})
  62.      * @SA\Groups({"user_general"})
  63.      */
  64.     private $created null;
  65.     /**
  66.      * @var string
  67.      *
  68.      * @ORM\Column(name="status", type="string", length=0, nullable=false, options={"default"="inactive"})
  69.      * @SA\Groups({"user_general"})
  70.      */
  71.     private $status self::STATUS['inactive'];
  72.     /**
  73.      * @var string
  74.      *
  75.      * @ORM\Column(name="sentry_logging", type="string", length=0, nullable=false, options={"default"="default"})
  76.      * @SA\Groups({"user_general"})
  77.      */
  78.     private $sentryLogging self::SENTRY_LOGGING['default'];
  79.     /**
  80.      * @var ArrayCollection
  81.      *
  82.      * @ORM\OneToMany(targetEntity="Role", mappedBy="user", cascade={"PERSIST"})
  83.      * @SA\Ignore
  84.      */
  85.     private $roles;
  86.     /**
  87.      * @var ArrayCollection|UserAllowedClient[]
  88.      *
  89.      * @ORM\OneToMany(targetEntity="UserAllowedClient", mappedBy="user", cascade={"PERSIST"})
  90.      * @SA\Groups({"user_details"})
  91.      */
  92.     private $allowedClients;
  93.     public function __construct(Account $account, ?string $roleName null, ?string $sentryLogging null)
  94.     {
  95.         $this->roles = new ArrayCollection();
  96.         $this->allowedClients = new ArrayCollection();
  97.         $this->created = new DateTime();
  98.         $this->status self::STATUS['active'];
  99.         $this->sentryLogging $sentryLogging ?? $this->sentryLogging;
  100.         $roleName ??= Role::ROLES['owner'];
  101.         $role = new Role($account$this$roleName);
  102.         $this->addRole($role);
  103.     }
  104.     public function getId(): ?int
  105.     {
  106.         return $this->id;
  107.     }
  108.     public function getCreated(): ?DateTimeInterface
  109.     {
  110.         return $this->created;
  111.     }
  112.     public function setCreated(DateTimeInterface $created): self
  113.     {
  114.         $this->created $created;
  115.         return $this;
  116.     }
  117.     public function getStatus(): ?string
  118.     {
  119.         return $this->status;
  120.     }
  121.     public function setStatus(string $status): self
  122.     {
  123.         $this->status $status;
  124.         return $this;
  125.     }
  126.     public function getType(): ?string
  127.     {
  128.         return $this instanceof UserApi self::TYPE['api'] : self::TYPE['interactive'];
  129.     }
  130.     public function getSentryLogging(): ?string
  131.     {
  132.         return $this->sentryLogging;
  133.     }
  134.     public function setSentryLogging(string $sentryLogging): self
  135.     {
  136.         $this->sentryLogging $sentryLogging;
  137.         return $this;
  138.     }
  139.     /**
  140.      * @return ArrayCollection<int, Role>|Role[]
  141.      */
  142.     public function getRoles(): Collection
  143.     {
  144.         return $this->roles;
  145.     }
  146.     public function addRole(Role $role): self
  147.     {
  148.         if (!$this->roles->contains($role)) {
  149.             $this->roles[] = $role;
  150.             $role->setUser($this);
  151.         }
  152.         return $this;
  153.     }
  154.     public function removeRole(Role $role): self
  155.     {
  156.         if ($this->roles->removeElement($role)) {
  157.             // set the owning side to null (unless already changed)
  158.             if ($role->getUser() === $this) {
  159.                 $role->setUser(null);
  160.             }
  161.         }
  162.         return $this;
  163.     }
  164.     public function addAllowedClient(UserAllowedClient $client)
  165.     {
  166.         if (!$this->allowedClients->contains($client)) {
  167.             $this->allowedClients->add($client);
  168.             $client->setUser($this);
  169.         }
  170.     }
  171.     public function removeAllowedClient(UserAllowedClient $client)
  172.     {
  173.         if ($this->allowedClients->contains($client)) {
  174.             $this->allowedClients->removeElement($client);
  175.             $client->setUser(null);
  176.         }
  177.     }
  178.     /**
  179.      * @return UserAllowedClient[]|ArrayCollection
  180.      */
  181.     public function getAllowedClients()
  182.     {
  183.         return $this->allowedClients;
  184.     }
  185.     
  186.     public function softDelete(): Entity
  187.     {
  188.         $this->setStatus(self::STATUS['inactive']);
  189.         foreach ($this->getRoles()->matching(Criteria::create()->where(Criteria::expr()->eq('status'self::STATUS['active']))) as $role) {
  190.             $role->softDelete();
  191.         }
  192.         return $this;
  193.     }
  194.     /**
  195.      * @SA\Groups({"user_search", "user_read"})
  196.      * @return Collection<int, Account>|Account[]
  197.      */
  198.     public function getAccounts(): Collection
  199.     {
  200.         return $this->getRoles()->map(function (Role $role) {
  201.             return $role->getAccount();
  202.         });
  203.     }
  204. }