src/Entity/User.php line 83

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiProperty;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Annotation\ApiFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  8. use App\Repository\UserRepository;
  9. use App\Trait\TimestampableEntity;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. use Symfony\Component\Serializer\Annotation\MaxDepth;
  18. #[ORM\Entity(repositoryClassUserRepository::class)]
  19. #[ApiResource(
  20.     iri'User'
  21.     itemOperations: [
  22.         'get' => [
  23.             // 'security' => "is_granted('ROLE_USER')",
  24.             'normalization_context' => [
  25.                 'groups' => 'user:item:get',
  26.                 'enable_max_depth' => true
  27.             ]
  28.         ],
  29.         'put' => [
  30.             // 'security' => "is_granted('ROLE_USER')",
  31.             'normalization_context' => [
  32.                 'groups' => 'user:item:put',
  33.                 'enable_max_depth' => true
  34.             ],
  35.             'denormalization_context' => [
  36.                 'groups' => 'user:item:put',
  37.                 'enable_max_depth' => true
  38.             ]
  39.         ],
  40.         'delete' => [
  41.             // 'security' => "is_granted('ROLE_USER')",
  42.         ]
  43.     ],
  44.     collectionOperations: [
  45.         'get' => [
  46.             // 'security' => "is_granted('ROLE_USER')",
  47.             'normalization_context' => [
  48.                 'groups' => ['user:collection:get''createdAt'],
  49.                 'enable_max_depth' => true
  50.             ]
  51.         ],
  52.         'post' => [
  53.             // 'security' => "is_granted('ROLE_USER')",
  54.             'normalization_context' => [
  55.                 'groups' => 'user:collection:post',
  56.                 'enable_max_depth' => true
  57.             ],
  58.             'denormalization_context' => [
  59.                 'groups' => 'user:collection:post',
  60.                 'enable_max_depth' => true
  61.             ]
  62.         ],
  63.     ]
  64. )]
  65. #[ApiFilter(SearchFilter::class, properties: [
  66.     'brokers.broker' => 'exact'
  67.     'insureds.insured' => 'exact',
  68.     'insurers.insurer' => 'exact',
  69.     'channels.channel' => 'exact',
  70.     'roles' => 'partial',
  71.     'email' => 'partial',
  72.     'name' => 'partial',
  73.     'createdAt' => 'start',
  74. ])]
  75. #[ApiFilter(OrderFilter::class, properties: [
  76.     'name',
  77.     'email'
  78.     'createdAt',
  79. ])]
  80. class User implements UserInterfacePasswordAuthenticatedUserInterface
  81. {
  82.     use TimestampableEntity;
  83.     
  84.     #[ORM\Id]
  85.     #[ORM\GeneratedValue]
  86.     #[ORM\Column(type'integer')]
  87.     #[Groups([
  88.         'user:collection:get',
  89.         'user:item:get',
  90.     ])]
  91.     private $id;
  92.     /**
  93.      * The name of the item.
  94.      *
  95.      * @see https://schema.org/name
  96.      */
  97.     #[ORM\Column(type'string'nullablefalse)]
  98.     #[ApiProperty(iri'https://schema.org/name')]
  99.     #[Assert\Type('string')]
  100.     #[Groups([
  101.         'user:collection:get',
  102.         'user:collection:post',
  103.         'user:item:get',
  104.         'user:item:put',
  105.     ])]
  106.     private ?string $name null;
  107.     #[ORM\Column(type'string'length180uniquetrue)]
  108.     #[ApiProperty(iri'https://schema.org/email')]
  109.     #[Groups([
  110.         'user:collection:get',
  111.         'user:collection:post',
  112.         'user:item:get',
  113.         'user:item:put',
  114.     ])]
  115.     private $email;
  116.     #[ORM\Column(type'json')]
  117.     #[ApiProperty()]
  118.     #[Groups([
  119.         'user:collection:get',
  120.         'user:collection:post',
  121.         'user:item:get',
  122.         'user:item:put',
  123.     ])]
  124.     private array $roles = [];
  125.     #[ORM\Column(type'string')]
  126.     private $password;
  127.     #[Groups([
  128.         'user:collection:post',
  129.         'user:item:put',
  130.     ])]
  131.     protected $plainPassword;
  132.     #[ORM\Column(type'string'length255nullabletrue)]
  133.     private $confirmationToken;
  134.     #[ORM\OneToMany(mappedBy'user'targetEntityBrokerUser::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  135.     #[Groups([
  136.         'user:collection:post',
  137.         'user:item:get',
  138.         'user:item:put',
  139.     ])]
  140.     #[MaxDepth(1)]
  141.     private $brokers;
  142.     #[ORM\OneToMany(mappedBy'user'targetEntityInsuredUser::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  143.     #[Groups([
  144.         'user:collection:post',
  145.         'user:item:get',
  146.         'user:item:put',
  147.     ])]
  148.     #[MaxDepth(1)]
  149.     private $insureds;
  150.     #[ORM\OneToMany(mappedBy'user'targetEntityInsurerUser::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  151.     #[Groups([
  152.         'user:collection:post',
  153.         'user:item:get',
  154.         'user:item:put',
  155.     ])]
  156.     #[MaxDepth(1)]
  157.     private $insurers;
  158.     #[ORM\OneToMany(mappedBy'user'targetEntityChannelUser::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  159.     #[Groups([
  160.         'user:collection:post',
  161.         'user:item:get',
  162.         'user:item:put',
  163.     ])]
  164.     #[MaxDepth(1)]
  165.     private $channels;
  166.     #[ApiProperty(iri'https://schema.org/image')]
  167.     #[ORM\ManyToOne(targetEntityMedia::class, cascade: ["persist""remove"])]
  168.     #[ORM\JoinColumn(nullabletrue)]
  169.     #[Groups([
  170.         'user:collection:get',
  171.         'user:collection:post',
  172.         'user:item:get',
  173.         'user:item:put',
  174.     ])]
  175.     #[MaxDepth(1)]
  176.     private $avatar;
  177.     public function __construct()
  178.     {
  179.         $this->brokers = new ArrayCollection();
  180.         $this->insureds = new ArrayCollection();
  181.         $this->insurers = new ArrayCollection();
  182.         $this->channels = new ArrayCollection();
  183.     }
  184.     public function getId(): ?int
  185.     {
  186.         return $this->id;
  187.     }
  188.     public function setName(?string $name): void
  189.     {
  190.         $this->name $name;
  191.     }
  192.     public function getName(): ?string
  193.     {
  194.         return $this->name;
  195.     }
  196.     public function getEmail(): ?string
  197.     {
  198.         return $this->email;
  199.     }
  200.     public function setEmail(string $email): self
  201.     {
  202.         $this->email $email;
  203.         return $this;
  204.     }
  205.     /**
  206.      * A visual identifier that represents this user.
  207.      *
  208.      * @see UserInterface
  209.      */
  210.     public function getUserIdentifier(): string
  211.     {
  212.         return (string) $this->email;
  213.     }
  214.     /**
  215.      * @see UserInterface
  216.      */
  217.     public function getRoles(): array
  218.     {
  219.         $roles $this->roles;
  220.         // guarantee every user at least has ROLE_USER
  221.         $roles[] = 'ROLE_USER';
  222.         return array_unique($roles);
  223.     }
  224.     public function setRoles(array $roles): self
  225.     {
  226.         $this->roles $roles;
  227.         return $this;
  228.     }
  229.     /**
  230.      * @see PasswordAuthenticatedUserInterface
  231.      */
  232.     public function getPassword(): string
  233.     {
  234.         return $this->password;
  235.     }
  236.     public function setPassword(string $password): self
  237.     {
  238.         $this->password $password;
  239.         return $this;
  240.     }
  241.     public function getPlainPassword(): ?string
  242.     {
  243.         return $this->plainPassword;
  244.     }
  245.     public function setPlainPassword(?string $plainPassword): self
  246.     {
  247.         $this->plainPassword $plainPassword;
  248.         return $this;
  249.     }
  250.     /**
  251.      * @see UserInterface
  252.      */
  253.     public function eraseCredentials(): void
  254.     {
  255.         // If you store any temporary, sensitive data on the user, clear it here
  256.         // $this->plainPassword = null;
  257.     }
  258.     /**
  259.      * @return Collection|BrokerUser[]
  260.      */
  261.     public function getBrokers(): Collection
  262.     {
  263.         return $this->brokers;
  264.     }
  265.     public function addBroker(BrokerUser $broker): self
  266.     {
  267.         if (!$this->brokers->contains($broker)) {
  268.             $this->brokers[] = $broker;
  269.             $broker->setUser($this);
  270.         }
  271.         return $this;
  272.     }
  273.     public function removeBroker(BrokerUser $broker): self
  274.     {
  275.         if ($this->brokers->removeElement($broker)) {
  276.             // set the owning side to null (unless already changed)
  277.             if ($broker->getUser() === $this) {
  278.                 $broker->setUser(null);
  279.             }
  280.         }
  281.         return $this;
  282.     }
  283.     /**
  284.      * @return Collection|InsuredUser[]
  285.      */
  286.     public function getInsureds(): Collection
  287.     {
  288.         return $this->insureds;
  289.     }
  290.     public function addInsured(InsuredUser $insured): self
  291.     {
  292.         if (!$this->insureds->contains($insured)) {
  293.             $this->insureds[] = $insured;
  294.             $insured->setUser($this);
  295.         }
  296.         return $this;
  297.     }
  298.     public function removeInsured(InsuredUser $insured): self
  299.     {
  300.         if ($this->insureds->removeElement($insured)) {
  301.             // set the owning side to null (unless already changed)
  302.             if ($insured->getUser() === $this) {
  303.                 $insured->setUser(null);
  304.             }
  305.         }
  306.         return $this;
  307.     }
  308.     /**
  309.      * @return Collection|InsurerUser[]
  310.      */
  311.     public function getInsurers(): Collection
  312.     {
  313.         return $this->insurers;
  314.     }
  315.     public function addInsurer(InsurerUser $insurer): self
  316.     {
  317.         if (!$this->insurers->contains($insurer)) {
  318.             $this->insurers[] = $insurer;
  319.             $insurer->setUser($this);
  320.         }
  321.         return $this;
  322.     }
  323.     public function removeInsurer(InsurerUser $insurer): self
  324.     {
  325.         if ($this->insurers->removeElement($insurer)) {
  326.             // set the owning side to null (unless already changed)
  327.             if ($insurer->getUser() === $this) {
  328.                 $insurer->setUser(null);
  329.             }
  330.         }
  331.         return $this;
  332.     }
  333.     public function getConfirmationToken(): ?string
  334.     {
  335.         return $this->confirmationToken;
  336.     }
  337.     public function setConfirmationToken(?string $confirmationToken): self
  338.     {
  339.         $this->confirmationToken $confirmationToken;
  340.         return $this;
  341.     }
  342.     /**
  343.      * @return Collection|ChannelUser[]
  344.      */
  345.     public function getChannels(): Collection
  346.     {
  347.         return $this->channels;
  348.     }
  349.     public function addChannel(ChannelUser $channel): self
  350.     {
  351.         if (!$this->channels->contains($channel)) {
  352.             $this->channels[] = $channel;
  353.             $channel->setUser($this);
  354.         }
  355.         return $this;
  356.     }
  357.     public function removeChannel(ChannelUser $channel): self
  358.     {
  359.         if ($this->channels->removeElement($channel)) {
  360.             // set the owning side to null (unless already changed)
  361.             if ($channel->getUser() === $this) {
  362.                 $channel->setUser(null);
  363.             }
  364.         }
  365.         return $this;
  366.     }
  367.     public function setAvatar(?Media $avatar): self
  368.     {
  369.         $this->avatar $avatar;
  370.         return $this;
  371.     }
  372.     public function getAvatar(): ?Media
  373.     {
  374.         return $this->avatar;
  375.     }
  376. }