src/Entity/User.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Traits\TimestampableTrait;
  4. use App\Repository\UserRepository;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  7. use Symfony\Component\HttpFoundation\File\File;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use ApiPlatform\Core\Annotation\ApiResource;
  10. use Symfony\Component\Serializer\Annotation\Groups;
  11. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. /**
  15.  * @Vich\Uploadable
  16.  * @ApiResource(
  17.  *     normalizationContext={"groups"={"user:read"}},
  18.  *     denormalizationContext={"groups"={"user:write"}},
  19.  *     collectionOperations={
  20.  *         "get"={},
  21.  *         "post"={
  22.  *             "denormalization_context"={"groups"={"user:write"}}
  23.  *         }
  24.  *     },
  25.  *     itemOperations={
  26.  *       "get"={},
  27.  *       "put"={},
  28.  *       "delete"={}
  29.  *    }
  30.  * )
  31.  */
  32. #[ORM\Table(name'user')]
  33. #[ORM\Entity(repositoryClassUserRepository::class)]
  34. #[UniqueEntity(fields: ['email'], message'Il existe déjà un compte avec cet e-mail')]
  35. class User implements UserInterface\Serializable
  36. {
  37.     use TimestampableTrait;
  38.     #[ORM\Id]
  39.     #[ORM\GeneratedValue]
  40.     #[ORM\Column(type'integer')]
  41.     #[Groups(['user:read'])]
  42.     private $id;
  43.     #[ORM\Column(type'string'length180uniquetrue)]
  44.     #[Groups(['user:read''user:write'])]
  45.     private $email;
  46.     #[ORM\Column(type'string'length255)]
  47.     #[Groups(['user:read''user:write'])]
  48.     private $firstname;
  49.     #[ORM\Column(type'json')]
  50.     #[Groups(['user:read''user:write'])]
  51.     private $roles = [];
  52.     /**
  53.      * @var string The hashed password
  54.      */
  55.     #[ORM\Column(type'string')]
  56.     private $password;
  57.     /**
  58.      * Utiliser pour changer de mot de passe.
  59.      */
  60.     #[Groups(['user:read''user:write'])]
  61.     private $plainPassword null;
  62.     #[ORM\ManyToOne(targetEntityAccountingFirm::class, inversedBy'users'fetch'EAGER')]
  63.     #[ORM\JoinColumn(nullabletrue)]
  64.     #[Groups(['user:read''user:write'])]
  65.     private $accountingFirm;
  66.     private $oldPassword;
  67.     #[ORM\Column(type'string'length255nullabletrue)]
  68.     private $avatar null;
  69.     /**
  70.      * @Vich\UploadableField(mapping="avatars", fileNameProperty="avatar")
  71.      * @var File|null
  72.      */
  73.     private $tmpAvatar null;
  74.     #[ORM\Column(type'boolean')]
  75.     private $activation_token false;
  76.     #[ORM\ManyToOne(targetEntityAffiliate::class, inversedBy'users')]
  77.     private $affiliate;
  78.     #[ORM\OneToMany(mappedBy'owner'targetEntityShortUrl::class, orphanRemovaltrue)]
  79.     private Collection $shortUrls;
  80.     #[ORM\Column(type'boolean')]
  81.     private $notificationSent false;
  82.     #[ORM\OneToMany(mappedBy'user'targetEntityQrcode::class, cascade: ['persist''remove'])]
  83.     private Collection $qrcodes;
  84.     public function __construct()
  85.     {
  86.         $this->qrcodes = new ArrayCollection();
  87.         $this->shortUrls = new ArrayCollection();
  88.     }
  89.     public function getId(): ?int
  90.     {
  91.         return $this->id;
  92.     }
  93.     public function getEmail(): ?string
  94.     {
  95.         return $this->email;
  96.     }
  97.     public function setEmail(string $email): self
  98.     {
  99.         $this->email $email;
  100.         return $this;
  101.     }
  102.     public function getFirstname(): ?string
  103.     {
  104.         return $this->firstname;
  105.     }
  106.     public function setFirstname(string $firstname): self
  107.     {
  108.         $this->firstname $firstname;
  109.         return $this;
  110.     }
  111.     /**
  112.      * A visual identifier that represents this user.
  113.      *
  114.      * @see UserInterface
  115.      */
  116.     public function getUsername(): string
  117.     {
  118.         return (string) $this->email;
  119.     }
  120.     /**
  121.      * @see UserInterface
  122.      */
  123.     public function getRoles(): array
  124.     {
  125.         $roles is_string($this->roles) ? json_decode($this->roles) : $this->roles;
  126.         // guarantee every user at least has ROLE_USER
  127.         $roles[] = 'ROLE_USER';
  128.         return array_unique($roles);
  129.     }
  130.     public function setRoles(?array $roles): self
  131.     {
  132.         $this->roles $roles;
  133.         return $this;
  134.     }
  135.     /**
  136.      * @see UserInterface
  137.      */
  138.     public function getPassword(): string
  139.     {
  140.         return $this->password;
  141.     }
  142.     public function setPassword(string $password): self
  143.     {
  144.         $this->password $password;
  145.         return $this;
  146.     }
  147.     /**
  148.      * Returning a salt is only needed, if you are not using a modern
  149.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  150.      *
  151.      * @see UserInterface
  152.      */
  153.     public function getSalt(): ?string
  154.     {
  155.         return null;
  156.     }
  157.     /**
  158.      * @see UserInterface
  159.      */
  160.     public function eraseCredentials()
  161.     {
  162.         // If you store any temporary, sensitive data on the user, clear it here
  163.         $this->plainPassword null;
  164.     }
  165.     public function getPlainPassword(): ?string
  166.     {
  167.         return $this->plainPassword;
  168.     }
  169.     public function setPlainPassword(?string $plainPassword): self
  170.     {
  171.         $this->plainPassword $plainPassword;
  172.         return $this;
  173.     }
  174.     public function getAccountingFirm(): ?AccountingFirm
  175.     {
  176.         return $this->accountingFirm;
  177.     }
  178.     public function setAccountingFirm(?AccountingFirm $accountingFirm): self
  179.     {
  180.         $this->accountingFirm $accountingFirm;
  181.         return $this;
  182.     }
  183.     /**
  184.      * @return string|null
  185.      */
  186.     public function getOldPassword(): ?string
  187.     {
  188.         return $this->oldPassword;
  189.     }
  190.     /**
  191.      * @param string|null $oldPassword
  192.      * @return User
  193.      */
  194.     public function setOldPassword(?string $oldPassword): self
  195.     {
  196.         $this->oldPassword $oldPassword;
  197.         return $this;
  198.     }
  199.     public function getAvatar(): ?string
  200.     {
  201.         return $this->avatar;
  202.     }
  203.     public function setAvatar(?string $avatar): self
  204.     {
  205.         $this->avatar $avatar;
  206.         return $this;
  207.     }
  208.     /**
  209.      * @return File|null
  210.      */
  211.     public function getTmpAvatar(): ?File
  212.     {
  213.         return $this->tmpAvatar;
  214.     }
  215.     /**
  216.      * @param File|null $tmpAvatar
  217.      * @return User
  218.      */
  219.     public function setTmpAvatar(?File $tmpAvatar): self
  220.     {
  221.         $this->tmpAvatar $tmpAvatar;
  222.         $this->updatedAt = new \DateTime('now');
  223.         return $this;
  224.     }
  225.     public function getActivationToken(): ?bool
  226.     {
  227.         return $this->activation_token;
  228.     }
  229.     public function setActivationToken(bool $activation_token): self
  230.     {
  231.         $this->activation_token $activation_token;
  232.         return $this;
  233.     }
  234.     public function serialize()
  235.     {
  236.         return serialize(array(
  237.             $this->id,
  238.             $this->email,
  239.             $this->email,
  240.             $this->password,
  241.             $this->plainPassword,
  242.         ));
  243.     }
  244.     public function unserialize($serialized)
  245.     {
  246.         list(
  247.             $this->id,
  248.             $this->email,
  249.             $this->email,
  250.             $this->password,
  251.             $this->plainPassword,
  252.         ) = unserialize($serialized);
  253.     }
  254.     public function getAffiliate(): ?Affiliate
  255.     {
  256.         return $this->affiliate;
  257.     }
  258.     public function setAffiliate(?Affiliate $affiliate): self
  259.     {
  260.         $this->affiliate $affiliate;
  261.         return $this;
  262.     }
  263.     public function isNotificationSent(): bool
  264.     {
  265.         return $this->notificationSent;
  266.     }
  267.     public function setNotificationSent(bool $notificationSent): self
  268.     {
  269.         $this->notificationSent $notificationSent;
  270.     return $this;
  271.   }
  272.     public function getQrcodes(): Collection
  273.     {
  274.         return $this->qrcodes;
  275.     }
  276.     public function addQrcode(Qrcode $qrcode): self
  277.     {
  278.         if (!$this->qrcodes->contains($qrcode)) {
  279.             $this->qrcodes[] = $qrcode;
  280.             $qrcode->setUser($this);
  281.         }
  282.         return $this;
  283.     }
  284.     public function removeQrcode(Qrcode $qrcode): self
  285.     {
  286.         if ($this->qrcodes->removeElement($qrcode)) {
  287.             if ($qrcode->getUser() === $this) {
  288.                 $qrcode->setUser(null);
  289.             }
  290.         }
  291.         return $this;
  292.     }
  293.     public function getShortUrls(): Collection
  294.     {
  295.         return $this->shortUrls;
  296.     }
  297. }