src/Entity/User.php line 44

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use ApiPlatform\Metadata\Get;
  5. use ApiPlatform\Metadata\GetCollection;
  6. use App\EntityListener\UserListener;
  7. use DateTimeImmutable;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\DBAL\Types\Types;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use App\Repository\UserRepository;
  13. use Serializable;
  14. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  15. use Symfony\Component\HttpFoundation\File\File;
  16. use Symfony\Component\Security\Core\User\UserInterface;
  17. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  18. use Symfony\Component\Serializer\Annotation\Groups;
  19. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  20. #[ORM\Entity(repositoryClassUserRepository::class)]
  21. #[ORM\Table(name'`app_user`')]
  22. #[ORM\HasLifecycleCallbacks]
  23. #[Vich\Uploadable]
  24. #[UniqueEntity(fields: ['username'], message'Il existe déjà un compte avec cet identifiant.')]
  25. #[ORM\EntityListeners([UserListener::class])]
  26. #[ApiResource(
  27.     operations: [
  28.         new Get(
  29.             normalizationContext: [
  30.                 'groups' => 'user:item'
  31.             ]
  32.         ),
  33.         new GetCollection(
  34.             normalizationContext: [
  35.                 'groups' => 'user:list'
  36.             ]
  37.         ),
  38.     ],
  39.     paginationEnabledfalse,
  40. )]
  41. class User implements UserInterfacePasswordAuthenticatedUserInterfaceSerializable
  42. {
  43.     #[ORM\Id]
  44.     #[ORM\GeneratedValue]
  45.     #[ORM\Column]
  46.     #[Groups(['user:list''user:item''assignment:item''assignment:list'])]
  47.     private ?int $id null;
  48.     #[ORM\Column(name'`user_login`'length255uniquetrue)]
  49.     #[Groups(['user:list''user:item'])]
  50.     private ?string $username null;
  51.     #[ORM\Column(name'`display_name`'length255nullabletrue)]
  52.     #[Groups(['user:list''user:item''assignment:item'])]
  53.     private ?string $displayName null;
  54.     #[ORM\ManyToOne(inversedBy'users')]
  55.     #[ORM\JoinColumn(nullabletrue)]
  56.     #[Groups(['user:list''user:item'])]
  57.     private ?Role $role null;
  58.     #[ORM\Column(typeTypes::BOOLEANnullabletrueoptions: [
  59.         "default" => 0
  60.     ])]
  61.     #[Groups(['user:list''user:item'])]
  62.     private ?bool $isClient null;
  63.     #[ORM\ManyToOne(inversedBy'users')]
  64.     #[ORM\JoinColumn(nullabletrue)]
  65.     #[Groups(['user:list''user:item'])]
  66.     private ?Client $client null;
  67.     #[ORM\Column(name'`user_pass`')]
  68.     #[Groups(['user:list''user:item'])]
  69.     private ?string $password null;
  70.     #[ORM\Column(name'`user_email`'length255)]
  71.     #[Groups(['user:list''user:item'])]
  72.     private ?string $email null;
  73.     #[ORM\Column(length255nullabletrue)]
  74.     #[Groups(['user:list''user:item'])]
  75.     private ?string $phone null;
  76.     #[ORM\Column(typeTypes::STRINGlength255nullabletrueoptions: [
  77.         "default" => null
  78.     ])]
  79.     #[Groups(['user:list''user:item'])]
  80.     private ?string $imageName null;
  81.     #[Vich\UploadableField(mapping'user_img'fileNameProperty'imageName'size'imageSize')]
  82.     private ?File $imageFile null;
  83.     #[ORM\Column(type'integer'nullabletrue)]
  84.     private ?int $imageSize null;
  85.     #[ORM\Column(typeTypes::BOOLEANoptions: ["default" => "0"])]
  86.     private bool $isVerified false;
  87.     #[ORM\Column(typeTypes::DATETIME_IMMUTABLEoptions: [
  88.         "default" => "CURRENT_TIMESTAMP"
  89.     ])]
  90.     private ?DateTimeImmutable $createdAt null;
  91.     #[ORM\Column(typeTypes::DATETIME_IMMUTABLEoptions: [
  92.         "default" => "CURRENT_TIMESTAMP"
  93.     ])]
  94.     private ?DateTimeImmutable $updatedAt null;
  95.     #[ORM\Column(typeTypes::BOOLEANoptions: ["default" => "0"])]
  96.     #[Groups(['user:list''user:item'])]
  97.     private ?bool $isActivated false;
  98.     #[ORM\Column(typeTypes::BOOLEANoptions: ["default" => "0"])]
  99.     #[Groups(['user:list''user:item'])]
  100.     private ?bool $isDeleted false;
  101.     #[ORM\OneToMany(mappedBy'user'targetEntityCard::class)]
  102.     private Collection $cards;
  103.     public function __construct()
  104.     {
  105.         $this->cards = new ArrayCollection();
  106.     }
  107.     public function getId(): ?int
  108.     {
  109.         return $this->id;
  110.     }
  111.     /**
  112.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  113.      */
  114.     public function getUsername(): ?string
  115.     {
  116.         return (string)$this->username;
  117.     }
  118.     public function setUsername(?string $username): self
  119.     {
  120.         $this->username $username;
  121.         return $this;
  122.     }
  123.     /**
  124.      * A visual identifier that represents this user.
  125.      *
  126.      * @see UserInterface
  127.      */
  128.     public function getUserIdentifier(): string
  129.     {
  130.         return (string)$this->username;
  131.     }
  132.     /**
  133.      * @see PasswordAuthenticatedUserInterface
  134.      */
  135.     public function getPassword(): string
  136.     {
  137.         if ($this->password != null)
  138.             return $this->password;
  139.         return "";
  140.     }
  141.     /**
  142.      * @param string|null $password
  143.      * @return $this
  144.      */
  145.     public function setPassword(?string $password): self
  146.     {
  147.         $this->password $password;
  148.         return $this;
  149.     }
  150.     /**
  151.      * Returning a salt is only needed, if you are not using a modern
  152.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  153.      *
  154.      * @see UserInterface
  155.      */
  156.     public function getSalt(): ?string
  157.     {
  158.         return null;
  159.     }
  160.     /**
  161.      * @see UserInterface
  162.      */
  163.     public function eraseCredentials()
  164.     {
  165.         // If you store any temporary, sensitive data on the user, clear it here
  166.         //$this->plainPassword = null;
  167.     }
  168.     public function getDisplayName(): ?string
  169.     {
  170.         return $this->displayName;
  171.     }
  172.     public function setDisplayName(?string $displayName): self
  173.     {
  174.         $this->displayName $displayName;
  175.         return $this;
  176.     }
  177.     public function getEmail(): ?string
  178.     {
  179.         return $this->email;
  180.     }
  181.     public function setEmail(?string $email): self
  182.     {
  183.         $this->email $email;
  184.         return $this;
  185.     }
  186.     public function getPhone(): ?string
  187.     {
  188.         return $this->phone;
  189.     }
  190.     public function setPhone(?string $phone): self
  191.     {
  192.         $this->phone $phone;
  193.         return $this;
  194.     }
  195.     /**
  196.      * @return string|null
  197.      */
  198.     public function getImageName(): ?string
  199.     {
  200.         return $this->imageName;
  201.     }
  202.     /**
  203.      * @param string|null $imageName
  204.      */
  205.     public function setImageName(?string $imageName): void
  206.     {
  207.         $this->imageName $imageName;
  208.     }
  209.     /**
  210.      * @param File|null $imageFile
  211.      */
  212.     public function setImageFile(?File $imageFile null): void
  213.     {
  214.         $this->imageFile $imageFile;
  215.         if (null !== $imageFile) {
  216.             $this->updatedAt = new DateTimeImmutable;
  217.         }
  218.     }
  219.     public function getImageFile(): ?File
  220.     {
  221.         return $this->imageFile;
  222.     }
  223.     public function setImageSize(?int $imageSize): void
  224.     {
  225.         $this->imageSize $imageSize;
  226.     }
  227.     public function getImageSize(): ?int
  228.     {
  229.         return $this->imageSize;
  230.     }
  231.     public function isVerified(): bool
  232.     {
  233.         return $this->isVerified;
  234.     }
  235.     public function setIsVerified(?bool $isVerified): self
  236.     {
  237.         $this->isVerified $isVerified;
  238.         return $this;
  239.     }
  240.     public function getCreatedAt(): ?DateTimeImmutable
  241.     {
  242.         return $this->createdAt;
  243.     }
  244.     public function setCreatedAt(?DateTimeImmutable $createdAt): self
  245.     {
  246.         $this->createdAt $createdAt;
  247.         return $this;
  248.     }
  249.     public function getUpdatedAt(): ?DateTimeImmutable
  250.     {
  251.         return $this->updatedAt;
  252.     }
  253.     public function setUpdatedAt(?DateTimeImmutable $updatedAt): self
  254.     {
  255.         $this->updatedAt $updatedAt;
  256.         return $this;
  257.     }
  258.     public function isIsActivated(): ?bool
  259.     {
  260.         return $this->isActivated;
  261.     }
  262.     public function setIsActivated(bool $isActivated): self
  263.     {
  264.         $this->isActivated $isActivated;
  265.         return $this;
  266.     }
  267.     public function isIsDeleted(): ?bool
  268.     {
  269.         return $this->isDeleted;
  270.     }
  271.     public function setIsDeleted(bool $isDeleted): self
  272.     {
  273.         $this->isDeleted $isDeleted;
  274.         return $this;
  275.     }
  276.     public function getRole(): ?Role
  277.     {
  278.         return $this->role;
  279.     }
  280.     public function setRole(?Role $role): self
  281.     {
  282.         $this->role $role;
  283.         return $this;
  284.     }
  285.     public function getRoles(): array
  286.     {
  287.         $roles = [];
  288.         $roles[] = 'ROLE_USER';
  289.         if ($this->getRole() != null)
  290.             $roles[] = $this->getRole()->getAlias();
  291.         return array_unique($roles);
  292.     }
  293.     /** @see \Serializable::serialize() */
  294.     public function serialize(): ?string
  295.     {
  296.         return serialize(array(
  297.             $this->id,
  298.             $this->username,
  299.             $this->displayName,
  300.             $this->password,
  301.             $this->email,
  302.             $this->phone,
  303.             $this->isVerified,
  304.             $this->isActivated,
  305.         ));
  306.     }
  307.     /** @param $serialized
  308.      * @see \Serializable::unserialize()
  309.      */
  310.     public function unserialize($serialized)
  311.     {
  312.         list (
  313.             $this->id,
  314.             $this->username,
  315.             $this->displayName,
  316.             $this->password,
  317.             $this->email,
  318.             $this->phone,
  319.             $this->isVerified,
  320.             $this->isActivated,
  321.             ) = unserialize($serialized, array('allowed_classes' => false));
  322.     }
  323.     public function getClient(): ?Client
  324.     {
  325.         return $this->client;
  326.     }
  327.     public function setClient(?Client $client): self
  328.     {
  329.         $this->client $client;
  330.         return $this;
  331.     }
  332.     /**
  333.      * @return Collection<int, Card>
  334.      */
  335.     public function getCards(): Collection
  336.     {
  337.         return $this->cards;
  338.     }
  339.     public function addCard(Card $card): self
  340.     {
  341.         if (!$this->cards->contains($card)) {
  342.             $this->cards->add($card);
  343.             $card->setUser($this);
  344.         }
  345.         return $this;
  346.     }
  347.     public function removeCard(Card $card): self
  348.     {
  349.         if ($this->cards->removeElement($card)) {
  350.             // set the owning side to null (unless already changed)
  351.             if ($card->getUser() === $this) {
  352.                 $card->setUser(null);
  353.             }
  354.         }
  355.         return $this;
  356.     }
  357.     public function isIsClient(): ?bool
  358.     {
  359.         return $this->isClient;
  360.     }
  361.     public function setIsClient(?bool $isClient): self
  362.     {
  363.         $this->isClient $isClient;
  364.         return $this;
  365.     }
  366. }