src/Entity/User.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use ApiPlatform\Core\Annotation\ApiSubresource;
  5. use ApiPlatform\Core\Annotation\ApiProperty;
  6. use App\Repository\UserRepository;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. use Symfony\Component\Uid\Ulid;
  16. use Symfony\Component\Serializer\Annotation\MaxDepth;
  17. #[ORM\Entity(repositoryClassUserRepository::class)]
  18. #[UniqueEntity(fields"email"message"Un compte existe déjà avec cette boîte mail." )]
  19. #[ApiResource(
  20.     normalizationContext: ['groups' => ['user_read'], 'enable_max_depth' => true ],
  21.     denormalizationContext: ['groups' => ['user_write']],
  22.     collectionOperations: [
  23.     "get" => ["security" => "is_granted('ROLE_ADMIN')"],
  24.     "post" => ["security" => "is_granted('ROLE_ADMIN')"]
  25.     ],
  26.     itemOperations: [ 
  27.         "get" => ["security" => "is_granted('ROLE_ADMIN') or object.id == user.id"],
  28.         "patch" => ["security" => "is_granted('ROLE_ADMIN') or object.id == user.id"],
  29.     ],
  30.     attributes: ["pagination_client_items_per_page" => true],
  31.     forceEagerfalse
  32. )]
  33. class User implements UserInterfacePasswordAuthenticatedUserInterface
  34. {
  35.     #[ORM\Id]
  36.     #[ORM\GeneratedValue]
  37.     #[ORM\Column(type'integer')]
  38.     #[ApiProperty(identifierfalse)]
  39.     #[Groups(['user_read'])]
  40.     public $id;
  41.     #[ORM\GeneratedValue]
  42.     #[ORM\Column(type'string'length26uniquetrue)]
  43.     #[ApiProperty(identifiertrue)]
  44.     #[Groups(['user_read'])]
  45.     public $ulid "";
  46.     #[Assert\Email()]
  47.     #[ORM\Column(type'string'length255uniquetrue)]
  48.     #[Groups(['user_read','user_write'])]
  49.     private $email;
  50.     #[Groups(['user_read','user_write'])]
  51.     #[ORM\Column(type'string'length255nullabletrue)]
  52.     private $firstname;
  53.     #[Groups(['user_read','user_write'])]
  54.     #[ORM\Column(type'string'length255nullabletrue)]
  55.     private $lastname;
  56.     #[ORM\Column(type'string'nullabletrue)]
  57.     #[Groups(['user_write'])]
  58.     private $password;
  59.     #[ORM\Column(type'json')]
  60.     #[Groups(['user_read','user_write'])]
  61.     private $roles = [];
  62.     #[ORM\Column(type'string'length255nullabletrue)]
  63.     #[Groups(['user_read','user_write'])]
  64.     private $phoneNumber;
  65.     #[ORM\ManyToOne(targetEntityRole::class, inversedBy'users')]
  66.     #[ORM\JoinColumn(nullabletrue)]
  67.     #[Groups(['user_read','user_write'])]
  68.     private $role;
  69.     #[ORM\ManyToMany(targetEntityGroup::class, inversedBy'users')]
  70.     #[Groups(['user_read'])]
  71.     #[ApiSubresource()]
  72.     private $groups;
  73.     #[ORM\OneToMany(mappedBy'creator'targetEntityAccomodation::class, orphanRemovaltrue)]
  74.     #[Groups(['user_read'])]
  75.     #[ApiSubresource()]
  76.     private $accomodation;
  77.     public function __construct()
  78.     {
  79.         $this->groups = new ArrayCollection();
  80.         $this->accomodation = new ArrayCollection();
  81.     }
  82.     public function __toString(): string
  83.     {
  84.         return $this->email;
  85.     }
  86.     public function getId(): ?int
  87.     {
  88.         return $this->id;
  89.     }
  90.     public function getEmail(): ?string
  91.     {
  92.         return $this->email;
  93.     }
  94.     public function setEmail(string $email): self
  95.     {
  96.         $this->email $email;
  97.         return $this;
  98.     }
  99.     /**
  100.      * A visual identifier that represents this user.
  101.      *
  102.      * @see UserInterface
  103.      */
  104.     public function getUserIdentifier(): string
  105.     {
  106.         return (string) $this->email;
  107.     }
  108.     /**
  109.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  110.      */
  111.     public function getUsername(): string
  112.     {
  113.         return (string) $this->email;
  114.     }
  115.     /**
  116.      * @see UserInterface
  117.      */
  118.     public function getRoles(): array
  119.     {
  120.         $roles $this->roles;
  121.         // guarantee every user at least has ROLE_USER
  122.         $roles[] = 'ROLE_USER';
  123.         return array_unique($roles);
  124.     }
  125.     public function setRoles(array $roles): self
  126.     {
  127.         $this->roles $roles;
  128.         return $this;
  129.     }
  130.     /**
  131.      * @see PasswordAuthenticatedUserInterface
  132.      */
  133.     public function getPassword(): string
  134.     {
  135.         return $this->password;
  136.     }
  137.     public function setPassword(string $password): self
  138.     {
  139.         $this->password $password;
  140.         return $this;
  141.     }
  142.     public function getUlid()
  143.     {
  144.         return $this->ulid;
  145.     }
  146.      public function setUlid(): self
  147.     {
  148.         $ulid = new Ulid();
  149.         $this->ulid $ulid;
  150.         return $this;
  151.     }
  152.      public function getFirstname(): ?string
  153.      {
  154.          return $this->firstname;
  155.      }
  156.      public function setFirstname(?string $firstname): self
  157.      {
  158.          $this->firstname $firstname;
  159.          return $this;
  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.      
  171.     /**
  172.      * Returning a salt is only needed, if you are not using a modern
  173.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  174.      *
  175.      * @see UserInterface
  176.      */
  177.     public function getSalt(): ?string
  178.     {
  179.         return null;
  180.     }
  181.     /**
  182.      * @see UserInterface
  183.      */
  184.     public function eraseCredentials()
  185.     {
  186.         // If you store any temporary, sensitive data on the user, clear it here
  187.         // $this->plainPassword = null;
  188.     }
  189.     /**
  190.      * @return Collection|Qrcode[]
  191.      */
  192.     public function getQrcodes(): Collection
  193.     {
  194.         return $this->qrcodes;
  195.     }
  196.     public function getIsVerified(): bool
  197.     {
  198.         return $this->isVerified;
  199.     }
  200.     public function setIsVerified(bool $isVerified): self
  201.     {
  202.         $this->isVerified $isVerified;
  203.         return $this;
  204.     }
  205.     public function getPhoneNumber(): ?string
  206.     {
  207.         return $this->phoneNumber;
  208.     }
  209.     public function setPhoneNumber(string $phoneNumber): self
  210.     {
  211.         $this->phoneNumber $phoneNumber;
  212.         return $this;
  213.     }
  214.     public function getRole(): ?Role
  215.     {
  216.         return $this->role;
  217.     }
  218.     public function setRole(?Role $role): self
  219.     {
  220.         $this->role $role;
  221.         return $this;
  222.     }
  223.     /**
  224.      * @return Collection<int, Group>
  225.      */
  226.     public function getGroups(): Collection
  227.     {
  228.         return $this->groups;
  229.     }
  230.     public function addGroup(Group $group): self
  231.     {
  232.         if (!$this->groups->contains($group)) {
  233.             $this->groups[] = $group;
  234.         }
  235.         return $this;
  236.     }
  237.     public function removeGroup(Group $group): self
  238.     {
  239.         $this->groups->removeElement($group);
  240.         return $this;
  241.     }
  242.     /**
  243.      * @return Collection<int, Accomodation>
  244.      */
  245.     public function getAccomodation(): Collection
  246.     {
  247.         return $this->accomodation;
  248.     }
  249.     public function addAccomodation(Accomodation $accomodation): self
  250.     {
  251.         if (!$this->accomodation->contains($accomodation)) {
  252.             $this->accomodation[] = $accomodation;
  253.             $accomodation->setCreator($this);
  254.         }
  255.         return $this;
  256.     }
  257.     public function removeAccomodation(Accomodation $accomodation): self
  258.     {
  259.         if ($this->accomodation->removeElement($accomodation)) {
  260.             // set the owning side to null (unless already changed)
  261.             if ($accomodation->getCreator() === $this) {
  262.                 $accomodation->setCreator(null);
  263.             }
  264.         }
  265.         return $this;
  266.     }
  267. }