changed to attributes

This commit is contained in:
tracer 2022-04-11 17:36:01 +02:00
parent 00b0f163e5
commit 86dd5ae078
1 changed files with 78 additions and 189 deletions

View File

@ -6,88 +6,53 @@ use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use JetBrains\PhpStorm\Pure;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
/** /**
* @ORM\Entity(repositoryClass=UserRepository::class) *
* @UniqueEntity(fields={"username"}, message="There is already an account with this username")
* @ORM\HasLifecycleCallbacks
* @method string getUserIdentifier()
*/ */
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
class User implements UserInterface, PasswordAuthenticatedUserInterface class User implements UserInterface, PasswordAuthenticatedUserInterface
{ {
/** #[ORM\Id]
* @ORM\Id #[ORM\GeneratedValue]
* @ORM\GeneratedValue #[ORM\Column(type: 'integer')]
* @ORM\Column(type="integer") private int $id;
*/
private $id;
/** #[ORM\Column(type: 'string', length: 180, unique: true)]
* @ORM\Column(type="string", length=180, unique=true) private string $username;
*/
private $username;
/** #[ORM\Column(type: 'json')]
* @ORM\Column(type="json") private array $roles = [];
*/
private $roles = [];
/** #[ORM\Column(type: 'string')]
* @var string The hashed password private string $password;
* @ORM\Column(type="string")
*/
private $password;
/** private string $plainPassword;
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $lastName;
/** #[ORM\Column(type: 'string', length: 255)]
* @ORM\Column(type="string", length=255) private string $email;
*/
private $email;
/** #[ORM\Column(type: 'string', length: 255, nullable: true)]
* @ORM\Column(type="datetime") private ?string $firstName = '';
*/
private $createdAt;
/** #[ORM\Column(type: 'string', length: 255, nullable: true)]
* @ORM\Column(type="datetime", nullable=true) private ?string $lastName = '';
*/
private $lastLoginAt;
/** #[ORM\ManyToMany(targetEntity: Projects::class, mappedBy: 'developer')]
* @ORM\OneToMany(targetEntity=Blog::class, mappedBy="author") private $projects;
*/
private $blogs;
/** public function __construct()
* @ORM\OneToMany(targetEntity=Comment::class, mappedBy="author")
*/
private $comments;
/**
* @ORM\Column(type="boolean")
*/
private $isVerified = false;
#[Pure] public function __construct()
{ {
$this->blogs = new ArrayCollection(); $this->projects = new ArrayCollection();
$this->comments = new ArrayCollection();
} }
/**
* @return null|string
*/
public function __toString() public function __toString()
{ {
return $this->username; return $this->username;
@ -99,13 +64,16 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
} }
/** /**
* A visual identifier that represents this user. * @return string
*
* @see UserInterface
*/ */
public function getUsername(): string public function getPlainPassword(): string
{ {
return (string)$this->username; return $this->plainPassword;
}
public function getUsername(): ?string
{
return $this->username;
} }
public function setUsername(string $username): self public function setUsername(string $username): self
@ -115,6 +83,16 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
return $this; return $this;
} }
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return $this->username;
}
/** /**
* @see UserInterface * @see UserInterface
*/ */
@ -124,7 +102,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
// guarantee every user at least has ROLE_USER // guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER'; $roles[] = 'ROLE_USER';
return array_unique($roles); return array_unique(array: $roles);
} }
public function setRoles(array $roles): self public function setRoles(array $roles): self
@ -135,7 +113,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
} }
/** /**
* @see UserInterface * @see PasswordAuthenticatedUserInterface
*/ */
public function getPassword(): string public function getPassword(): string
{ {
@ -149,24 +127,32 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
return $this; 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 * @see UserInterface
*/ */
public function eraseCredentials() public function eraseCredentials()
{ {
// If you store any temporary, sensitive data on the user, clear it here $this->plainPassword = '';
// $this->plainPassword = null; }
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* @param string $plainPassword
*/
public function setPlainPassword(string $plainPassword): void
{
$this->plainPassword = $plainPassword;
} }
public function getFirstName(): ?string public function getFirstName(): ?string
@ -193,127 +179,30 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
return $this; return $this;
} }
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getLastLoginAt(): ?\DateTimeInterface
{
return $this->lastLoginAt;
}
public function setLastLoginAt(?\DateTimeInterface $lastLoginAt): self
{
$this->lastLoginAt = $lastLoginAt;
return $this;
}
/** /**
* @return Collection|Blog[] * @return Collection<int, Projects>
*/ */
public function getBlogs(): Collection public function getProjects(): Collection
{ {
return $this->blogs; return $this->projects;
} }
public function addBlog(Blog $blog): self public function addProject(Projects $project): self
{ {
if (!$this->blogs->contains($blog)) { if (!$this->projects->contains(element: $project)) {
$this->blogs[] = $blog; $this->projects[] = $project;
$blog->setAuthor($this); $project->addDeveloper(developer: $this);
} }
return $this; return $this;
} }
public function removeBlog(Blog $blog): self public function removeProject(Projects $project): self
{ {
if ($this->blogs->removeElement($blog)) { if ($this->projects->removeElement(element: $project)) {
// set the owning side to null (unless already changed) $project->removeDeveloper(developer: $this);
if ($blog->getAuthor() === $this) {
$blog->setAuthor(null);
}
} }
return $this; return $this;
} }
/**
* @return Collection|Comment[]
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): self
{
if (!$this->comments->contains($comment)) {
$this->comments[] = $comment;
$comment->setAuthor($this);
}
return $this;
}
public function removeComment(Comment $comment): self
{
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getAuthor() === $this) {
$comment->setAuthor(null);
}
}
return $this;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
public function __call(string $name, array $arguments)
{
// TODO: Implement @method string getUserIdentifier()
}
/**
* Gets triggered only on insert
* @ORM\PrePersist
*/
public function onPrePersist()
{
$this->createdAt = new \DateTime();
}
} }