src/Entity/User.php line 36

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use ApiPlatform\Metadata\Get;
  5. use ApiPlatform\Metadata\GetCollection;
  6. use ApiPlatform\Metadata\Post;
  7. use ApiPlatform\Metadata\Put;
  8. use ApiPlatform\Metadata\Patch;
  9. use App\Repository\UserRepository;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  14. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  15. use Symfony\Component\Security\Core\User\UserInterface;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. #[ORM\Entity(repositoryClassUserRepository::class)]
  18. #[ApiResource(
  19.     normalizationContext: ['groups' => ['read:user']],
  20.     denormalizationContext: ['groups' => ['write:user']],
  21.     operations: [
  22.         new Get(),
  23.         new GetCollection(),
  24.         new Post(),
  25.         new Put(),
  26.         new Patch(),
  27.     ],
  28.     paginationEnabledtrue,
  29.     paginationItemsPerPage30
  30. )]
  31. #[ORM\Table(name'`user`')]
  32. #[UniqueEntity(fields: ['email'], message'There is already an account with this email')]
  33. class User implements UserInterfacePasswordAuthenticatedUserInterface
  34. {
  35.     #[ORM\Id]
  36.     #[ORM\GeneratedValue]
  37.     #[ORM\Column]
  38.     #[Groups(['read:user','write:user'])]
  39.     private ?int $id null;
  40.     
  41.     #[Groups(['read:user','write:user'])]
  42.     #[ORM\Column(length180uniquetrue)]
  43.     private ?string $email null;
  44.     #[ORM\Column]
  45.     #[Groups(['read:user','write:user'])]
  46.     private array $roles = [];
  47.     /**
  48.      * @var string The hashed password
  49.      */
  50.     #[ORM\Column]
  51.     #[Groups(['write:user'])] // Ne jamais exposer le mot de passe en lecture
  52.     private ?string $password null;
  53.     #[ORM\Column]
  54.     #[Groups(['read:user','write:user'])]
  55.     private ?bool $isdelete false;
  56.     #[ORM\OneToMany(mappedBy'user'targetEntityPersonne::class)]
  57.     #[Groups(['read:user','read:personne'])]
  58.     private Collection $personnes;
  59.     #[ORM\Column]
  60.     #[Groups(['read:user','read:personne','write:user',])]
  61.     private ?bool $oneConnexion =false;
  62.     #[ORM\OneToMany(mappedBy'client'targetEntityAcheter::class)]
  63.     private Collection $acheters;
  64.     #[ORM\Column(length255nullabletrue)]
  65.     private ?string $log null;
  66.     public function __construct()
  67.     {
  68.         $this->personnes = new ArrayCollection();
  69.         $this->acheters = new ArrayCollection();
  70.     }
  71.     public function getId(): ?int
  72.     {
  73.         return $this->id;
  74.     }
  75.     public function getEmail(): ?string
  76.     {
  77.         return $this->email;
  78.     }
  79.     public function setEmail(string $email): self
  80.     {
  81.         $this->email $email;
  82.         return $this;
  83.     }
  84.     /**
  85.      * A visual identifier that represents this user.
  86.      *
  87.      * @see UserInterface
  88.      */
  89.     public function getUserIdentifier(): string
  90.     {
  91.         return (string) $this->email;
  92.     }
  93.     /**
  94.      * @see UserInterface
  95.      */
  96.     public function getRoles(): array
  97.     {
  98.         $roles $this->roles;
  99.         // guarantee every user at least has ROLE_USER
  100.         $roles[] = 'ROLE_USER';
  101.         return array_unique($roles);
  102.     }
  103.     public function setRoles(array $roles): self
  104.     {
  105.         $this->roles $roles;
  106.         return $this;
  107.     }
  108.     /**
  109.      * @see PasswordAuthenticatedUserInterface
  110.      */
  111.     public function getPassword(): string
  112.     {
  113.         return $this->password;
  114.     }
  115.     public function setPassword(string $password): self
  116.     {
  117.         $this->password $password;
  118.         return $this;
  119.     }
  120.     /**
  121.      * @see UserInterface
  122.      */
  123.     public function eraseCredentials()
  124.     {
  125.         // If you store any temporary, sensitive data on the user, clear it here
  126.         // $this->plainPassword = null;
  127.     }
  128.     
  129.     public function isIsdelete(): ?bool
  130.     {
  131.         return $this->isdelete;
  132.     }
  133.     public function setIsdelete(bool $isdelete): self
  134.     {
  135.         $this->isdelete $isdelete;
  136.         return $this;
  137.     }
  138.     /**
  139.      * @return Collection<int, Personne>
  140.      */
  141.     public function getPersonnes(): Collection
  142.     {
  143.         return $this->personnes;
  144.     }
  145.     public function addPersonne(Personne $personne): self
  146.     {
  147.         if (!$this->personnes->contains($personne)) {
  148.             $this->personnes->add($personne);
  149.             $personne->setUser($this);
  150.         }
  151.         return $this;
  152.     }
  153.     public function removePersonne(Personne $personne): self
  154.     {
  155.         if ($this->personnes->removeElement($personne)) {
  156.             // set the owning side to null (unless already changed)
  157.             if ($personne->getUser() === $this) {
  158.                 $personne->setUser(null);
  159.             }
  160.         }
  161.         return $this;
  162.     }
  163.     public function isOneConnexion(): ?bool
  164.     {
  165.         return $this->oneConnexion;
  166.     }
  167.     public function setOneConnexion(bool $oneConnexion): self
  168.     {
  169.         $this->oneConnexion $oneConnexion;
  170.         return $this;
  171.     }
  172.     /**
  173.      * @return Collection<int, Acheter>
  174.      */
  175.     public function getAcheters(): Collection
  176.     {
  177.         return $this->acheters;
  178.     }
  179.     public function addAcheter(Acheter $acheter): self
  180.     {
  181.         if (!$this->acheters->contains($acheter)) {
  182.             $this->acheters->add($acheter);
  183.             $acheter->setClient($this);
  184.         }
  185.         return $this;
  186.     }
  187.     public function removeAcheter(Acheter $acheter): self
  188.     {
  189.         if ($this->acheters->removeElement($acheter)) {
  190.             // set the owning side to null (unless already changed)
  191.             if ($acheter->getClient() === $this) {
  192.                 $acheter->setClient(null);
  193.             }
  194.         }
  195.         return $this;
  196.     }
  197.     public function getLog(): ?string
  198.     {
  199.         return $this->log;
  200.     }
  201.     public function setLog(?string $log): static
  202.     {
  203.         $this->log $log;
  204.         return $this;
  205.     }
  206.     public function __toString(): string
  207.     {
  208.         $personne $this->getPersonnes()->first();
  209.         if ($personne) {
  210.             return sprintf('%s %s'$personne->getNom(), $personne->getPrenom());
  211.         }
  212.         return $this->email ?? 'User #' $this->id;
  213.     }
  214. }