src/Entity/User.php line 37

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use ApiPlatform\Metadata\Post;
  5. use App\Controller\Api\ApiRegisterController;
  6. use App\Repository\UserRepository;
  7. use App\Service\GeoIP;
  8. use App\Trait\DateTrait;
  9. use App\Trait\DeletableTrait;
  10. use App\Trait\Notifiable;
  11. use DateTimeImmutable;
  12. use DateTimeInterface;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\Common\Collections\Collection;
  15. use Doctrine\ORM\Mapping as ORM;
  16. use GeoIp2\Exception\AddressNotFoundException;
  17. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  18. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  19. use Symfony\Component\Security\Core\User\UserInterface;
  20. use Symfony\Component\Serializer\Annotation\Groups;
  21. use Symfony\Component\Validator\Constraints as Assert;
  22. #[ORM\Entity(repositoryClassUserRepository::class)]
  23. #[ORM\Table(name'`user`')]
  24. #[UniqueEntity(fields: ['email'], message"Cet adresse email est déjà utilisée pour un autre compte")]
  25. #[UniqueEntity(fields: ['phone'], message"Ce numéro est déjà utilisé pour un autre compte")]
  26. #[ApiResource(operations: [
  27.     new Post(
  28.         uriTemplate'/register',
  29.         controllerApiRegisterController::class,
  30.         denormalizationContext: ['groups' => ['user:register']],
  31.         writefalse
  32.     )
  33. ])]
  34. class User implements UserInterfacePasswordAuthenticatedUserInterface
  35. {
  36.     use DateTrait;
  37.     use Notifiable;
  38.     use DeletableTrait;
  39.     #[ORM\Id]
  40.     #[ORM\GeneratedValue]
  41.     #[ORM\Column]
  42.     #[Groups(['article:view'])]
  43.     private ?int $id null;
  44.     #[ORM\Column(length255nullabletrue)]
  45.     #[Groups(['user:register''article:view'])]
  46.     private ?string $lastname null;
  47.     #[ORM\Column(length255)]
  48.     #[Groups(['user:register''article:view'])]
  49.     private ?string $firstname null;
  50.     #[ORM\Column(length180uniquetrue)]
  51.     #[Groups(['user:register'])]
  52.     private ?string $email null;
  53.     #[ORM\Column]
  54.     private array $roles = [];
  55.     /**
  56.      * @var string The hashed password
  57.      */
  58.     #[ORM\Column]
  59.     #[Groups(['user:register'])]
  60.     private ?string $password null;
  61.     #[ORM\Column(length15uniquetrue)]
  62.     #[Groups(['user:register'])]
  63.     private ?string $phone null;
  64.     #[ORM\Column(options: ["default" => false])]
  65.     private ?bool $isPhoneVerified false;
  66.     #[ORM\ManyToOne(cascade: ["persist"])]
  67.     #[ORM\JoinColumn(nullabletrue)]
  68.     private ?Attachment $thumbnail null;
  69.     #[ORM\Column(length3nullabletrueoptions: ["default" => "CD"])]
  70.     private ?string $country;
  71.     #[ORM\Column(length255nullabletrue)]
  72.     private ?string $lastLoginIp null;
  73.     #[ORM\Column(nullabletrue)]
  74.     private ?DateTimeImmutable $lastLoginAt null;
  75.     #[ORM\Column(nullabletrue)]
  76.     private ?DateTimeInterface $bannedAt null;
  77.     #[ORM\Column(nullabletrue)]
  78.     private ?string $confirmationToken null;
  79.     #[ORM\Column(type'boolean')]
  80.     private bool $isVerified false;
  81.     #[ORM\Column(type'string'nullabletrueoptions: ["default" => null])]
  82.     private ?string $theme null;
  83.     #[ORM\OneToMany(mappedBy'author'targetEntityArticle::class, orphanRemovaltrue)]
  84.     private Collection $articles;
  85.     #[ORM\OneToMany(mappedBy'author'targetEntityService::class, orphanRemovaltrue)]
  86.     private Collection $services;
  87.     #[ORM\OneToMany(mappedBy'user'targetEntityEstablishment::class, orphanRemovaltrue)]
  88.     private Collection $establishments;
  89.     #[ORM\OneToMany(mappedBy'user'targetEntityConnectionLog::class, orphanRemovaltrue)]
  90.     private Collection $connectionLogs;
  91.     #[ORM\Column(options: ['default' => true])]
  92.     private ?bool $isActive true;
  93.     #[ORM\OneToMany(mappedBy'user'targetEntityOTP::class, orphanRemovaltrue)]
  94.     private Collection $OTPs;
  95.     #[ORM\Column(length255nullabletrue)]
  96.     private ?string $city null;
  97.     #[ORM\Column(length255nullabletrue)]
  98.     private ?string $latitude null;
  99.     #[ORM\Column(length255nullabletrue)]
  100.     private ?string $longitude null;
  101.     #[ORM\Column(length255nullabletrue)]
  102.     private ?string $timeZone null;
  103.     #[ORM\Column(length255nullabletrue)]
  104.     private ?string $postal null;
  105.     #[ORM\Column(length255nullabletrue)]
  106.     private ?string $ip null;
  107.     #[ORM\Column(length255nullabletrue)]
  108.     private ?string $subdivisions null;
  109.     #[ORM\Column(length255nullabletrue)]
  110.     private ?string $isp null;
  111.     #[ORM\OneToMany(mappedBy'createdBy'targetEntityCms::class)]
  112.     private Collection $cms;
  113.     #[ORM\OneToOne(inversedBy'user'cascade: ['persist''remove'])]
  114.     private ?UserDetail $detail null;
  115.     #[ORM\OneToMany(mappedBy'user'targetEntityMutuality::class)]
  116.     private Collection $mutualities;
  117.     #[ORM\Column(length255options: ['default' => "fr"])]
  118.     #[Assert\Locale(
  119.         canonicalizetrue,
  120.     )]
  121.     private ?string $locale;
  122.     #[ORM\OneToMany(mappedBy'author'targetEntityPlanning::class)]
  123.     private Collection $createdPlannings;
  124.     #[ORM\ManyToMany(targetEntityPlanning::class, mappedBy'users')]
  125.     private Collection $plannings;
  126.     public function __construct($ip null)
  127.     {
  128.         if (is_null($this->createdAt)) $this->createdAt = new DateTimeImmutable();
  129.         $this->updatedAt = new DateTimeImmutable();
  130.         $this->OTPs = new ArrayCollection();
  131.         $this->country 'CD';
  132.         $this->locale 'fr';
  133.         if ($ip$this->initGeoIp($ip);
  134.         $this->articles = new ArrayCollection();
  135.         $this->services = new ArrayCollection();
  136.         $this->establishments = new ArrayCollection();
  137.         $this->connectionLogs = new ArrayCollection();
  138.         $this->cms = new ArrayCollection();
  139.         $this->mutualities = new ArrayCollection();
  140.     }
  141.     public function __serialize(): array
  142.     {
  143.         return [$this->id$this->phone$this->email$this->password,];
  144.     }
  145.     public function __unserialize(array $data): void
  146.     {
  147.         if (count($data) === 4) [$this->id$this->phone$this->email$this->password,] = $data;
  148.     }
  149.     public function __toString(): string
  150.     {
  151.         return "$this->firstname $this->lastname";
  152.     }
  153.     public function getId(): ?int
  154.     {
  155.         return $this->id;
  156.     }
  157.     public function getName()
  158.     {
  159.         return "$this->firstname $this->lastname";
  160.     }
  161.     public function getLastname(): ?string
  162.     {
  163.         return $this->lastname;
  164.     }
  165.     public function setLastname(?string $lastname): self
  166.     {
  167.         $this->lastname $lastname;
  168.         return $this;
  169.     }
  170.     public function getFirstname(): ?string
  171.     {
  172.         return $this->firstname;
  173.     }
  174.     public function setFirstname(string $firstname): self
  175.     {
  176.         $this->firstname $firstname;
  177.         return $this;
  178.     }
  179.     public function getEmail(): ?string
  180.     {
  181.         return $this->email;
  182.     }
  183.     public function setEmail(string $email): self
  184.     {
  185.         $this->email $email;
  186.         return $this;
  187.     }
  188.     /**
  189.      * A visual identifier that represents this user.
  190.      *
  191.      * @see UserInterface
  192.      */
  193.     public function getUserIdentifier(): string
  194.     {
  195.         return (string)$this->email;
  196.     }
  197.     /**
  198.      * @see UserInterface
  199.      */
  200.     public function getRoles(): array
  201.     {
  202.         $roles $this->roles;
  203.         // guarantee every user at least has ROLE_USER
  204.         $roles[] = 'ROLE_USER';
  205.         return array_unique($roles);
  206.     }
  207.     public function setRoles(array $roles): self
  208.     {
  209.         $this->roles $roles;
  210.         return $this;
  211.     }
  212.     /**
  213.      * @see PasswordAuthenticatedUserInterface
  214.      */
  215.     public function getPassword(): string
  216.     {
  217.         return $this->password;
  218.     }
  219.     public function setPassword(string $password): self
  220.     {
  221.         $this->password $password;
  222.         return $this;
  223.     }
  224.     /**
  225.      * @see UserInterface
  226.      */
  227.     public function eraseCredentials()
  228.     {
  229.         // If you store any temporary, sensitive data on the user, clear it here
  230.         // $this->plainPassword = null;
  231.     }
  232.     public function getPhone(): ?string
  233.     {
  234.         return $this->phone;
  235.     }
  236.     public function getThumbnail(): ?Attachment
  237.     {
  238.         return $this->thumbnail;
  239.     }
  240.     public function setThumbnail(?Attachment $thumbnail): self
  241.     {
  242.         $this->thumbnail $thumbnail;
  243.         return $this;
  244.     }
  245.     public function setPhone(string $phone): static
  246.     {
  247.        $this->phone GeoIP::phoneResolve($phone$this->country);
  248.         return $this;
  249.     }
  250.     public function isIsActive(): ?bool
  251.     {
  252.         return $this->isActive;
  253.     }
  254.     public function setIsActive(bool $isActive): static
  255.     {
  256.         $this->isActive $isActive;
  257.         return $this;
  258.     }
  259.     public function getCity(): ?string
  260.     {
  261.         return $this->city;
  262.     }
  263.     public function setCity(?string $city): self
  264.     {
  265.         $this->city $city;
  266.         return $this;
  267.     }
  268.     public function getLatitude(): ?string
  269.     {
  270.         return $this->latitude;
  271.     }
  272.     public function setLatitude(?string $latitude): self
  273.     {
  274.         $this->latitude $latitude;
  275.         return $this;
  276.     }
  277.     public function getLongitude(): ?string
  278.     {
  279.         return $this->longitude;
  280.     }
  281.     public function setLongitude(?string $longitude): self
  282.     {
  283.         $this->longitude $longitude;
  284.         return $this;
  285.     }
  286.     public function getTimeZone(): ?string
  287.     {
  288.         return $this->timeZone;
  289.     }
  290.     public function setTimeZone(?string $timeZone): self
  291.     {
  292.         $this->timeZone $timeZone;
  293.         return $this;
  294.     }
  295.     public function getPostal(): ?string
  296.     {
  297.         return $this->postal;
  298.     }
  299.     public function setPostal(?string $postal): self
  300.     {
  301.         $this->postal $postal;
  302.         return $this;
  303.     }
  304.     public function getIp(): ?string
  305.     {
  306.         return $this->ip;
  307.     }
  308.     public function setIp(?string $ip): self
  309.     {
  310.         $this->ip $ip;
  311.         return $this;
  312.     }
  313.     public function getSubdivisions(): ?string
  314.     {
  315.         return $this->subdivisions;
  316.     }
  317.     public function setSubdivisions(?string $subdivisions): self
  318.     {
  319.         $this->subdivisions $subdivisions;
  320.         return $this;
  321.     }
  322.     public function getIsp(): ?string
  323.     {
  324.         return $this->isp;
  325.     }
  326.     public function setIsp(?string $isp): self
  327.     {
  328.         $this->isp $isp;
  329.         return $this;
  330.     }
  331.     public function getLastLogin(): ?DateTimeImmutable
  332.     {
  333.         return $this->lastLogin;
  334.     }
  335.     public function setLastLogin(DateTimeImmutable $lastLogin): self
  336.     {
  337.         $this->lastLogin $lastLogin;
  338.         return $this;
  339.     }
  340.     public function getCountry(): ?string
  341.     {
  342.         return $this->country ?: 'CD';
  343.     }
  344.     public function setCountry(?string $country): self
  345.     {
  346.         $this->country $country;
  347.         return $this;
  348.     }
  349.     public function getLastLoginIp(): ?string
  350.     {
  351.         return $this->lastLoginIp;
  352.     }
  353.     public function setLastLoginIp(?string $lastLoginIp): User
  354.     {
  355.         $this->lastLoginIp $lastLoginIp;
  356.         return $this;
  357.     }
  358.     public function getLastLoginAt(): ?DateTimeImmutable
  359.     {
  360.         return $this->lastLoginAt;
  361.     }
  362.     public function setLastLoginAt(DateTimeImmutable $lastLoginAt): self
  363.     {
  364.         $this->lastLoginAt $lastLoginAt;
  365.         return $this;
  366.     }
  367.     public function isBanned(): bool
  368.     {
  369.         return null !== $this->bannedAt;
  370.     }
  371.     public function getBannedAt(): ?DateTimeInterface
  372.     {
  373.         return $this->bannedAt;
  374.     }
  375.     public function setBannedAt(?DateTimeInterface $bannedAt): User
  376.     {
  377.         $this->bannedAt $bannedAt;
  378.         return $this;
  379.     }
  380.     public function getConfirmationToken(): ?string
  381.     {
  382.         return $this->confirmationToken;
  383.     }
  384.     public function setConfirmationToken(?string $confirmationToken): User
  385.     {
  386.         $this->confirmationToken $confirmationToken;
  387.         return $this;
  388.     }
  389.     public function canLogin(): bool
  390.     {
  391.         return !$this->isBanned() && null === $this->getConfirmationToken();
  392.     }
  393.     public function isVerified(): bool
  394.     {
  395.         return $this->isVerified;
  396.     }
  397.     public function setIsVerified(bool $isVerified): self
  398.     {
  399.         $this->isVerified $isVerified;
  400.         return $this;
  401.     }
  402.     public function getTheme(): ?string
  403.     {
  404.         return $this->theme;
  405.     }
  406.     public function setTheme(?string $theme): self
  407.     {
  408.         $this->theme $theme;
  409.         return $this;
  410.     }
  411.     /**
  412.      * @return Collection<int, Article>
  413.      */
  414.     public function getArticles(): Collection
  415.     {
  416.         return $this->articles;
  417.     }
  418.     public function addArticle(Article $article): self
  419.     {
  420.         if (!$this->articles->contains($article)) {
  421.             $this->articles->add($article);
  422.             $article->setAuthor($this);
  423.         }
  424.         return $this;
  425.     }
  426.     public function removeArticle(Article $article): self
  427.     {
  428.         if ($this->articles->removeElement($article)) {
  429.             if ($article->getAuthor() === $this) {
  430.                 $article->setAuthor(null);
  431.             }
  432.         }
  433.         return $this;
  434.     }
  435.     /**
  436.      * @return Collection<int, Service>
  437.      */
  438.     public function getServices(): Collection
  439.     {
  440.         return $this->services;
  441.     }
  442.     public function addService(Service $service): self
  443.     {
  444.         if (!$this->services->contains($service)) {
  445.             $this->services->add($service);
  446.             $service->setAuthor($this);
  447.         }
  448.         return $this;
  449.     }
  450.     public function removeService(Service $service): self
  451.     {
  452.         if ($this->services->removeElement($service)) {
  453.             // set the owning side to null (unless already changed)
  454.             if ($service->getAuthor() === $this) {
  455.                 $service->setAuthor(null);
  456.             }
  457.         }
  458.         return $this;
  459.     }
  460.     /**
  461.      * @return Collection<int, Establishment>
  462.      */
  463.     public function getEstablishments(): Collection
  464.     {
  465.         return $this->establishments;
  466.     }
  467.     public function addEstablishment(Establishment $establishment): self
  468.     {
  469.         if (!$this->establishments->contains($establishment)) {
  470.             $this->establishments->add($establishment);
  471.             $establishment->setUser($this);
  472.         }
  473.         return $this;
  474.     }
  475.     public function removeEstablishment(Establishment $establishment): self
  476.     {
  477.         if ($this->establishments->removeElement($establishment)) {
  478.             // set the owning side to null (unless already changed)
  479.             if ($establishment->getUser() === $this) {
  480.                 $establishment->setUser(null);
  481.             }
  482.         }
  483.         return $this;
  484.     }
  485.     /**
  486.      * @return Collection<int, ConnectionLog>
  487.      */
  488.     public function getConnectionLogs(): Collection
  489.     {
  490.         return $this->connectionLogs;
  491.     }
  492.     public function addConnectionLog(ConnectionLog $connectionLog): self
  493.     {
  494.         if (!$this->connectionLogs->contains($connectionLog)) {
  495.             $this->connectionLogs->add($connectionLog);
  496.             $connectionLog->setUser($this);
  497.         }
  498.         return $this;
  499.     }
  500.     public function removeConnectionLog(ConnectionLog $connectionLog): self
  501.     {
  502.         if ($this->connectionLogs->removeElement($connectionLog)) {
  503.             // set the owning side to null (unless already changed)
  504.             if ($connectionLog->getUser() === $this) {
  505.                 $connectionLog->setUser(null);
  506.             }
  507.         }
  508.         return $this;
  509.     }
  510.     /**
  511.      * @return Collection<int, Cms>
  512.      */
  513.     public function getCms(): Collection
  514.     {
  515.         return $this->cms;
  516.     }
  517.     public function addCm(Cms $cm): self
  518.     {
  519.         if (!$this->cms->contains($cm)) {
  520.             $this->cms->add($cm);
  521.             $cm->setCreatedBy($this);
  522.         }
  523.         return $this;
  524.     }
  525.     public function removeCm(Cms $cm): self
  526.     {
  527.         if ($this->cms->removeElement($cm)) {
  528.             // set the owning side to null (unless already changed)
  529.             if ($cm->getCreatedBy() === $this) {
  530.                 $cm->setCreatedBy(null);
  531.             }
  532.         }
  533.         return $this;
  534.     }
  535.     /**
  536.      * @param string $ip
  537.      * @throws \Exception
  538.      */
  539.     public function initGeoIp(string $ip): void
  540.     {
  541.         try {
  542.             $isLocal in_array($ip, ['127.0.0.1''::1']);
  543.             $ip $isLocal '41.78.192.90' $ip;
  544.             $record GeoIP::check($ip);
  545.             $this->setCountry($record->countryCode ?? null); // 'US'
  546.             $this->setIsp($record->isp ?? null);
  547.             $this->setSubdivisions($record->regionName ?? null);
  548.             $this->setCity($record->city ?? null); // 'Minneapolis'
  549.             $this->setPostal($record->zip ?? null); // '55455'
  550.             $this->setIp($record->query ?? null);
  551.             $this->setTimeZone($record->timezone ?? null);
  552.             $this->setLatitude($record->lat ?? null); // 44.9733
  553.             $this->setLongitude($record->lon ?? null);
  554.         } catch (AddressNotFoundException $ex) {
  555.             throw new \Exception("It wasn't possible to retrieve information about the providen IP");
  556.         }
  557.     }
  558.     public function isIsPhoneVerified(): ?bool
  559.     {
  560.         return $this->isPhoneVerified;
  561.     }
  562.     public function setIsPhoneVerified(bool $isPhoneVerified): static
  563.     {
  564.         $this->isPhoneVerified $isPhoneVerified;
  565.         return $this;
  566.     }
  567.     public function getGender(): ?string
  568.     {
  569.         return $this->gender;
  570.     }
  571.     public function setGender(string $gender): static
  572.     {
  573.         $this->gender $gender;
  574.         return $this;
  575.     }
  576.     public function getDetail(): ?UserDetail
  577.     {
  578.         return $this->detail;
  579.     }
  580.     public function setDetail(?UserDetail $detail): static
  581.     {
  582.         $this->detail $detail;
  583.         return $this;
  584.     }
  585.     /**
  586.      * @return Collection<int, Mutuality>
  587.      */
  588.     public function getMutualities(): Collection
  589.     {
  590.         return $this->mutualities;
  591.     }
  592.     public function addMutuality(Mutuality $mutuality): static
  593.     {
  594.         if (!$this->mutualities->contains($mutuality)) {
  595.             $this->mutualities->add($mutuality);
  596.             $mutuality->setUser($this);
  597.         }
  598.         return $this;
  599.     }
  600.     public function removeMutuality(Mutuality $mutuality): static
  601.     {
  602.         if ($this->mutualities->removeElement($mutuality)) {
  603.             // set the owning side to null (unless already changed)
  604.             if ($mutuality->getUser() === $this) {
  605.                 $mutuality->setUser(null);
  606.             }
  607.         }
  608.         return $this;
  609.     }
  610.     public function getFullname(): string
  611.     {
  612.         return $this->firstname ' ' $this->lastname;
  613.     }
  614.     public function getLocale(): ?string
  615.     {
  616.         return $this->locale;
  617.     }
  618.     public function setLocale(string $locale): static
  619.     {
  620.         $this->locale $locale;
  621.         return $this;
  622.     }
  623. }