src/Entity/User.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use ApiPlatform\Core\Annotation\ApiResource;
  11. use ApiPlatform\Core\Annotation\ApiProperty;
  12. use ApiPlatform\Core\Annotation\ApiFilter;
  13. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\RangeFilter;
  14. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  15. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. /**
  18.  * @ApiResource(
  19.  *     iri="http://schema.org/User",
  20.  *     attributes={"security"="is_granted('ROLE_USER')"},
  21.  *     collectionOperations={
  22.  *         "get",
  23.  *         "post"={"security"="is_granted('ROLE_USER')"}
  24.  *     },
  25.  *     itemOperations={
  26.  *         "get",
  27.  *         "put"={"security"="is_granted('ROLE_USER')"  },
  28.  *         "delete"={"security"="is_granted('ROLE_ADMIN')" },
  29.  *     })
  30.  * @ORM\Entity(repositoryClass=UserRepository::class)
  31.  * @ApiFilter(SearchFilter::class,
  32.  *     properties={
  33.  *     "username": "exact",
  34.  * })
  35.  * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
  36.  */
  37. class User implements  UserInterface, \Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface
  38. {
  39.     const ROLE_DEFAULT "ROLE_USER";
  40.     const ROLE_SUPER_ADMIN "ROLE_SUPER_ADMIN";
  41.     /**
  42.      * @ORM\Id
  43.      * @ORM\Column(type="integer")
  44.      * @ORM\GeneratedValue(strategy="AUTO")
  45.      */
  46.     public $id;
  47.     /**
  48.      * @var MediaObject|null
  49.      *
  50.      * @ORM\ManyToOne(targetEntity=MediaObject::class)
  51.      * @ORM\JoinColumn(nullable=true)
  52.      * @Groups({"media_object_read"})
  53.      * @ApiProperty(iri="http://schema.org/image")
  54.      */
  55.     public $userPicture;
  56.     /**
  57.      * @ORM\Column(type="string", length=255, nullable=true)
  58.      */
  59.     public $lastName;
  60.     /**
  61.      * @ORM\Column(type="string", length=255, nullable=true)
  62.      */
  63.     public $firstName;
  64.     /**
  65.      * @ORM\Column(type="string", length=255, nullable=true)
  66.      */
  67.     public $phoneNumber;
  68.     /**
  69.      * @ORM\OneToMany(targetEntity=Contact::class, mappedBy="threatBy")
  70.      */
  71.     private $contacts;
  72.     /**
  73.      * @ORM\OneToMany(targetEntity=Logs::class, mappedBy="user")
  74.      */
  75.     private $logs;
  76.     /**
  77.      * @ORM\OneToMany(targetEntity=Subscription::class, mappedBy="createdBy", orphanRemoval=true)
  78.      */
  79.     private $subscriptions;
  80.     /**
  81.      * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="users")
  82.      */
  83.     private $company;
  84.     /**
  85.      * @ORM\ManyToOne(targetEntity=Address::class)
  86.      */
  87.     private $address;
  88.     /**
  89.      * @ORM\Column(type="string", length=500, unique=true)
  90.      */
  91.     private $email;
  92.     /**
  93.      * @ORM\Column(type="string", length=500)
  94.      */
  95.     private $password;
  96.     /**
  97.      * @ORM\Column(type="array")
  98.      */
  99.     private $roles = [];
  100.     /**
  101.      * @ORM\Column(type="boolean", nullable=true)
  102.      */
  103.     private $active;
  104.     /**
  105.      * @ORM\Column(type="string", length=500, nullable=true)
  106.      */
  107.     private $salt;
  108.     /**
  109.      * @ORM\Column(type="string", length=500, nullable=true)
  110.      */
  111.     private $emailCanonical;
  112.     /**
  113.      * @ORM\Column(type="string", length=255, nullable=true)
  114.      */
  115.     private $plainPassword;
  116.     /**
  117.      * @ORM\Column(type="string", length=500, nullable=true)
  118.      */
  119.     private $confirmationToken;
  120.     /**
  121.      * @ORM\Column(type="datetime_immutable", nullable=true)
  122.      */
  123.     private $passwordRequestedAt;
  124.     /**
  125.      * @ORM\Column(type="string", length=255, unique=true)
  126.      */
  127.     private $username;
  128.     /**
  129.      * @ORM\Column(type="datetime_immutable", nullable=true)
  130.      */
  131.     private $lastLogin;
  132.     /**
  133.      * @ORM\Column(type="boolean", nullable=true)
  134.      */
  135.     private $enabled;
  136.     /**
  137.      * @ORM\Column(type="string", length=255, nullable=true)
  138.      */
  139.     private $usernameCanonical;
  140.     /**
  141.      * @ORM\Column(type="boolean")
  142.      */
  143.     private $isVerified false;
  144.     /**
  145.      * @ORM\Column(type="string", length=255)
  146.      */
  147.     private $local;
  148.     /**
  149.      * @ORM\Column(type="integer", nullable=true)
  150.      */
  151.     private $loginInCount;
  152.     /**
  153.      * @ORM\OneToMany(targetEntity=BookingRoom::class, mappedBy="user")
  154.      */
  155.     private $bookingRooms;
  156.     /**
  157.      * @ORM\OneToMany(targetEntity=Payment::class, mappedBy="user")
  158.      */
  159.     private $payments;
  160.     /**
  161.      * @ORM\Column(type="string", length=1000, nullable=true)
  162.      */
  163.     private $resendMail;
  164.     /**
  165.      * @ORM\Column(type="datetime_immutable", nullable=true)
  166.      */
  167.     private $createdAt;
  168.     /**
  169.      * @ORM\Column(type="datetime_immutable", nullable=true)
  170.      */
  171.     private $updateAt;
  172.     /**
  173.      * @ORM\OneToMany(targetEntity=AnnounceChat::class, mappedBy="sendBy", orphanRemoval=true)
  174.      */
  175.     private $announceChats;
  176.     /**
  177.      * @ORM\OneToMany(targetEntity=AnnounceChannel::class, mappedBy="createdBy")
  178.      */
  179.     private $announceChannels;
  180.     public function __construct()
  181.     {
  182.         $this->createdAt= new \DateTimeImmutable();
  183.         $this->customers = new ArrayCollection();
  184.         $this->contacts = new ArrayCollection();
  185.         $this->logs = new ArrayCollection();
  186.         $this->subscriptions = new ArrayCollection();
  187.         $this->enabled false;
  188.         $this->roles = [];
  189.         $this->bookingRooms = new ArrayCollection();
  190.         $this->payments = new ArrayCollection();
  191.         $this->local="fr";
  192.         $this->announceChats = new ArrayCollection();
  193.         $this->announceChannels = new ArrayCollection();
  194.     }
  195.     public function getLastName(): ?string
  196.     {
  197.         return $this->lastName;
  198.     }
  199.     public function  setId(?int $id){
  200.         $this->id=$id;
  201.     }
  202.     public function setLastName(?string $lastName): self
  203.     {
  204.         $this->lastName $lastName;
  205.         return $this;
  206.     }
  207.     public function getFirstName(): ?string
  208.     {
  209.         return $this->firstName;
  210.     }
  211.     public function setFirstName(?string $firstName): self
  212.     {
  213.         $this->firstName $firstName;
  214.         return $this;
  215.     }
  216.     public function getPhoneNumber(): ?string
  217.     {
  218.         return $this->phoneNumber;
  219.     }
  220.     public function setPhoneNumber(?string $phoneNumber): self
  221.     {
  222.         $this->phoneNumber $phoneNumber;
  223.         return $this;
  224.     }
  225.     /**
  226.      * @return Collection|Contact[]
  227.      */
  228.     public function getContacts(): Collection
  229.     {
  230.         return $this->contacts;
  231.     }
  232.     /**
  233.      * @return Collection|Logs[]
  234.      */
  235.     public function getLogs(): Collection
  236.     {
  237.         return $this->logs;
  238.     }
  239.     public function addContact(Contact $contact): self
  240.     {
  241.         if (!$this->contacts->contains($contact)) {
  242.             $this->contacts[] = $contact;
  243.             $contact->setThreatBy($this);
  244.         }
  245.         return $this;
  246.     }
  247.     public function addLogs(Logs $logs): self
  248.     {
  249.         if (!$this->logs->contains($logs)) {
  250.             $this->logs[] = $logs;
  251.             $logs->setUser($this);
  252.         }
  253.         return $this;
  254.     }
  255.     public function removeContact(Contact $contact): self
  256.     {
  257.         if ($this->contacts->contains($contact)) {
  258.             $this->contacts->removeElement($contact);
  259.             // set the owning side to null (unless already changed)
  260.             if ($contact->getThreatBy() === $this) {
  261.                 $contact->setThreatBy(null);
  262.             }
  263.         }
  264.         return $this;
  265.     }
  266.     public function removeLogs(Logs $logs): self
  267.     {
  268.         if ($this->logs->contains($logs)) {
  269.             $this->logs->removeElement($logs);
  270.             // set the owning side to null (unless already changed)
  271.             if ($logs->getUser() === $this) {
  272.                 $logs->setUser(null);
  273.             }
  274.         }
  275.         return $this;
  276.     }
  277.     /**
  278.      * @return Collection|Subscription[]
  279.      */
  280.     public function getSubscriptions(): Collection
  281.     {
  282.         return $this->subscriptions;
  283.     }
  284.     public function addSubscription(Subscription $subscription): self
  285.     {
  286.         if (!$this->subscriptions->contains($subscription)) {
  287.             $this->subscriptions[] = $subscription;
  288.             $subscription->setCreatedBy($this);
  289.         }
  290.         return $this;
  291.     }
  292.     public function removeSubscription(Subscription $subscription): self
  293.     {
  294.         if ($this->subscriptions->contains($subscription)) {
  295.             $this->subscriptions->removeElement($subscription);
  296.             // set the owning side to null (unless already changed)
  297.             if ($subscription->getCreatedBy() === $this) {
  298.                 $subscription->setCreatedBy(null);
  299.             }
  300.         }
  301.         return $this;
  302.     }
  303.     public function getCompany(): ?Company
  304.     {
  305.         return $this->company;
  306.     }
  307.     public function setCompany(?Company $company): self
  308.     {
  309.         $this->company $company;
  310.         return $this;
  311.     }
  312.     public function getAddress(): ?Address
  313.     {
  314.         return $this->address;
  315.     }
  316.     public function setAddress(?Address $address): self
  317.     {
  318.         $this->address $address;
  319.         return $this;
  320.     }
  321.     public function getEmail(): ?string
  322.     {
  323.         return $this->email;
  324.     }
  325.     public function setEmail(string $email): self
  326.     {
  327.         $this->email $email;
  328.         return $this;
  329.     }
  330.     public function getPassword(): ?string
  331.     {
  332.         return $this->password;
  333.     }
  334.     public function setPassword(string $password): self
  335.     {
  336.         $this->password $password;
  337.         return $this;
  338.     }
  339.     public function getRoles(): array
  340.     {
  341.         $roles $this->roles;
  342.         // we need to make sure to have at least one role
  343.         $roles[] = static::ROLE_DEFAULT;
  344.         return array_values(array_unique($roles));
  345.     }
  346.     public function setRoles(array $roles): self
  347.     {
  348.         $this->roles $roles;
  349.         return $this;
  350.     }
  351.     public function isActive(): ?bool
  352.     {
  353.         return $this->active;
  354.     }
  355.     public function setActive(bool $active): self
  356.     {
  357.         $this->active $active;
  358.         return $this;
  359.     }
  360.     public function getSalt(): ?string
  361.     {
  362.         return $this->salt;
  363.     }
  364.     public function setSalt(?string $salt): self
  365.     {
  366.         $this->salt $salt;
  367.         return $this;
  368.     }
  369.     public function getEmailCanonical(): ?string
  370.     {
  371.         return $this->emailCanonical;
  372.     }
  373.     public function setEmailCanonical(string $emailCanonical): self
  374.     {
  375.         $this->emailCanonical $emailCanonical;
  376.         return $this;
  377.     }
  378.     public function getPlainPassword(): ?string
  379.     {
  380.         return $this->plainPassword;
  381.     }
  382.     public function setPlainPassword(string $plainPassword): self
  383.     {
  384.         $this->plainPassword $plainPassword;
  385.         return $this;
  386.     }
  387.     public function getConfirmationToken(): ?string
  388.     {
  389.         return $this->confirmationToken;
  390.     }
  391.     public function setConfirmationToken(string $confirmationToken): self
  392.     {
  393.         $this->confirmationToken $confirmationToken;
  394.         return $this;
  395.     }
  396.     public function getPasswordRequestedAt(): ?\DateTimeImmutable
  397.     {
  398.         return $this->passwordRequestedAt;
  399.     }
  400.     public function setPasswordRequestedAt(\DateTimeImmutable $passwordRequestedAt): self
  401.     {
  402.         $this->passwordRequestedAt $passwordRequestedAt;
  403.         return $this;
  404.     }
  405.     public function getUsername(): ?string
  406.     {
  407.         return $this->username;
  408.     }
  409.     public function setUsername(string $username): self
  410.     {
  411.         $this->username $username;
  412.         return $this;
  413.     }
  414.     public function eraseCredentials()
  415.     {
  416.         // TODO: Implement eraseCredentials() method.
  417.         $this->plainPassword null;
  418.     }
  419.     public function getUserIdentifier(): string
  420.     {
  421.         return $this->username;
  422.     }
  423.     public function addRole($role)
  424.     {
  425.         $role strtoupper($role);
  426.         if ($role === static::ROLE_DEFAULT) {
  427.             return $this;
  428.         }
  429.         if (!in_array($role$this->rolestrue)) {
  430.             $this->roles[] = $role;
  431.         }
  432.         return $this;
  433.     }
  434.     public function isEnabled()
  435.     {
  436.         return $this->enabled;
  437.     }
  438.     /**
  439.      * {@inheritdoc}
  440.      */
  441.     public function isSuperAdmin()
  442.     {
  443.         return $this->hasRole(static::ROLE_SUPER_ADMIN);
  444.     }
  445.     public function hasRole($role)
  446.     {
  447.         return in_array(strtoupper($role), $this->getRoles(), true);
  448.     }
  449.     public function removeRole($role)
  450.     {
  451.         if (false !== $key array_search(strtoupper($role), $this->rolestrue)) {
  452.             unset($this->roles[$key]);
  453.             $this->roles array_values($this->roles);
  454.         }
  455.         return $this;
  456.     }
  457.     public function setSuperAdmin($boolean)
  458.     {
  459.         if (true === $boolean) {
  460.             $this->addRole(static::ROLE_SUPER_ADMIN);
  461.         } else {
  462.             $this->removeRole(static::ROLE_SUPER_ADMIN);
  463.         }
  464.         return $this;
  465.     }
  466.     public function setLastLogin(\DateTimeImmutable $time null)
  467.     {
  468.         $this->lastLogin $time;
  469.         return $this;
  470.     }
  471.     public function getLastLogin(): ?\DateTimeImmutable
  472.     {
  473.         return $this->lastLogin;
  474.     }
  475.     public function isPasswordRequestNonExpired($ttl)
  476.     {
  477.         return $this->getPasswordRequestedAt() instanceof \DateTime &&
  478.             $this->getPasswordRequestedAt()->getTimestamp() + $ttl time();
  479.     }
  480.     public function setEnabled(bool $enabled): self
  481.     {
  482.         $this->enabled $enabled;
  483.         return $this;
  484.     }
  485.     public function getId(){
  486.         return $this->id;
  487.     }
  488.     public function getUsernameCanonical(): ?string
  489.     {
  490.         return $this->usernameCanonical;
  491.     }
  492.     public function setUsernameCanonical(string $usernameCanonical): self
  493.     {
  494.         $this->usernameCanonical $usernameCanonical;
  495.         return $this;
  496.     }
  497.     public function isVerified(): bool
  498.     {
  499.         return $this->isVerified;
  500.     }
  501.     public function setIsVerified(bool $isVerified): self
  502.     {
  503.         $this->isVerified $isVerified;
  504.         return $this;
  505.     }
  506.     public function __toString()
  507.     {
  508.         // TODO: Implement __toString() method.
  509.         return  $this->lastName." ".$this->firstName;
  510.     }
  511.     public function getLocal(): ?string
  512.     {
  513.         return $this->local;
  514.     }
  515.     public function setLocal(string $local): self
  516.     {
  517.         $this->local $local;
  518.         return $this;
  519.     }
  520.     public function getLoginInCount(): ?int
  521.     {
  522.         return $this->loginInCount;
  523.     }
  524.     public function setLoginInCount(?int $loginInCount): self
  525.     {
  526.         $this->loginInCount $loginInCount;
  527.         return $this;
  528.     }
  529.     /**
  530.      * @return Collection<int, BookingRoom>
  531.      */
  532.     public function getBookingRooms(): Collection
  533.     {
  534.         return $this->bookingRooms;
  535.     }
  536.     public function addBookingRoom(BookingRoom $bookingRoom): self
  537.     {
  538.         if (!$this->bookingRooms->contains($bookingRoom)) {
  539.             $this->bookingRooms[] = $bookingRoom;
  540.             $bookingRoom->setUser($this);
  541.         }
  542.         return $this;
  543.     }
  544.     public function removeBookingRoom(BookingRoom $bookingRoom): self
  545.     {
  546.         if ($this->bookingRooms->removeElement($bookingRoom)) {
  547.             // set the owning side to null (unless already changed)
  548.             if ($bookingRoom->getUser() === $this) {
  549.                 $bookingRoom->setUser(null);
  550.             }
  551.         }
  552.         return $this;
  553.     }
  554.     /**
  555.      * @return Collection<int, Payment>
  556.      */
  557.     public function getPayments(): Collection
  558.     {
  559.         return $this->payments;
  560.     }
  561.     public function addPayment(Payment $payment): self
  562.     {
  563.         if (!$this->payments->contains($payment)) {
  564.             $this->payments[] = $payment;
  565.             $payment->setUser($this);
  566.         }
  567.         return $this;
  568.     }
  569.     public function removePayment(Payment $payment): self
  570.     {
  571.         if ($this->payments->removeElement($payment)) {
  572.             // set the owning side to null (unless already changed)
  573.             if ($payment->getUser() === $this) {
  574.                 $payment->setUser(null);
  575.             }
  576.         }
  577.         return $this;
  578.     }
  579.     public function getResendMail(): ?string
  580.     {
  581.         return $this->resendMail;
  582.     }
  583.     public function setResendMail(?string $resendMail): self
  584.     {
  585.         $this->resendMail $resendMail;
  586.         return $this;
  587.     }
  588.     public function getCreatedAt(): ?\DateTimeImmutable
  589.     {
  590.         return $this->createdAt;
  591.     }
  592.     public function setCreatedAt(\DateTimeImmutable $createdAt): self
  593.     {
  594.         $this->createdAt $createdAt;
  595.         return $this;
  596.     }
  597.     public function getUpdateAt(): ?\DateTimeImmutable
  598.     {
  599.         return $this->updateAt;
  600.     }
  601.     public function setUpdateAt(\DateTimeImmutable $updateAt): self
  602.     {
  603.         $this->updateAt $updateAt;
  604.         return $this;
  605.     }
  606.     /**
  607.      * @return Collection<int, AnnounceChat>
  608.      */
  609.     public function getAnnounceChats(): Collection
  610.     {
  611.         return $this->announceChats;
  612.     }
  613.     public function addAnnounceChat(AnnounceChat $announceChat): self
  614.     {
  615.         if (!$this->announceChats->contains($announceChat)) {
  616.             $this->announceChats[] = $announceChat;
  617.             $announceChat->setSendBy($this);
  618.         }
  619.         return $this;
  620.     }
  621.     public function removeAnnounceChat(AnnounceChat $announceChat): self
  622.     {
  623.         if ($this->announceChats->removeElement($announceChat)) {
  624.             // set the owning side to null (unless already changed)
  625.             if ($announceChat->getSendBy() === $this) {
  626.                 $announceChat->setSendBy(null);
  627.             }
  628.         }
  629.         return $this;
  630.     }
  631.     /**
  632.      * @return Collection<int, AnnounceChannel>
  633.      */
  634.     public function getAnnounceChannels(): Collection
  635.     {
  636.         return $this->announceChannels;
  637.     }
  638.     public function addAnnounceChannel(AnnounceChannel $announceChannel): self
  639.     {
  640.         if (!$this->announceChannels->contains($announceChannel)) {
  641.             $this->announceChannels[] = $announceChannel;
  642.             $announceChannel->setCreatedBy($this);
  643.         }
  644.         return $this;
  645.     }
  646.     public function removeAnnounceChannel(AnnounceChannel $announceChannel): self
  647.     {
  648.         if ($this->announceChannels->removeElement($announceChannel)) {
  649.             // set the owning side to null (unless already changed)
  650.             if ($announceChannel->getCreatedBy() === $this) {
  651.                 $announceChannel->setCreatedBy(null);
  652.             }
  653.         }
  654.         return $this;
  655.     }
  656. }