src/Entity/User.php line 25
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
#[ApiResource(
normalizationContext: ['groups' => ['read']],
denormalizationContext: ['groups' => ['write']],
)]
#[ApiFilter(SearchFilter::class, properties: ['username' => 'exact','googleId' => 'exact'])]
#[ORM\Table(name: 'user')]
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['username'], message: 'There is already an account with this username')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[Groups(["read","write"])]
#[ORM\Column(type: 'string', length: 180, unique: true)]
private ?string $username = null;
#[Groups(["read","write"])]
#[ORM\Column(type: 'json')]
private array $roles = [];
#[Groups(["read","write"])]
#[ORM\Column(type: 'string')]
private ?string $password = "";
#[ORM\Column(type: 'boolean', nullable: true)]
#[Groups(["read","write"])]
private ?bool $isValid = null;
#[ORM\Column(type: 'boolean')]
#[Groups(["read","write"])]
private bool $isVerified = false;
#[ORM\ManyToMany(targetEntity: Role::class, inversedBy: 'users')]
private Collection $storedRoles;
#[Groups(["read","write"])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $exponentPushToken = null;
#[Groups(["read","write"])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $googleId = null;
#[Groups(["read","write"])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $avatar = null;
#[Groups(["read","write"])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $facebookId = null;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: AiRequest::class)]
private Collection $aiRequests;
public function __construct()
{
$this->storedRoles = new ArrayCollection();
$this->isValid = true;
$this->aiRequests = new ArrayCollection();
}
public function __toString()
{
return $this->getUserIdentifier();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->username;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
foreach($this->getStoredRoles() as $singleRole){
$roles[] = $singleRole->getName();
}
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getIsValid(): ?bool
{
return $this->isValid;
}
public function setIsValid(?bool $isValid): self
{
$this->isValid = $isValid;
return $this;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
/**
* @return Collection<int, Role>
*/
public function getStoredRoles(): Collection
{
return $this->storedRoles;
}
public function addStoredRole(Role $storedRole): self
{
if (!$this->storedRoles->contains($storedRole)) {
$this->storedRoles->add($storedRole);
$storedRole->addUser($this);
}
return $this;
}
public function removeStoredRole(Role $storedRole): self
{
if ($this->storedRoles->removeElement($storedRole)) {
$storedRole->removeUser($this);
}
return $this;
}
public function getExponentPushToken(): ?string
{
return $this->exponentPushToken;
}
public function setExponentPushToken(?string $exponentPushToken): self
{
$this->exponentPushToken = $exponentPushToken;
return $this;
}
public function getGoogleId(): ?string
{
return $this->googleId;
}
public function setGoogleId(?string $googleId): self
{
$this->googleId = $googleId;
return $this;
}
public function getAvatar(): ?string
{
return $this->avatar;
}
public function setAvatar(?string $avatar): self
{
$this->avatar = $avatar;
return $this;
}
public function getFacebookId(): ?string
{
return $this->facebookId;
}
public function setFacebookId(?string $facebookId): self
{
$this->facebookId = $facebookId;
return $this;
}
/**
* @return Collection<int, AiRequest>
*/
public function getAiRequests(): Collection
{
return $this->aiRequests;
}
public function addAiRequest(AiRequest $aiRequest): static
{
if (!$this->aiRequests->contains($aiRequest)) {
$this->aiRequests->add($aiRequest);
$aiRequest->setUser($this);
}
return $this;
}
public function removeAiRequest(AiRequest $aiRequest): static
{
if ($this->aiRequests->removeElement($aiRequest)) {
// set the owning side to null (unless already changed)
if ($aiRequest->getUser() === $this) {
$aiRequest->setUser(null);
}
}
return $this;
}
}