src/Entity/User.php line 109

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 ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  9. use App\Repository\UserRepository;
  10. use App\Trait\TimestampableEntity;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  15. use Symfony\Component\Security\Core\User\UserInterface;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. use Symfony\Component\Serializer\Annotation\Groups;
  18. use Symfony\Component\Serializer\Annotation\MaxDepth;
  19. use Ramsey\Uuid\Uuid;
  20. use Ramsey\Uuid\UuidInterface;
  21. #[ORM\Entity(repositoryClassUserRepository::class)]
  22. #[ApiResource(
  23.     iri'User'
  24.     itemOperations: [
  25.         'get' => [
  26.             'security' => "
  27.                 is_granted('ROLE_ADMIN') or 
  28.                 is_granted('ROLE_OPERATIONS_COORDINATOR') or 
  29.                 is_granted('ROLE_RESEARCH_ANALYST')
  30.             ",
  31.             'normalization_context' => [
  32.                 'groups' => 'user:item:get',
  33.                 'enable_max_depth' => true
  34.             ]
  35.         ],
  36.         'put' => [
  37.             'security' => "
  38.                 is_granted('ROLE_ADMIN') or 
  39.                 is_granted('ROLE_OPERATIONS_COORDINATOR') or 
  40.                 is_granted('ROLE_RESEARCH_ANALYST')
  41.             ",
  42.             'normalization_context' => [
  43.                 'groups' => 'user:item:put',
  44.                 'enable_max_depth' => true
  45.             ],
  46.             'denormalization_context' => [
  47.                 'groups' => 'user:item:put',
  48.                 'enable_max_depth' => true
  49.             ]
  50.         ],
  51.         'delete' => [
  52.             'security' => "
  53.                 is_granted('ROLE_ADMIN') or 
  54.                 is_granted('ROLE_OPERATIONS_COORDINATOR') or 
  55.                 is_granted('ROLE_RESEARCH_ANALYST')
  56.             ",
  57.         ]
  58.     ],
  59.     collectionOperations: [
  60.         'get' => [
  61.             'security' => "
  62.                 is_granted('ROLE_ADMIN') or 
  63.                 is_granted('ROLE_OPERATIONS_COORDINATOR') or 
  64.                 is_granted('ROLE_RESEARCH_ANALYST')
  65.             ",
  66.             'normalization_context' => [
  67.                 'groups' => [
  68.                     'user:collection:get'
  69.                     'createdAt'
  70.                 ],
  71.                 'enable_max_depth' => true
  72.             ]
  73.         ],
  74.         'post' => [
  75.             'security' => "
  76.                 is_granted('ROLE_ADMIN') or 
  77.                 is_granted('ROLE_OPERATIONS_COORDINATOR') or 
  78.                 is_granted('ROLE_RESEARCH_ANALYST')
  79.             ",
  80.             'normalization_context' => [
  81.                 'groups' => 'user:collection:post',
  82.                 'enable_max_depth' => true
  83.             ],
  84.             'denormalization_context' => [
  85.                 'groups' => 'user:collection:post',
  86.                 'enable_max_depth' => true
  87.             ]
  88.         ],
  89.     ]
  90. )]
  91. #[ApiFilter(SearchFilter::class, properties: [
  92.     'roles' => 'partial',
  93.     'email' => 'partial',
  94.     'name' => 'partial',
  95.     'roles' => 'partial',
  96.     'desk' => 'exact',
  97.     'createdAt' => 'start',
  98. ])]
  99. #[ApiFilter(OrderFilter::class, properties: [
  100.     'name',
  101.     'email',
  102.     'desk',
  103.     'createdAt',
  104. ])]
  105. #[ApiFilter(PropertyFilter::class)]
  106. class User implements UserInterfacePasswordAuthenticatedUserInterface
  107. {
  108.     use TimestampableEntity;
  109.     public const ROLE_USER 'ROLE_USER';
  110.     public const ROLE_OPERATOR 'ROLE_OPERATOR';
  111.     public const ROLE_OPERATIONS_COORDINATOR 'ROLE_OPERATIONS_COORDINATOR';
  112.     public const ROLE_BROKER 'ROLE_BROKER';
  113.     public const ROLE_FINANCIAL_MANAGER 'ROLE_FINANCIAL_MANAGER';
  114.     public const ROLE_RESEARCH_ANALYST 'ROLE_RESEARCH_ANALYST';
  115.     public const ROLE_INTERN 'ROLE_INTERN';
  116.     public const ROLE_ADMIN 'ROLE_ADMIN';
  117.     public const ROLE_SUPERADMIN 'ROLE_SUPERADMIN';
  118.     public const ROLES = [
  119.         self::ROLE_USER,
  120.         self::ROLE_OPERATOR,
  121.         self::ROLE_OPERATIONS_COORDINATOR,
  122.         self::ROLE_BROKER,
  123.         self::ROLE_FINANCIAL_MANAGER,
  124.         self::ROLE_RESEARCH_ANALYST,
  125.         self::ROLE_INTERN,
  126.         self::ROLE_ADMIN,
  127.         self::ROLE_SUPERADMIN,
  128.     ];
  129.     public const DESK_OPERATIONS 'Operations';
  130.     public const DESK_DRY 'Dry';
  131.     public const DESK_TANKERS 'Tankers';
  132.     public const DESK_ACCOUNTING 'Accounting';
  133.     public const DESK_RESEARCH 'Research';
  134.     public const DESKS = [
  135.         self::DESK_OPERATIONS,
  136.         self::DESK_DRY,
  137.         self::DESK_TANKERS,
  138.         self::DESK_ACCOUNTING,
  139.         self::DESK_RESEARCH,
  140.     ];
  141.     #[ORM\Id]
  142.     #[ORM\GeneratedValue(strategy'NONE')]
  143.     #[ORM\Column(type'uuid'uniquetrue)]
  144.     private ?UuidInterface $id null;
  145.     #[ORM\Column(type'string'nullablefalse)]
  146.     #[ApiProperty()]
  147.     #[Assert\Type('string')]
  148.     #[Groups([
  149.         'user:collection:get',
  150.         'user:collection:post',
  151.         'user:item:get',
  152.         'user:item:put',
  153.         'contract:item:get'
  154.         'contract:item:put'
  155.         'contract:collection:get'
  156.         'contract:collection:post',
  157.         'market_rumor:item:get'
  158.         'market_rumor:item:put'
  159.         'market_rumor:collection:get'
  160.         'market_rumor:collection:post'
  161.     ])]
  162.     private ?string $name null;
  163.     #[ORM\Column(type'string'length180uniquetrue)]
  164.     #[ApiProperty()]
  165.     #[Groups([
  166.         'user:collection:get',
  167.         'user:collection:post',
  168.         'user:item:get',
  169.         'user:item:put',
  170.     ])]
  171.     private $email;
  172.     #[ORM\Column(type'json')]
  173.     #[ApiProperty()]
  174.     #[Groups([
  175.         'user:collection:get',
  176.         'user:collection:post',
  177.         'user:item:get',
  178.         'user:item:put',
  179.     ])]
  180.     private array $roles = [];
  181.     #[ORM\Column(type'string')]
  182.     private $password;
  183.     #[Groups([
  184.         'user:collection:post',
  185.         'user:item:put',
  186.     ])]
  187.     protected $plainPassword;
  188.     #[ORM\Column(type'string'length255nullabletrue)]
  189.     private $confirmationToken;
  190.     #[ApiProperty()]
  191.     #[ORM\ManyToOne(
  192.         targetEntityMedia::class, 
  193.         cascade: ["persist""remove"]
  194.     )]
  195.     #[ORM\JoinColumn(nullabletrue)]
  196.     #[Groups([
  197.         'user:collection:get',
  198.         'user:collection:post',
  199.         'user:item:get',
  200.         'user:item:put',
  201.     ])]
  202.     #[MaxDepth(1)]
  203.     private $avatar;
  204.     #[ORM\Column(type'string'nullabletrue)]
  205.     #[ApiProperty()]
  206.     #[Assert\Type('string')]
  207.     #[Assert\Choice(
  208.         choicesself::DESKS
  209.         message'The desk is not valid.'
  210.     )]
  211.     #[Groups([
  212.         'user:collection:get',
  213.         'user:collection:post',
  214.         'user:item:get',
  215.         'user:item:put',
  216.         'contract:collection:get'
  217.     ])]
  218.     private ?string $desk null;
  219.     #[ORM\OneToMany(
  220.         targetEntityContract::class, 
  221.         mappedBy'broker'
  222.     )]
  223.     private ?Collection $contracts null;
  224.     #[ORM\OneToMany(
  225.         targetEntityContract::class, 
  226.         mappedBy'operator'
  227.     )]
  228.     private ?Collection $operations null;
  229.     // #[ORM\OneToMany(
  230.     //     targetEntity: MarketRumor::class, 
  231.     //     mappedBy: 'broker'
  232.     // )]
  233.     // private ?Collection $marketRumors = null;
  234.     public function __construct()
  235.     {
  236.         $this->id Uuid::uuid4();
  237.         $this->contracts = new ArrayCollection();
  238.         $this->operations = new ArrayCollection();
  239.         // $this->marketRumors = new ArrayCollection();
  240.     }
  241.     public function getId(): ?UuidInterface
  242.     {
  243.         return $this->id;
  244.     }
  245.     public function setName(?string $name): void
  246.     {
  247.         $this->name $name;
  248.     }
  249.     public function getName(): ?string
  250.     {
  251.         return $this->name;
  252.     }
  253.     public function getEmail(): ?string
  254.     {
  255.         return $this->email;
  256.     }
  257.     public function setEmail(string $email): self
  258.     {
  259.         $this->email $email;
  260.         return $this;
  261.     }
  262.     public function getUserIdentifier(): string
  263.     {
  264.         return (string) $this->email;
  265.     }
  266.     public function getRoles(): array
  267.     {
  268.         $roles $this->roles;
  269.         // guarantee every user at least has ROLE_USER
  270.         $roles[] = self::ROLE_USER;
  271.         return array_unique($roles);
  272.     }
  273.     public function setRoles(array $roles): self
  274.     {
  275.         $this->roles $roles;
  276.         return $this;
  277.     }
  278.     public function getPassword(): string
  279.     {
  280.         return $this->password;
  281.     }
  282.     public function setPassword(string $password): self
  283.     {
  284.         $this->password $password;
  285.         return $this;
  286.     }
  287.     public function getPlainPassword(): ?string
  288.     {
  289.         return $this->plainPassword;
  290.     }
  291.     public function setPlainPassword(?string $plainPassword): self
  292.     {
  293.         $this->plainPassword $plainPassword;
  294.         return $this;
  295.     }
  296.     public function eraseCredentials(): void
  297.     {
  298.         // If you store any temporary, sensitive data on the user, clear it here
  299.         // $this->plainPassword = null;
  300.     }
  301.     public function getConfirmationToken(): ?string
  302.     {
  303.         return $this->confirmationToken;
  304.     }
  305.     public function setConfirmationToken(?string $confirmationToken): self
  306.     {
  307.         $this->confirmationToken $confirmationToken;
  308.         return $this;
  309.     }
  310.     public function setAvatar(?Media $avatar): self
  311.     {
  312.         $this->avatar $avatar;
  313.         return $this;
  314.     }
  315.     public function getAvatar(): ?Media
  316.     {
  317.         return $this->avatar;
  318.     }
  319.     public function setDesk(?string $desk): void
  320.     {
  321.         $this->desk $desk;
  322.     }
  323.     public function getDesk(): ?string
  324.     {
  325.         return $this->desk;
  326.     }
  327.     
  328.     public function getContracts(): Collection
  329.     {
  330.         return $this->contracts;
  331.     }
  332.     public function addContract(Contract $contract): self
  333.     {
  334.         if (!$this->contracts->contains($contract)) {
  335.             $this->contracts[] = $contract;
  336.             $contract->setBroker($this);
  337.         }
  338.         return $this;
  339.     }
  340.     public function removeContract(Contract $contract): self
  341.     {
  342.         if ($this->contracts->removeElement($contract)) {
  343.             if ($contract->getBroker() === $this) {
  344.                 $contract->setBroker(null);
  345.             }
  346.         }
  347.         return $this;
  348.     }
  349.     public function getOperations(): Collection
  350.     {
  351.         return $this->operations;
  352.     }
  353.     public function addOperation(Contract $operation): self
  354.     {
  355.         if (!$this->operations->contains($operation)) {
  356.             $this->operations[] = $operation;
  357.             $operation->setOperator($this);
  358.         }
  359.         return $this;
  360.     }
  361.     public function removeOperation(Contract $operation): self
  362.     {
  363.         if ($this->operations->removeElement($operation)) {
  364.             if ($operation->getOperator() === $this) {
  365.                 $operation->setOperator(null);
  366.             }
  367.         }
  368.         return $this;
  369.     }
  370.     // public function getMarketRumors(): Collection
  371.     // {
  372.     //     return $this->marketRumors;
  373.     // }
  374.     // public function addMarketRumor(MarketRumor $marketRumor): self
  375.     // {
  376.     //     if (!$this->marketRumors->contains($marketRumor)) {
  377.     //         $this->marketRumors[] = $marketRumor;
  378.     //         $marketRumor->setBroker($this);
  379.     //     }
  380.     //     return $this;
  381.     // }
  382.     // public function removeMarketRumor(MarketRumor $marketRumor): self
  383.     // {
  384.     //     if ($this->marketRumors->removeElement($marketRumor)) {
  385.     //         if ($marketRumor->getBroker() === $this) {
  386.     //             $marketRumor->setBroker(null);
  387.     //         }
  388.     //     }
  389.     //     return $this;
  390.     // }
  391. }