src/Entity/User.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use App\Service\ToolsService;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Hslavich\OneloginSamlBundle\Security\User\SamlUserInterface;
  9. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. /**
  15.  * @ORM\Entity(repositoryClass=UserRepository::class)
  16.  * @ORM\Table(name="`user`")
  17.  * @UniqueEntity(
  18.  *     fields={"email", "category"},
  19.  *     errorPath="email",
  20.  *     message="The email is already in use",
  21.  *     ignoreNull=false
  22.  * )
  23.  * @ORM\HasLifecycleCallbacks()
  24.  */
  25. class User implements UserInterfacePasswordAuthenticatedUserInterfaceSamlUserInterface
  26. {
  27.     const GROUP_ADMIN 'admin';
  28.     const GROUP_COMPANY 'company';
  29.     const GROUP_CLIENT 'client';
  30.     const GROUP_SPECIALIST 'specialist';
  31.     const ROLE_USER 'ROLE_USER';
  32.     const ROLE_CLIENT 'ROLE_CLIENT';
  33.     const ROLE_COMPANY 'ROLE_COMPANY';
  34.     const ROLE_EXTERNAL_COMPANY 'ROLE_EXTERNAL_COMPANY';
  35.     const ROLE_ADMIN 'ROLE_ADMIN';
  36.     const ROLE_SUPER_ADMIN 'ROLE_SUPER_ADMIN';
  37.     const ROLE_SPECIALIST 'ROLE_SPECIALIST';
  38.     const GROUPS = [
  39.         self::GROUP_ADMIN,
  40.         self::GROUP_COMPANY,
  41.         self::GROUP_CLIENT,
  42.         self::GROUP_SPECIALIST,
  43.     ];
  44.     /**
  45.      * @ORM\Id
  46.      * @ORM\GeneratedValue
  47.      * @ORM\Column(type="integer")
  48.      * @Groups("user_favorite:read")
  49.      */
  50.     protected $id;
  51.     /**
  52.      * @ORM\Column(type="string", length=180)
  53.      * @Assert\Email
  54.      * @Assert\NotBlank
  55.      * @Groups("user_favorite:read")
  56.      */
  57.     protected $email;
  58.     /**
  59.      * @ORM\Column(type="json")
  60.      * @Groups("user_favorite:read")
  61.      */
  62.     protected $roles = [];
  63.     /**
  64.      * @var string|null The hashed password
  65.      * @ORM\Column(type="string")
  66.      * @Assert\Length(min="8", max="255")
  67.      * @Assert\NotBlank
  68.      */
  69.     protected $password null;
  70.     /**
  71.      * @ORM\Column(type="datetime")
  72.      */
  73.     protected $createdAt;
  74.     /**
  75.      * @ORM\Column(type="datetime", nullable=true)
  76.      */
  77.     protected $lastLogin;
  78.     /**
  79.      * @ORM\Column(type="string", length=255)
  80.      * @Assert\NotBlank()
  81.      * @Assert\Length(max="255", min="0")
  82.      * @Groups("user_favorite:read")
  83.      * @var string
  84.      */
  85.     protected $name '';
  86.     /**
  87.      * @ORM\Column(type="string", length=50)
  88.      * @Assert\Choice(choices=self::GROUPS)
  89.      * @Groups("user_favorite:read")
  90.      */
  91.     protected $category;
  92.     /**
  93.      * @ORM\Column(type="boolean")
  94.      * @Groups("user_favorite:read")
  95.      */
  96.     protected $active true;
  97.     /**
  98.      * @ORM\Column(type="datetime", nullable=true)
  99.      */
  100.     protected $banStart;
  101.     /**
  102.      * @ORM\Column(type="datetime", nullable=true)
  103.      */
  104.     protected $banEnd;
  105.     /**
  106.      * @ORM\Column(type="string", length=255, nullable=true, unique=true)
  107.      */
  108.     protected $resetToken;
  109.     /**
  110.      * @ORM\OneToOne(targetEntity=Specialist::class, mappedBy="user", cascade={"persist", "remove"})
  111.      */
  112.     protected $specialist;
  113.     /**
  114.      * @ORM\OneToOne(targetEntity=Company::class, mappedBy="user", cascade={"persist", "remove"})
  115.      */
  116.     protected $company;
  117.     /**
  118.      * @ORM\OneToOne(targetEntity=Client::class, mappedBy="user", cascade={"persist", "remove"})
  119.      */
  120.     protected $client;
  121.     /**
  122.      * @var bool
  123.      */
  124.     protected $_hashPwd false;
  125.     /**
  126.      * @var null|string
  127.      */
  128.     private $token null;
  129.     /**
  130.      * @ORM\OneToMany(targetEntity=Document::class, mappedBy="owner")
  131.      */
  132.     private $documents;
  133.     /**
  134.      * @ORM\OneToMany(targetEntity=MarketplaceReservation::class, mappedBy="user")
  135.      */
  136.     private $marketplaceReservations;
  137.     /**
  138.      * @ORM\Column(type="string", length=255, nullable=true)
  139.      * @Groups("user_favorite:read")
  140.      */
  141.     private $firstName;
  142.     /**
  143.      * @ORM\Column(type="string", length=255, nullable=true)
  144.      * @Groups("user_favorite:read")
  145.      */
  146.     private $lastName;
  147.     /**
  148.      * @ORM\OneToMany(targetEntity=UserFavorite::class, mappedBy="user", cascade={"persist", "remove"})
  149.      */
  150.     private $favorites;
  151.     /**
  152.      * @ORM\ManyToOne(targetEntity=Segmentation::class, inversedBy="users", cascade={"persist"})
  153.      */
  154.     private $segmentation;
  155.     /**
  156.      * @ORM\Column(type="string", length=255, nullable=true, options={"default": "FR"})
  157.      */
  158.     private $country;
  159.     public function __construct()
  160.     {
  161.         $this->createdAt = new \DateTime();
  162.         $this->roles[] = 'ROLE_USER';
  163.         $this->documents = new ArrayCollection();
  164.         $this->marketplaceReservations = new ArrayCollection();
  165.         $this->favorites = new ArrayCollection();
  166.     }
  167.     public function getId(): ?int
  168.     {
  169.         return $this->id;
  170.     }
  171.     public function getEmail(): ?string
  172.     {
  173.         return $this->email;
  174.     }
  175.     public function setEmail(string $email): self
  176.     {
  177.         $this->email $email;
  178.         return $this;
  179.     }
  180.     /**
  181.      * A visual identifier that represents this user.
  182.      *
  183.      * @see UserInterface
  184.      */
  185.     public function getUserIdentifier(): string
  186.     {
  187.         return (string)$this->email;
  188.     }
  189.     /**
  190.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  191.      */
  192.     public function getUsername(): string
  193.     {
  194.         return (string)$this->email;
  195.     }
  196.     /**
  197.      * @see UserInterface
  198.      */
  199.     public function getRoles(): array
  200.     {
  201.         $roles $this->roles;
  202.         // guarantee every user at least has ROLE_USER
  203.         $roles[] = 'ROLE_USER';
  204.         return array_unique($roles);
  205.     }
  206.     /**
  207.      * @param string $role
  208.      * @return bool
  209.      */
  210.     public function hasRole(string $role): bool
  211.     {
  212.         return in_array($role$this->roles);
  213.     }
  214.     /**
  215.      * @see UserInterface
  216.      */
  217.     public function addRole(string $role): self
  218.     {
  219.         if (!in_array($role$this->roles)) {
  220.             $this->roles[] = $role;
  221.         }
  222.         return $this;
  223.     }
  224.     /**
  225.      * @see UserInterface
  226.      */
  227.     public function removeRole(string $role): self
  228.     {
  229.         if (in_array($role$this->roles)) {
  230.             foreach ($this->roles as $key => $item) {
  231.                 if ($item === $role) {
  232.                     unset($this->roles[$key]);
  233.                 }
  234.             }
  235.             $this->roles array_values($this->roles);
  236.         }
  237.         return $this;
  238.     }
  239.     public function setRoles(array $roles): self
  240.     {
  241.         $this->roles $roles;
  242.         return $this;
  243.     }
  244.     /**
  245.      * @see PasswordAuthenticatedUserInterface
  246.      */
  247.     public function getPassword(): ?string
  248.     {
  249.         return $this->password;
  250.     }
  251.     /**
  252.      * This setter also checks if the password has changed it will rehash it
  253.      * @param string|null $password New password
  254.      * @param bool $hash Forces rehash of password
  255.      * @return $this
  256.      */
  257.     public function setPassword(?string $passwordbool $hash false): self
  258.     {
  259.         if (!empty($password) && ($this->password !== $password || $hash)) {
  260.             $this->_hashPwd true;
  261.         }
  262.         if (!empty($password)){
  263.             $this->password $password;
  264.         }
  265.         return $this;
  266.     }
  267.     /**
  268.      * Returning a salt is only needed, if you are not using a modern
  269.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  270.      *
  271.      * @see UserInterface
  272.      */
  273.     public function getSalt(): ?string
  274.     {
  275.         return null;
  276.     }
  277.     /**
  278.      * @see UserInterface
  279.      */
  280.     public function eraseCredentials()
  281.     {
  282.         // If you store any temporary, sensitive data on the user, clear it here
  283.         // $this->plainPassword = null;
  284.     }
  285.     public function getCreatedAt(): ?\DateTime
  286.     {
  287.         return $this->createdAt;
  288.     }
  289.     public function setCreatedAt(\DateTime $createdAt): self
  290.     {
  291.         $this->createdAt $createdAt;
  292.         return $this;
  293.     }
  294.     public function getLastLogin(): ?\DateTimeInterface
  295.     {
  296.         return $this->lastLogin;
  297.     }
  298.     public function setLastLogin(?\DateTimeInterface $lastLogin): self
  299.     {
  300.         $this->lastLogin $lastLogin;
  301.         return $this;
  302.     }
  303.     public function getName(): ?string
  304.     {
  305.         return $this->name;
  306.     }
  307.     public function setName(?string $name): self
  308.     {
  309.         $this->name $name;
  310.         return $this;
  311.     }
  312.     public function getCategory(): ?string
  313.     {
  314.         return $this->category;
  315.     }
  316.     public function setCategory(string $category): self
  317.     {
  318.         $this->category $category;
  319.         return $this;
  320.     }
  321.     public function getActive(): ?bool
  322.     {
  323.         return $this->active;
  324.     }
  325.     public function setActive(bool $active): self
  326.     {
  327.         $this->active $active;
  328.         return $this;
  329.     }
  330.     public function getBanStart(): ?\DateTimeInterface
  331.     {
  332.         return $this->banStart;
  333.     }
  334.     public function setBanStart(?\DateTimeInterface $banStart): self
  335.     {
  336.         $this->banStart $banStart;
  337.         return $this;
  338.     }
  339.     public function getBanEnd(): ?\DateTimeInterface
  340.     {
  341.         return $this->banEnd;
  342.     }
  343.     public function setBanEnd(?\DateTimeInterface $banEnd): self
  344.     {
  345.         $this->banEnd $banEnd;
  346.         return $this;
  347.     }
  348.     public function getResetToken(): ?string
  349.     {
  350.         return $this->resetToken;
  351.     }
  352.     public function setResetToken(?string $resetToken): self
  353.     {
  354.         $this->resetToken $resetToken;
  355.         return $this;
  356.     }
  357.     public function getSpecialist(): ?Specialist
  358.     {
  359.         return $this->specialist;
  360.     }
  361.     public function setSpecialist(Specialist $specialist): self
  362.     {
  363.         // set the owning side of the relation if necessary
  364.         if ($specialist->getUser() !== $this) {
  365.             $specialist->setUser($this);
  366.         }
  367.         $this->specialist $specialist;
  368.         return $this;
  369.     }
  370.     public function getCompany(): ?Company
  371.     {
  372.         return $this->company;
  373.     }
  374.     public function setCompany(Company $company): self
  375.     {
  376.         $this->company $company;
  377.         return $this;
  378.     }
  379.     public function getClient(): ?Client
  380.     {
  381.         return $this->client;
  382.     }
  383.     public function setClient(Client $client): self
  384.     {
  385.         // set the owning side of the relation if necessary
  386.         if ($client->getUser() !== $this) {
  387.             $client->setUser($this);
  388.         }
  389.         $this->client $client;
  390.         return $this;
  391.     }
  392.     /**
  393.      * @return bool
  394.      */
  395.     public function isHashPwd(): bool
  396.     {
  397.         return $this->_hashPwd;
  398.     }
  399.     /**
  400.      * @return string
  401.      */
  402.     public function __toString()
  403.     {
  404.         return $this->name;
  405.     }
  406.     /**
  407.      * @ORM\PrePersist()
  408.      */
  409.     public function updateName()
  410.     {
  411.         if ($this->company instanceof Company) {
  412.             $this->name $this->company->getName();
  413.         } elseif ($this->specialist instanceof Specialist) {
  414.             $this->name = (string)$this->specialist;
  415.         } elseif ($this->client instanceof Client) {
  416.             $this->name = (string)$this->client;
  417.         }
  418.     }
  419.     /**
  420.      * @param string|null $token
  421.      * @return User
  422.      */
  423.     public function setToken(?string $token): User
  424.     {
  425.         $this->token $token;
  426.         return $this;
  427.     }
  428.     /**
  429.      * @return string|null
  430.      */
  431.     public function getToken(): ?string
  432.     {
  433.         return $this->token;
  434.     }
  435.     public function setSamlAttributes(array $attributes)
  436.     {
  437.         $this->token $attributes['sessionIndex'];
  438.         $data = [];
  439.         foreach ($attributes as $key => $value) {
  440.             $data[ToolsService::toCamelCase($key)] = $value;
  441.         }
  442.         if (!empty($data['email'])) {
  443.             if(is_array($data['email'])) $this->email $data['email'][0];
  444.             elseif (is_string($data['email'])) $this->email $data['email'];
  445.         }
  446.         if (!empty($data['firstName'])) {
  447.             if(is_array($data['firstName'])) $this->firstName $data['firstName'][0];
  448.             elseif (is_string($data['firstName'])) $this->firstName $data['firstName'];
  449.         }
  450.         if (!empty($data['lastName'])) {
  451.             if(is_array($data['lastName'])) $this->lastName $data['lastName'][0];
  452.             elseif (is_string($data['lastName'])) $this->lastName $data['lastName'];
  453.         }
  454.         if (!empty($data['country'])) {
  455.             if(is_array($data['country'])) $this->country $data['country'][0];
  456.             elseif (is_string($data['country'])) $this->country $data['country'];
  457.         }
  458.     }
  459.     /**
  460.      * @return Collection<int, Document>
  461.      */
  462.     public function getDocuments(): Collection
  463.     {
  464.         return $this->documents;
  465.     }
  466.     public function addDocument(Document $document): self
  467.     {
  468.         if (!$this->documents->contains($document)) {
  469.             $this->documents[] = $document;
  470.             $document->setOwner($this);
  471.         }
  472.         return $this;
  473.     }
  474.     public function removeDocument(Document $document): self
  475.     {
  476.         if ($this->documents->removeElement($document)) {
  477.             // set the owning side to null (unless already changed)
  478.             if ($document->getOwner() === $this) {
  479.                 $document->setOwner(null);
  480.             }
  481.         }
  482.         return $this;
  483.     }
  484.     /**
  485.      * @return Collection<int, MarketplaceReservation>
  486.      */
  487.     public function getMarketplaceReservations(): Collection
  488.     {
  489.         return $this->marketplaceReservations;
  490.     }
  491.     public function addMarketplaceReservation(MarketplaceReservation $marketplaceReservation): self
  492.     {
  493.         if (!$this->marketplaceReservations->contains($marketplaceReservation)) {
  494.             $this->marketplaceReservations[] = $marketplaceReservation;
  495.             $marketplaceReservation->setUser($this);
  496.         }
  497.         return $this;
  498.     }
  499.     public function removeMarketplaceReservation(MarketplaceReservation $marketplaceReservation): self
  500.     {
  501.         if ($this->marketplaceReservations->removeElement($marketplaceReservation)) {
  502.             // set the owning side to null (unless already changed)
  503.             if ($marketplaceReservation->getUser() === $this) {
  504.                 $marketplaceReservation->setUser(null);
  505.             }
  506.         }
  507.         return $this;
  508.     }
  509.     public function getFirstName(): ?string
  510.     {
  511.         return $this->firstName;
  512.     }
  513.     public function setFirstName(?string $firstName): self
  514.     {
  515.         $this->firstName $firstName;
  516.         return $this;
  517.     }
  518.     public function getLastName(): ?string
  519.     {
  520.         return $this->lastName;
  521.     }
  522.     public function setLastName(?string $lastName): self
  523.     {
  524.         $this->lastName $lastName;
  525.         return $this;
  526.     }
  527.     /**
  528.      * @return Collection<int, UserFavorite>
  529.      */
  530.     public function getFavorites(): Collection
  531.     {
  532.         return $this->favorites;
  533.     }
  534.     public function addFavorite(UserFavorite $favorite): self
  535.     {
  536.         if (!$this->favorites->contains($favorite)) {
  537.             $this->favorites[] = $favorite;
  538.             $favorite->setUser($this);
  539.         }
  540.         return $this;
  541.     }
  542.     public function removeFavorite(UserFavorite $favorite): self
  543.     {
  544.         if ($this->favorites->removeElement($favorite)) {
  545.             // set the owning side to null (unless already changed)
  546.             if ($favorite->getUser() === $this) {
  547.                 $favorite->setUser(null);
  548.             }
  549.         }
  550.         return $this;
  551.     }
  552.     public function getSegmentation(): ?Segmentation
  553.     {
  554.         return $this->segmentation;
  555.     }
  556.     public function setSegmentation(?Segmentation $segmentation): self
  557.     {
  558.         $this->segmentation $segmentation;
  559.         return $this;
  560.     }
  561.     public function getCountry(): ?string
  562.     {
  563.         return $this->country;
  564.     }
  565.     public function setCountry(?string $country): self
  566.     {
  567.         $this->country $country;
  568.         return $this;
  569.     }
  570. }