tinternet.net/src/Entity/User.php

290 lines
6.1 KiB
PHP

<?php
namespace App\Entity;
use App\Doctrine\Timestampable;
use App\Entity\Blog\Post;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;
use Scheb\TwoFactorBundle\Model\Totp\TotpConfiguration;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="`user`")
*/
class User implements UserInterface, TwoFactorInterface, EntityInterface
{
use Timestampable;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $displayName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $totpSecret;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $passwordRequestedAt;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $confirmationToken;
/**
* @ORM\Column(type="boolean", options={"default"=0})
*/
private $isAdmin;
/**
* @ORM\OneToMany(targetEntity=Post::class, mappedBy="author")
*/
private $posts;
public function __construct()
{
$this->posts = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
if ($this->getIsAdmin()) {
$roles[] = 'ROLE_ADMIN';
}
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $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 getDisplayName(): ?string
{
return $this->displayName;
}
public function setDisplayName(?string $displayName): self
{
$this->displayName = $displayName;
return $this;
}
public function getTotpSecret(): ?string
{
return $this->totpSecret;
}
public function setTotpSecret(?string $totpSecret): self
{
$this->totpSecret = $totpSecret;
return $this;
}
public function isTotpAuthenticationEnabled(): bool
{
return null !== $this->getTotpSecret();
}
public function getTotpAuthenticationUsername(): string
{
return $this->getEmail();
}
public function getTotpAuthenticationConfiguration(): TotpConfigurationInterface
{
return new TotpConfiguration($this->totpSecret, TotpConfiguration::ALGORITHM_SHA1, 30, 6);
}
public function isGoogleAuthenticatorEnabled(): bool
{
return $this->isTotpAuthenticationEnabled();
}
public function getGoogleAuthenticatorUsername(): string
{
return $this->getTotpAuthenticationUsername();
}
public function getGoogleAuthenticatorSecret(): ?string
{
return $this->getTotpSecret();
}
public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): void
{
$this->setTotpSecret($googleAuthenticatorSecret);
}
public function getPasswordRequestedAt(): ?\DateTimeInterface
{
return $this->passwordRequestedAt;
}
public function setPasswordRequestedAt(?\DateTimeInterface $passwordRequestedAt): self
{
$this->passwordRequestedAt = $passwordRequestedAt;
return $this;
}
public function getConfirmationToken(): ?string
{
return $this->confirmationToken;
}
public function setConfirmationToken(?string $confirmationToken): self
{
$this->confirmationToken = $confirmationToken;
return $this;
}
public function getIsAdmin(): ?bool
{
return $this->isAdmin;
}
public function setIsAdmin(bool $isAdmin): self
{
$this->isAdmin = $isAdmin;
return $this;
}
/**
* @return Collection|Post[]
*/
public function getPosts(): Collection
{
return $this->posts;
}
public function addPost(Post $post): self
{
if (!$this->posts->contains($post)) {
$this->posts[] = $post;
$post->setAuthor($this);
}
return $this;
}
public function removePost(Post $post): self
{
if ($this->posts->removeElement($post)) {
// set the owning side to null (unless already changed)
if ($post->getAuthor() === $this) {
$post->setAuthor(null);
}
}
return $this;
}
}