src/Entity/User.php line 25

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use App\Repository\UserRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  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\Serializer\Annotation\Groups;
  13. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  14. #[ApiResource(
  15.     normalizationContext: ['groups' => ['read']],
  16.     denormalizationContext: ['groups' => ['write']],
  17. )]
  18. #[ApiFilter(SearchFilter::class, properties: ['username' => 'exact','googleId' => 'exact'])]
  19. #[ORM\Table(name'user')]
  20. #[ORM\Entity(repositoryClassUserRepository::class)]
  21. #[UniqueEntity(fields: ['username'], message'There is already an account with this username')]
  22. class User implements UserInterfacePasswordAuthenticatedUserInterface
  23. {
  24.     #[ORM\Id]
  25.     #[ORM\GeneratedValue]
  26.     #[ORM\Column(type'integer')]
  27.     private $id;
  28.     #[Groups(["read","write"])]
  29.     #[ORM\Column(type'string'length180uniquetrue)]
  30.     private ?string $username null;
  31.     #[Groups(["read","write"])]
  32.     #[ORM\Column(type'json')]
  33.     private array $roles = [];
  34.     #[Groups(["read","write"])]
  35.     #[ORM\Column(type'string')]
  36.     private ?string $password "";
  37.     
  38.     #[ORM\Column(type'boolean'nullabletrue)]
  39.     #[Groups(["read","write"])]
  40.     private ?bool $isValid null;
  41.     #[ORM\Column(type'boolean')]
  42.     #[Groups(["read","write"])]
  43.     private bool $isVerified false;
  44.     #[ORM\ManyToMany(targetEntityRole::class, mappedBy'users')]
  45.     private Collection $storedRoles;
  46.     
  47.     #[Groups(["read","write"])]
  48.     #[ORM\Column(length255nullabletrue)]
  49.     private ?string $exponentPushToken null;
  50.     #[Groups(["read","write"])]
  51.     #[ORM\Column(length255nullabletrue)]
  52.     private ?string $googleId null;
  53.     #[Groups(["read","write"])]
  54.     #[ORM\Column(length255nullabletrue)]
  55.     private ?string $avatar null;
  56.     #[Groups(["read","write"])]
  57.     #[ORM\Column(length255nullabletrue)]
  58.     private ?string $facebookId null;
  59.     public function __construct()
  60.     {
  61.         $this->storedRoles = new ArrayCollection();
  62.         $this->isValid true;
  63.     }
  64.     public function __toString()
  65.     {
  66.         return $this->getUserIdentifier();
  67.     }
  68.     public function getId(): ?int
  69.     {
  70.         return $this->id;
  71.     }
  72.     /**
  73.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  74.      */
  75.     public function getUsername(): string
  76.     {
  77.         return (string) $this->username;
  78.     }
  79.     public function setUsername(string $username): self
  80.     {
  81.         $this->username $username;
  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->username;
  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.         foreach($this->getStoredRoles() as $singleRole){
  102.             $roles[] = $singleRole->getName();
  103.         }
  104.         return array_unique($roles);
  105.     }
  106.     public function setRoles(array $roles): self
  107.     {
  108.         $this->roles $roles;
  109.         return $this;
  110.     }
  111.     /**
  112.      * @see PasswordAuthenticatedUserInterface
  113.      */
  114.     public function getPassword(): string
  115.     {
  116.         return $this->password;
  117.     }
  118.     public function setPassword(string $password): self
  119.     {
  120.         $this->password $password;
  121.         return $this;
  122.     }
  123.     /**
  124.      * Returning a salt is only needed, if you are not using a modern
  125.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  126.      *
  127.      * @see UserInterface
  128.      */
  129.     public function getSalt(): ?string
  130.     {
  131.         return null;
  132.     }
  133.     /**
  134.      * @see UserInterface
  135.      */
  136.     public function eraseCredentials()
  137.     {
  138.         // If you store any temporary, sensitive data on the user, clear it here
  139.         // $this->plainPassword = null;
  140.     }
  141.     public function getIsValid(): ?bool
  142.     {
  143.         return $this->isValid;
  144.     }
  145.     public function setIsValid(?bool $isValid): self
  146.     {
  147.         $this->isValid $isValid;
  148.         return $this;
  149.     }
  150.     public function isVerified(): bool
  151.     {
  152.         return $this->isVerified;
  153.     }
  154.     public function setIsVerified(bool $isVerified): self
  155.     {
  156.         $this->isVerified $isVerified;
  157.         return $this;
  158.     }
  159.     /**
  160.      * @return Collection<int, Role>
  161.      */
  162.     public function getStoredRoles(): Collection
  163.     {
  164.         return $this->storedRoles;
  165.     }
  166.     public function addStoredRole(Role $storedRole): self
  167.     {
  168.         if (!$this->storedRoles->contains($storedRole)) {
  169.             $this->storedRoles->add($storedRole);
  170.             $storedRole->addUser($this);
  171.         }
  172.         return $this;
  173.     }
  174.     public function removeStoredRole(Role $storedRole): self
  175.     {
  176.         if ($this->storedRoles->removeElement($storedRole)) {
  177.             $storedRole->removeUser($this);
  178.         }
  179.         return $this;
  180.     }
  181.     public function getExponentPushToken(): ?string
  182.     {
  183.         return $this->exponentPushToken;
  184.     }
  185.     public function setExponentPushToken(?string $exponentPushToken): self
  186.     {
  187.         $this->exponentPushToken $exponentPushToken;
  188.         return $this;
  189.     }
  190.     public function getGoogleId(): ?string
  191.     {
  192.         return $this->googleId;
  193.     }
  194.     public function setGoogleId(?string $googleId): self
  195.     {
  196.         $this->googleId $googleId;
  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.     public function getFacebookId(): ?string
  209.     {
  210.         return $this->facebookId;
  211.     }
  212.     public function setFacebookId(?string $facebookId): self
  213.     {
  214.         $this->facebookId $facebookId;
  215.         return $this;
  216.     }
  217. }