src/Entity/User.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Attribute\Searchable;
  4. use Overblog\GraphQLBundle\Annotation\Field;
  5. use App\Repository\UserRepository;
  6. use App\Traits\DateTimeTrait;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. use Rollerworks\Component\PasswordStrength\Validator\Constraints as RollerworksPassword;
  15. use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;
  16. #[ORM\Entity(repositoryClassUserRepository::class)]
  17. #[ORM\HasLifecycleCallbacks]
  18. #[UniqueEntity(fields"email"message"This email is already used.")]
  19. class User extends BasicEntity implements UserInterfacePasswordAuthenticatedUserInterfaceTwoFactorInterface
  20. {
  21.     use DateTimeTrait;
  22.     public const ROLE_ADMIN 'ROLE_ADMIN';
  23.     public const ROLE_CUSTOMER 'ROLE_CUSTOMER';
  24.     public const ROLE_OWNER 'ROLE_OWNER';
  25.     #[ORM\Id]
  26.     #[ORM\GeneratedValue]
  27.     #[ORM\Column(type'integer')]
  28.     #[Field(name"id"type"int")]
  29.     private $id;
  30.     #[ORM\Column(type'string'length255)]
  31.     #[Assert\NotBlank]
  32.     #[Field(name"first_name"type"string")]
  33.     #[Searchable(name'first_name')]
  34.     private $firstName;
  35.     #[ORM\Column(type'string'length255)]
  36.     #[Assert\NotBlank]
  37.     #[Field(name"last_name"type"string")]
  38.     #[Searchable(name'last_name')]
  39.     private $lastName;
  40.     #[ORM\Column(type'string'length255nullabletrue)]
  41.     #[Assert\Email]
  42.     #[Field(name"email"type"string")]
  43.     #[Searchable(name'email')]
  44.     private $email;
  45.     #[ORM\Column(type'string'length255nullabletrue)]
  46.     #[RollerworksPassword\PasswordStrength(
  47.         minStrength4,
  48.         minLength12,
  49.         message"Your password must contain at least 12 alphanumeric characters"
  50.     )]
  51.     #[Assert\NotCompromisedPassword]
  52.     #[Field(name"password"type"string")]
  53.     private $password;
  54.     #[ORM\Column(type'string'length30nullabletrue)]
  55.     #[Field(name"phone_number"type"string")]
  56.     #[Searchable(name'phone_number')]
  57.     private $phoneNumber;
  58.     #[ORM\Column(type'json')]
  59.     #[Field(name"roles"type"string")]
  60.     private $roles = [];
  61.     #[ORM\Column(type'boolean')]
  62.     #[Field(name"is_actif"type"boolean")]
  63.     private $isActif;
  64.     #[ORM\Column(type'string'length255)]
  65.     private $registration_token;
  66.     #[ORM\Column(type'string'length255nullabletrue)]
  67.     #[Field(name"avatar"type"string")]
  68.     private $avatar;
  69.     #[ORM\Column(type'string'length255nullabletrue)]
  70.     #[Field(name"forgotpwd_token"type"string")]
  71.     private $forgotpwd_token;
  72.     #[ORM\Column(name'googleAuthenticatorSecret'type'string'nullabletrue)]
  73.     private $googleAuthenticatorSecret;
  74.     #[ORM\Column(type'boolean')]
  75.     #[Field(name"is_enabled_2fa"type"boolean")]
  76.     private $isEnabled2FA;
  77.     #[ORM\Column(type'boolean')]
  78.     #[Field(name"is_enable_notif"type"boolean")]
  79.     private $isEnableNotif true;
  80.     #[ORM\ManyToMany(targetEntityService::class)]
  81.     private $notifications;
  82.     #[ORM\ManyToMany(targetEntityOrganisation::class, inversedBy"users")]
  83.     #[Field(name"organisations")]
  84.     private $organisations;
  85.     #[ORM\Column(options: ['default' => false])]
  86.     #[Field(name"is_deleted"type"boolean")]
  87.     private ?bool $isDeleted false;
  88.     #[ORM\Column(options: ['default' => 0])]
  89.     private ?int $attemptRecoverPwd 0;
  90.     #[ORM\Column(nullabletrue)]
  91.     private ?\DateTimeImmutable $forgotPwdExpireAt null;
  92.     #[ORM\Column(length255nullabletrue)]
  93.     #[Field(name"api_token"type"string")]
  94.     private ?string $apiToken null;
  95.     #[ORM\OneToMany(mappedBy'calledBy'targetEntityWebServiceLog::class)]
  96.     private Collection $webServiceLogs;
  97.     public function __construct()
  98.     {
  99.         $this->notifications = new ArrayCollection();
  100.         $this->organisations = new ArrayCollection();
  101.         $this->webServiceLogs = new ArrayCollection();
  102.     }
  103.     public function getId(): ?int
  104.     {
  105.         return $this->id;
  106.     }
  107.     public function getFirstName(): ?string
  108.     {
  109.         return $this->firstName;
  110.     }
  111.     public function setFirstName(string $firstName): User
  112.     {
  113.         $this->firstName $firstName;
  114.         return $this;
  115.     }
  116.     public function getLastName(): ?string
  117.     {
  118.         return $this->lastName;
  119.     }
  120.     public function setLastName(string $lastName): User
  121.     {
  122.         $this->lastName $lastName;
  123.         return $this;
  124.     }
  125.     public function getEmail(): ?string
  126.     {
  127.         return $this->email;
  128.     }
  129.     public function setEmail(?string $email): User
  130.     {
  131.         $this->email $email;
  132.         return $this;
  133.     }
  134.     public function getPassword(): ?string
  135.     {
  136.         return $this->password;
  137.     }
  138.     public function setPassword(string $password): User
  139.     {
  140.         $this->password $password;
  141.         return $this;
  142.     }
  143.     public function getPhoneNumber(): ?string
  144.     {
  145.         return $this->phoneNumber;
  146.     }
  147.     public function setPhoneNumber(?string $phoneNumber): User
  148.     {
  149.         $this->phoneNumber $phoneNumber;
  150.         return $this;
  151.     }
  152.     public function getRoles(): ?array
  153.     {
  154.         $roles $this->roles;
  155.         // guarantee every user at least has ROLE_USER
  156.         $roles[] = 'ROLE_USER';
  157.         return array_unique($roles);
  158.     }
  159.     public function setRoles(array $roles): self
  160.     {
  161.         $this->roles $roles;
  162.         return $this;
  163.     }
  164.     public function getSalt(): ?string
  165.     {
  166.         return null;
  167.     }
  168.     public function eraseCredentials()
  169.     {
  170.     }
  171.     public function getUsername(): string
  172.     {
  173.         return $this->getUserIdentifier();
  174.     }
  175.     public function getUserIdentifier(): string
  176.     {
  177.         return $this->email;
  178.     }
  179.     public function isAdmin(): bool
  180.     {
  181.         return in_array(self::ROLE_ADMIN$this->roles);
  182.     }
  183.     public function isOwner(): bool
  184.     {
  185.         return in_array(self::ROLE_OWNER$this->roles);
  186.     }
  187.     public function isCustomer(): bool
  188.     {
  189.         return in_array(self::ROLE_CUSTOMER$this->roles);
  190.     }
  191.     public function getIsActif(): ?bool
  192.     {
  193.         return $this->isActif;
  194.     }
  195.     public function setIsActif(bool $isActif): self
  196.     {
  197.         $this->isActif $isActif;
  198.         return $this;
  199.     }
  200.     public function getRegistrationToken(): ?string
  201.     {
  202.         return $this->registration_token;
  203.     }
  204.     public function setRegistrationToken(string $registration_token): self
  205.     {
  206.         $this->registration_token $registration_token;
  207.         return $this;
  208.     }
  209.     #[ORM\PrePersist]
  210.     public function generateAndSaveToken(): void
  211.     {
  212.         $this->setRegistrationToken(md5(uniqid().$this->email));
  213.     }
  214.     // public function getAvatar(): ?string
  215.     // {
  216.     //     return $this->avatar;
  217.     // }
  218.     public function getAvatar(): ?string
  219.     {
  220.         $path __DIR__.'/../../public/uploads/avatar/'.$this->avatar// Path to the image file
  221.         if (!empty($this->avatar) && file_exists($path)) {
  222.             $type pathinfo($pathPATHINFO_EXTENSION); // Get the file extension
  223.             $data file_get_contents($path); // Read the file's contents
  224.             $base64 'data:image/' $type ';base64,' base64_encode($data); // Convert to base64
  225.             return $base64// Return the base64 encoded string
  226.         }
  227.         return null// Return null if the file doesn't exist
  228.     }
  229.     public function setAvatar(?string $avatar): self
  230.     {
  231.         $this->avatar $avatar;
  232.         return $this;
  233.     }
  234.     public function getForgotpwdToken(): ?string
  235.     {
  236.         return $this->forgotpwd_token;
  237.     }
  238.     public function setForgotpwdToken(?string $forgotpwd_token): self
  239.     {
  240.         $this->forgotpwd_token $forgotpwd_token;
  241.         return $this;
  242.     }
  243.     public function getGoogleAuthenticatorSecret(): ?string
  244.     {
  245.         return $this->googleAuthenticatorSecret;
  246.     }
  247.     public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): void
  248.     {
  249.         $this->googleAuthenticatorSecret $googleAuthenticatorSecret;
  250.     }
  251.     public function isGoogleAuthenticatorEnabled(): bool
  252.     {
  253.         return $this->getIsEnabled2FA();
  254.     }
  255.     public function getGoogleAuthenticatorUsername(): string
  256.     {
  257.         return $this->getUsername();
  258.     }
  259.     public function getIsEnabled2FA(): ?bool
  260.     {
  261.         return $this->isEnabled2FA;
  262.     }
  263.     public function setIsEnabled2FA(bool $isEnabled2FA): self
  264.     {
  265.         $this->isEnabled2FA $isEnabled2FA;
  266.         return $this;
  267.     }
  268.     public function getIsEnableNotif(): ?bool
  269.     {
  270.         return $this->isEnableNotif;
  271.     }
  272.     public function setIsEnableNotif(bool $isEnableNotif): self
  273.     {
  274.         $this->isEnableNotif $isEnableNotif;
  275.         return $this;
  276.     }
  277.     /**
  278.      * @return Collection<int, Service>
  279.      */
  280.     public function getNotifications(): Collection
  281.     {
  282.         return $this->notifications;
  283.     }
  284.     public function addNotification(Service $notification): self
  285.     {
  286.         if (!$this->notifications->contains($notification)) {
  287.             $this->notifications[] = $notification;
  288.         }
  289.         return $this;
  290.     }
  291.     public function removeNotification(Service $notification): self
  292.     {
  293.         $this->notifications->removeElement($notification);
  294.         return $this;
  295.     }
  296.     public function haveNotif(Service $service): bool
  297.     {
  298.         return $this->notifications->contains($service);
  299.     }
  300.     /**
  301.      * @return Collection<int, Organisation>
  302.      */
  303.     public function getOrganisations(): Collection
  304.     {
  305.         return $this->organisations;
  306.     }
  307.     public function addOrganisation(Organisation $organisation): self
  308.     {
  309.         if (!$this->organisations->contains($organisation)) {
  310.             $this->organisations[] = $organisation;
  311.         }
  312.         return $this;
  313.     }
  314.     public function removeOrganisation(Organisation $organisation): self
  315.     {
  316.         $this->organisations->removeElement($organisation);
  317.         return $this;
  318.     }
  319.     /**
  320.      * @param array $organisations
  321.      * @return $this
  322.      */
  323.     public function setOrganisations(array $organisations): self
  324.     {
  325.         $this->organisations = new ArrayCollection();
  326.         foreach ($organisations as $organisation) {
  327.             $this->addOrganisation($organisation);
  328.         }
  329.         return $this;
  330.     }
  331.     public function isIsDeleted(): ?bool
  332.     {
  333.         return $this->isDeleted;
  334.     }
  335.     public function setIsDeleted(bool $isDeleted): static
  336.     {
  337.         $this->isDeleted $isDeleted;
  338.         return $this;
  339.     }
  340.     public function getAttemptRecoverPwd(): ?int
  341.     {
  342.         return $this->attemptRecoverPwd;
  343.     }
  344.     public function setAttemptRecoverPwd(int $attemptRecoverPwd): static
  345.     {
  346.         $this->attemptRecoverPwd $attemptRecoverPwd;
  347.         return $this;
  348.     }
  349.     public function getForgotPwdExpireAt(): ?\DateTimeImmutable
  350.     {
  351.         return $this->forgotPwdExpireAt;
  352.     }
  353.     public function setForgotPwdExpireAt(?\DateTimeImmutable $forgotPwdExpireAt): static
  354.     {
  355.         $this->forgotPwdExpireAt $forgotPwdExpireAt;
  356.         return $this;
  357.     }
  358.     public function getApiToken(): ?string
  359.     {
  360.         return $this->apiToken;
  361.     }
  362.     public function setApiToken(string $apiToken): static
  363.     {
  364.         $this->apiToken $apiToken;
  365.         return $this;
  366.     }
  367.     /**
  368.      * @return Collection<int, WebServiceLog>
  369.      */
  370.     public function getWebServiceLogs(): Collection
  371.     {
  372.         return $this->webServiceLogs;
  373.     }
  374.     public function addWebServiceLog(WebServiceLog $webServiceLog): static
  375.     {
  376.         if (!$this->webServiceLogs->contains($webServiceLog)) {
  377.             $this->webServiceLogs->add($webServiceLog);
  378.             $webServiceLog->setCalledBy($this);
  379.         }
  380.         return $this;
  381.     }
  382.     public function removeWebServiceLog(WebServiceLog $webServiceLog): static
  383.     {
  384.         if ($this->webServiceLogs->removeElement($webServiceLog)) {
  385.             // set the owning side to null (unless already changed)
  386.             if ($webServiceLog->getCalledBy() === $this) {
  387.                 $webServiceLog->setCalledBy(null);
  388.             }
  389.         }
  390.         return $this;
  391.     }
  392. }