src/Entity/User.php line 80

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
  4. use ApiPlatform\Metadata\ApiFilter;
  5. use ApiPlatform\Metadata\ApiResource;
  6. use ApiPlatform\Metadata\Delete;
  7. use ApiPlatform\Metadata\Get;
  8. use ApiPlatform\Metadata\GetCollection;
  9. use ApiPlatform\Metadata\Post;
  10. use ApiPlatform\Metadata\Put;
  11. use App\ApiPlatform\Extension\UsersExtension;
  12. use App\ApiPlatform\State\MeStateProvider;
  13. use App\ApiPlatform\State\UserProcessor;
  14. use App\Controller\Action\GetMeAction;
  15. use App\Controller\Action\UserActionPlansController;
  16. use App\Controller\Action\UsersPAController;
  17. use DateTime;
  18. use DateTimeImmutable;
  19. use App\Traits\AutoTimestampTrait;
  20. use Doctrine\Common\Collections\ArrayCollection;
  21. use Doctrine\Common\Collections\Collection;
  22. use Doctrine\DBAL\Types\Types;
  23. use Doctrine\ORM\Mapping as ORM;
  24. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  25. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  26. use Symfony\Component\Security\Core\User\UserInterface;
  27. use Symfony\Component\Serializer\Annotation\Groups;
  28. use Symfony\Component\Validator\Constraints as Assert;
  29. #[ORM\Table(name'user')]
  30. #[ORM\Entity]
  31. #[ApiResource(
  32.     normalizationContext: ['datetime_format' => 'Y-m-d''groups' => ['user.get''message.get']],
  33.     denormalizationContext: ['groups' => ['user.write''user.write']],
  34.     operations: [
  35.         new GetCollection(
  36.             order: ['firstname' => 'ASC'],
  37.         ),
  38.         new Get(),
  39.         new Get(
  40.             providerMeStateProvider::class,
  41.             name'_api_/me',
  42.             uriTemplate'/me',
  43.             controllerGetMeAction::class
  44.         ),
  45.         new Get(
  46.             providerMeStateProvider::class,
  47.             name'_api_/users_PA/{site_id}',
  48.             uriTemplate'/users_PA/{site_id}',
  49.             controllerUsersPAController::class,
  50.             writefalse,
  51.             readfalse
  52.         ), new Put(
  53.             processorUserProcessor::class,
  54.             security"is_granted('EDIT', object)",
  55.             securityMessage"You can only edit your own account unless you're an administrator.",
  56.             denormalizationContext: ['groups' => ['user.write']]
  57.         ),
  58.         new Delete(
  59.             security'user.isMasterAdmin()',
  60.         ),
  61.         new Post(
  62.             security'user.isMasterAdmin()',
  63.         )
  64.     ]
  65. )]
  66. #[ApiFilter(
  67.     SearchFilter::class,
  68.     properties: [
  69.         'customer' => 'exact',
  70.         'email' => 'partial',
  71.         'firstname' => 'partial',
  72.         'lastname' => 'partial',
  73.         'department' => 'partial',
  74.     ]
  75. )]
  76. #[UniqueEntity('email')]
  77. class User implements UserInterfacePasswordAuthenticatedUserInterface
  78. {
  79.     use AutoTimestampTrait;
  80.     const ROLE_ADMIN 'ROLE_ADMIN';
  81.     const ROLE_USER 'ROLE_USER';
  82.     #[ORM\Column(name'id'type'integer'nullablefalse)]
  83.     #[ORM\Id]
  84.     #[ORM\GeneratedValue(strategy'IDENTITY')]
  85.     #[Groups(['user.get''plan.get''task.get''comment.get'])]
  86.     private int $id;
  87.     #[ORM\Column(name'firstname'type'string'length30nullablefalse)]
  88.     #[Groups(['user.get''user.write''profile.write''plan.get''task.get''comment.get'])]
  89.     private ?string $firstname null;
  90.     #[ORM\Column(name'lastname'type'string'length30nullablefalse)]
  91.     #[Groups(['user.get''user.write''profile.write''plan.get''task.get''comment.get'])]
  92.     private ?string $lastname null;
  93.     #[ORM\Column(name'department'type'string'length30nullabletrue)]
  94.     #[Groups(['user.get''user.write''profile.write'])]
  95.     private ?string $department null;
  96.     #[ORM\Column(name'position'type'string'length30nullabletrue)]
  97.     #[Groups(['user.get''user.write''profile.write'])]
  98.     private ?string $position null;
  99.     #[ORM\Column(name'email'type'string'length255nullablefalse)]
  100.     #[Groups(['user.get''profile.write''user.write'])]
  101.     #[Assert\Email()]
  102.     private ?string $email null;
  103.     #[ORM\Column(name'password'type'text'length65535nullabletrue)]
  104.     private ?string $password null;
  105.     #[ORM\Column(name'phone'type'string'length30nullabletrue)]
  106.     #[Groups(['user.get''profile.write''user.write'])]
  107.     private ?string $phone null;
  108.     #[ORM\Column(name'mobile'type'string'length30nullabletrue)]
  109.     #[Groups(['user.get''profile.write''user.write'])]
  110.     private ?string $mobile null;
  111.     #[ORM\Column(name'website'type'string'length255nullabletrue)]
  112.     #[Groups(['user.get''profile.write''user.write'])]
  113.     private ?string $website null;
  114.     #[ORM\Column(type'json')]
  115.     #[Groups(['user.get''user.write'])]
  116.     private $roles = [];
  117.     #[ORM\ManyToOne(inversedBy'users')]
  118.     #[ORM\JoinColumn(nullabletrue)]
  119.     #[Groups(['user.get''user.write'])]
  120.     private ?Customer $customer null;
  121.     #[ORM\OneToMany(mappedBy'user'targetEntityUserSite::class, orphanRemovaltruecascade: ['persist'])]
  122.     #[Groups(['user.get''user.write'])]
  123.     private Collection $userSites;
  124.     #[Groups(['user.write''profile.write'])]
  125.     private ?string $plainPassword null;
  126.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  127.     private ?string $activationToken null;
  128.     #[ORM\Column(nullabletrue)]
  129.     private ?\DateTimeImmutable $activationTokenCreatedAt null;
  130.     #[ORM\Column]
  131.     #[Groups(['user.write''profile.write''user.get'])]
  132.     private bool $isEnabled true;
  133.     #[ORM\Column(nullabletrue)]
  134.     #[Groups(['user.write''user.get', ])]
  135.     private ?\DateTimeImmutable $expireAt null;
  136.     #[Groups(['document.get'])]
  137.     #[ORM\Column(type'datetime')]
  138.     protected $createdAt;
  139.     #[Groups(['document.get'])]
  140.     #[ORM\Column(type'datetime')]
  141.     protected $updatedAt;
  142.     #[ORM\Column(name'application_master'type'string'nullabletrue)]
  143.     #[Groups(['user.get''user.write'])]
  144.     private ?string $applicationMaster null;
  145.     #[ORM\Column(name'lang'type'string'nullabletrue)]
  146.     #[Groups(['user.get''user.write'])]
  147.     private ?string $lang 'fr';
  148.     #[ORM\OneToMany(mappedBy'userId'targetEntityExport::class)]
  149.     private Collection $exports;
  150.     #[ORM\Column(name'customer_associated 'type'json'nullabletrue)]
  151.     #[Groups(['user.get''user.write'])]
  152.     private ?array $customerAssociated = [];
  153.     #[ORM\OneToMany(mappedBy'site'targetEntityAffectation::class, cascade: ['persist'], orphanRemovaltrue)]
  154.     #[Groups(['user.write'])]
  155.     private Collection $affectations;
  156.     #[ORM\Column(nullabletrue)]
  157.     #[Groups(['user.get''user.write'])]
  158.     private ?bool $hideInPA null;
  159.     #[ORM\OneToMany(mappedBy'user'targetEntityDiscussion::class)]
  160.     private Collection $discussions;
  161.     #[ORM\Column]
  162.     #[Groups(['user.get''user.write''message.get'])]
  163.     private ?bool $isHotline false;
  164.     #[ORM\OneToMany(mappedBy'user'targetEntitySiteQuestions::class)]
  165.     private Collection $siteQuestions;
  166.     #[ORM\ManyToOne(inversedBy'profile_picture')]
  167.     #[Groups(['user.get''user.write'])]
  168.     private ?Media $profile_picture null;
  169.     #[ORM\Column(nullabletrue)]
  170.     private ?DateTime $lockedAt null;
  171.     #[ORM\Column(nullabletrue)]
  172.     private int $attempts 0;
  173.     public function __construct()
  174.     {
  175.         $this->sites = new ArrayCollection();
  176.         $this->userSites = new ArrayCollection();
  177.         $this->exports = new ArrayCollection();
  178.         $this->affectations = new ArrayCollection();
  179.         $this->discussions = new ArrayCollection();
  180.         $this->siteQuestions = new ArrayCollection();
  181.     }
  182.     public function getId(): ?int
  183.     {
  184.         return $this->id;
  185.     }
  186.     public function getFirstname(): ?string
  187.     {
  188.         return $this->firstname;
  189.     }
  190.     public function setFirstname(string $firstname): self
  191.     {
  192.         $this->firstname $firstname;
  193.         return $this;
  194.     }
  195.     public function getLastname(): ?string
  196.     {
  197.         return $this->lastname;
  198.     }
  199.     public function setLastname(string $lastname): self
  200.     {
  201.         $this->lastname $lastname;
  202.         return $this;
  203.     }
  204.     public function getDepartment(): ?string
  205.     {
  206.         return $this->department;
  207.     }
  208.     public function setDepartment(string $department): self
  209.     {
  210.         $this->department $department;
  211.         return $this;
  212.     }
  213.     public function getPosition(): ?string
  214.     {
  215.         return $this->position;
  216.     }
  217.     public function setPosition(string $position): self
  218.     {
  219.         $this->position $position;
  220.         return $this;
  221.     }
  222.     public function getEmail(): ?string
  223.     {
  224.         return $this->email;
  225.     }
  226.     public function setEmail(string $email): self
  227.     {
  228.         $this->email $email;
  229.         return $this;
  230.     }
  231.     public function getPhone(): ?string
  232.     {
  233.         return $this->phone;
  234.     }
  235.     public function setPhone(string $phone): self
  236.     {
  237.         $this->phone $phone;
  238.         return $this;
  239.     }
  240.     public function getMobile(): ?string
  241.     {
  242.         return $this->mobile;
  243.     }
  244.     public function setMobile(string $mobile): self
  245.     {
  246.         $this->mobile $mobile;
  247.         return $this;
  248.     }
  249.     public function getWebsite(): ?string
  250.     {
  251.         return $this->website;
  252.     }
  253.     public function setWebsite(string $website): self
  254.     {
  255.         $this->website $website;
  256.         return $this;
  257.     }
  258.     /**
  259.      * A visual identifier that represents this user.
  260.      *
  261.      * @see UserInterface
  262.      */
  263.     public function getUserIdentifier(): string
  264.     {
  265.         return (string) $this->email;
  266.     }
  267.     public function hasRole(string $role): bool
  268.     {
  269.         return in_array($role$this->roles);
  270.     }
  271.     /**
  272.      * @see UserInterface
  273.      */
  274.     public function getRoles(): array
  275.     {
  276.         $roles $this->roles;
  277.         if (empty($this->roles))
  278.             $roles[] = 'ROLE_USER';
  279.         return array_unique($roles);
  280.     }
  281.     public function setRoles(array $roles): self
  282.     {
  283.         $this->roles $roles;
  284.         return $this;
  285.     }
  286.     public function addRole(string $role)
  287.     {
  288.         if (in_array($role$this->roles)) {
  289.             return;
  290.         }
  291.         $this->roles[] = $role;
  292.         return $this;
  293.     }
  294.     /**
  295.      * @see PasswordAuthenticatedUserInterface
  296.      */
  297.     public function getPassword(): string
  298.     {
  299.         return $this->password;
  300.     }
  301.     public function setPassword(string $password): self
  302.     {
  303.         $this->password $password;
  304.         return $this;
  305.     }
  306.     /**
  307.      * @see UserInterface
  308.      */
  309.     public function eraseCredentials()
  310.     {
  311.         $this->plainPassword null;
  312.     }
  313.     public function getPlainPassword(): ?string
  314.     {
  315.         return $this->plainPassword;
  316.     }
  317.     public function setPlainPassword(string $plainPassword null): self
  318.     {
  319.         $this->plainPassword $plainPassword;
  320.         return $this;
  321.     }
  322.     public function __toString()
  323.     {
  324.         return (string)($this->firstname ' ' $this->lastname);
  325.     }
  326.     public function getCustomer(): ?Customer
  327.     {
  328.         return $this->customer;
  329.     }
  330.     public function setCustomer(?Customer $customer): self
  331.     {
  332.         $this->customer $customer;
  333.         return $this;
  334.     }
  335.     /**
  336.      * @return Collection<int, UserSite>
  337.      */
  338.     public function getUserSites(): Collection
  339.     {
  340.         return $this->userSites;
  341.     }
  342.     public function addUserSite(UserSite $userSite): self
  343.     {
  344.         if (!$this->userSites->contains($userSite)) {
  345.             $this->userSites->add($userSite);
  346.             $userSite->setUser($this);
  347.         }
  348.         return $this;
  349.     }
  350.     public function removeUserSite(UserSite $userSite): self
  351.     {
  352.         if ($this->userSites->removeElement($userSite)) {
  353.             // set the owning side to null (unless already changed)
  354.             if ($userSite->getUser() === $this) {
  355.                 $userSite->setUser(null);
  356.             }
  357.         }
  358.         return $this;
  359.     }
  360.     public function getActivationToken(): ?string
  361.     {
  362.         return $this->activationToken;
  363.     }
  364.     public function setActivationToken(?string $activationToken): self
  365.     {
  366.         $this->activationToken $activationToken;
  367.         return $this;
  368.     }
  369.     public function getActivationTokenCreatedAt(): ?\DateTimeImmutable
  370.     {
  371.         return $this->activationTokenCreatedAt;
  372.     }
  373.     public function setActivationTokenCreatedAt(?\DateTimeImmutable $activationTokenCreatedAt): self
  374.     {
  375.         $this->activationTokenCreatedAt $activationTokenCreatedAt;
  376.         return $this;
  377.     }
  378.     public function isIsEnabled(): ?bool
  379.     {
  380.         return $this->isEnabled;
  381.     }
  382.     public function setIsEnabled(bool $isEnabled): self
  383.     {
  384.         $this->isEnabled $isEnabled;
  385.         return $this;
  386.     }
  387.     public function isAdmin()
  388.     {
  389.         return in_array(self::ROLE_ADMIN$this->roles);
  390.     }
  391.     public function isMasterAdmin()
  392.     {
  393.         return $this->getApplicationMaster() !== null;
  394.     }
  395.     public function getExpireAt(): ?\DateTimeImmutable
  396.     {
  397.         return $this->expireAt;
  398.     }
  399.     public function setExpireAt(?\DateTimeImmutable $expireAt): self
  400.     {
  401.         $this->expireAt $expireAt;
  402.         return $this;
  403.     }
  404.     /**
  405.      * @return string|null
  406.      */
  407.     public function getApplicationMaster(): ?string
  408.     {
  409.         return $this->applicationMaster;
  410.     }
  411.     /**
  412.      * @param string|null $applicationMaster
  413.      */
  414.     public function setApplicationMaster(?string $applicationMaster): void
  415.     {
  416.         $this->applicationMaster $applicationMaster;
  417.     }
  418.     /**
  419.      * @return string|null
  420.      */
  421.     public function getLang(): ?string
  422.     {
  423.         return $this->lang;
  424.     }
  425.     /**
  426.      * @param string|null $lang
  427.      */
  428.     public function setLang(?string $lang): void
  429.     {
  430.         $this->lang $lang;
  431.     }
  432.     /**
  433.      * @return Collection<int, Export>
  434.      */
  435.     public function getExports(): Collection
  436.     {
  437.         return $this->exports;
  438.     }
  439.     public function addExport(Export $export): self
  440.     {
  441.         if (!$this->exports->contains($export)) {
  442.             $this->exports->add($export);
  443.             $export->setUserId($this);
  444.         }
  445.         return $this;
  446.     }
  447.     public function removeExport(Export $export): self
  448.     {
  449.         if ($this->exports->removeElement($export)) {
  450.             // set the owning side to null (unless already changed)
  451.             if ($export->getUserId() === $this) {
  452.                 $export->setUserId(null);
  453.             }
  454.         }
  455.         return $this;
  456.     }
  457.     /**
  458.      * @return array|null
  459.      */
  460.     public function getCustomerAssociated(): ?array
  461.     {
  462.         return $this->customerAssociated;
  463.     }
  464.     /**
  465.      * @param array|null $customerAssociated
  466.      */
  467.     public function setCustomerAssociated(?array $customerAssociated): void
  468.     {
  469.         $this->customerAssociated $customerAssociated;
  470.     }
  471.     /**
  472.      * @return Collection<int, Affectation>
  473.      */
  474.     public function getAffectations(): Collection
  475.     {
  476.         return $this->affectations;
  477.     }
  478.     public function addAffectation(Affectation $affectation): self
  479.     {
  480.         if (!$this->affectations->contains($affectation)) {
  481.             $this->affectations->add($affectation);
  482.             $affectation->setSite($this);
  483.         }
  484.         return $this;
  485.     }
  486.     public function removeAffectation(Affectation $affectation): self
  487.     {
  488.         if ($this->affectations->removeElement($affectation)) {
  489.             // set the owning side to null (unless already changed)
  490.             if ($affectation->getSite() === $this) {
  491.                 $affectation->setSite(null);
  492.             }
  493.         }
  494.         return $this;
  495.     }
  496.     public function isHideInPA(): ?bool
  497.     {
  498.         return $this->hideInPA;
  499.     }
  500.     public function setHideInPA(?bool $hideInPA): self
  501.     {
  502.         $this->hideInPA $hideInPA;
  503.         return $this;
  504.     }
  505.     /**
  506.      * @return Collection<int, Discussion>
  507.      */
  508.     public function getDiscussions(): Collection
  509.     {
  510.         return $this->discussions;
  511.     }
  512.     public function addDiscussion(Discussion $discussion): self
  513.     {
  514.         if (!$this->discussions->contains($discussion)) {
  515.             $this->discussions->add($discussion);
  516.             $discussion->setUser($this);
  517.         }
  518.         return $this;
  519.     }
  520.     public function removeDiscussion(Discussion $discussion): self
  521.     {
  522.         if ($this->discussions->removeElement($discussion)) {
  523.             // set the owning side to null (unless already changed)
  524.             if ($discussion->getUser() === $this) {
  525.                 $discussion->setUser(null);
  526.             }
  527.         }
  528.         return $this;
  529.     }
  530.     public function isIsHotline(): ?bool
  531.     {
  532.         return $this->isHotline;
  533.     }
  534.     public function setIsHotline(bool $isHotline): self
  535.     {
  536.         $this->isHotline $isHotline;
  537.         return $this;
  538.     }
  539.     /**
  540.      * @return Collection<int, SiteQuestions>
  541.      */
  542.     public function getSiteQuestions(): Collection
  543.     {
  544.         return $this->siteQuestions;
  545.     }
  546.     public function addSiteQuestion(SiteQuestions $siteQuestion): self
  547.     {
  548.         if (!$this->siteQuestions->contains($siteQuestion)) {
  549.             $this->siteQuestions->add($siteQuestion);
  550.             $siteQuestion->setUser($this);
  551.         }
  552.         return $this;
  553.     }
  554.     public function removeSiteQuestion(SiteQuestions $siteQuestion): self
  555.     {
  556.         if ($this->siteQuestions->removeElement($siteQuestion)) {
  557.             // set the owning side to null (unless already changed)
  558.             if ($siteQuestion->getUser() === $this) {
  559.                 $siteQuestion->setUser(null);
  560.             }
  561.         }
  562.         return $this;
  563.     }
  564.     public function getProfilePicture(): ?Media
  565.     {
  566.         return $this->profile_picture;
  567.     }
  568.     public function setProfilePicture(?Media $profile_picture): self
  569.     {
  570.         $this->profile_picture $profile_picture;
  571.         return $this;
  572.     }
  573.     public function getLockedAt(): ?DateTime
  574.     {
  575.         return $this->lockedAt;
  576.     }
  577.     public function setLockedAt(?DateTime $lockedAt): self
  578.     {
  579.         $this->lockedAt $lockedAt;
  580.         return $this;
  581.     }
  582.     public function getAttempts(): int
  583.     {
  584.         return $this->attempts;
  585.     }
  586.     public function setAttempts(int $attempts): self
  587.     {
  588.         $this->attempts $attempts;
  589.         return $this;
  590.     }
  591. }