src/Entity/User.php line 21
<?php
namespace App\Entity;
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;
#[ApiResource(
denormalizationContext: ['groups' => ['write']],
)]
#[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("write")]
#[ORM\Column(type: 'string', length: 180, unique: true)]
private ?string $username = null;
#[Groups("write")]
#[ORM\Column(type: 'json')]
private array $roles = [];
#[Groups("write")]
#[ORM\Column(type: 'string')]
private ?string $password = null;
#[Groups("write")]
#[ORM\Column(type: 'boolean', nullable: true)]
private ?bool $isValid = null;
#[ORM\Column(type: 'boolean')]
#[Groups("write")]
private bool $isVerified = false;
#[ORM\ManyToMany(targetEntity: Role::class, mappedBy: 'users')]
private Collection $storedRoles;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: SessionHistory::class, orphanRemoval: true)]
private Collection $sessionHistories;
public function __construct()
{
$this->storedRoles = new ArrayCollection();
$this->isValid = true;
$this->sessionHistories = 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;
}
/**
* @return Collection<int, SessionHistory>
*/
public function getSessionHistories(): Collection
{
return $this->sessionHistories;
}
public function addSessionHistory(SessionHistory $sessionHistory): self
{
if (!$this->sessionHistories->contains($sessionHistory)) {
$this->sessionHistories->add($sessionHistory);
$sessionHistory->setUser($this);
}
return $this;
}
public function removeSessionHistory(SessionHistory $sessionHistory): self
{
if ($this->sessionHistories->removeElement($sessionHistory)) {
// set the owning side to null (unless already changed)
if ($sessionHistory->getUser() === $this) {
$sessionHistory->setUser(null);
}
}
return $this;
}
}