From 5fa381695b078d358f7407f25c990262c8790f70 Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Fri, 26 Apr 2024 15:48:58 +0200 Subject: [PATCH] Update user entity: add validation administrator and date-time --- diagrams/database.drawio | 2 +- src/Entity/TEntityBase.php | 64 ++++++++++ src/Entity/User.php | 241 +++++++++++++++++++++++++++++++------ 3 files changed, 266 insertions(+), 41 deletions(-) create mode 100644 src/Entity/TEntityBase.php diff --git a/diagrams/database.drawio b/diagrams/database.drawio index 16968a2..35c06b4 100644 --- a/diagrams/database.drawio +++ b/diagrams/database.drawio @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/src/Entity/TEntityBase.php b/src/Entity/TEntityBase.php new file mode 100644 index 0000000..27d0e49 --- /dev/null +++ b/src/Entity/TEntityBase.php @@ -0,0 +1,64 @@ +id; + } + + /** + * The creation date and time + * + * @return DateTimeInterface|null The creation date and time + */ + public function getCreation (): ?DateTimeInterface { + return $this->creation; + } + + /** + * The last modification date and time + * + * @return DateTimeInterface|null The last modification date and time + */ + public function getLastModification (): ?DateTimeInterface { + return $this->lastModification; + } +} \ No newline at end of file diff --git a/src/Entity/User.php b/src/Entity/User.php index f765df9..cef1a0d 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -3,97 +3,258 @@ namespace App\Entity; use App\Repository\UserRepository; +use DateTimeImmutable; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\UserInterface; +use Symfony\Component\Validator\Constraints as Assert; +/** + * A registered user + */ #[ORM\Entity(repositoryClass: UserRepository::class)] -class User implements UserInterface, PasswordAuthenticatedUserInterface -{ - #[ORM\Id] - #[ORM\GeneratedValue] +class User implements UserInterface, PasswordAuthenticatedUserInterface { + use TEntityBase; + + /** + * @var string|null The email + */ + #[ORM\Column(length: 100, unique: true)] + #[Assert\NotBlank] + #[Assert\Email] + private ?string $email = null; + /** + * @var string|null The hashed password + */ #[ORM\Column] - private ?int $id = null; + #[Assert\NotBlank] + private ?string $password = null; - #[ORM\Column(length: 180, unique: true)] - private ?string $username = null; + /** + * @var string|null The name + */ + #[ORM\Column(length: 100, nullable: true)] + private ?string $name = null; + /** + * @var string[] The roles + */ #[ORM\Column] private array $roles = []; /** - * @var string The hashed password + * @var Collection The {@see Tag tags} of the user */ - #[ORM\Column] - private ?string $password = null; + #[ORM\OneToMany(mappedBy: 'user', targetEntity: Tag::class, orphanRemoval: true)] + #[Assert\Valid] + private Collection $tags; - public function getId(): ?int - { - return $this->id; + /** + * @var User|null The administrator who had validated this user or Null if not validated yet + */ + #[ORM\ManyToOne(targetEntity: self::class)] + #[Assert\Valid] + private ?self $validationAdministrator = null; + /** + * @var DateTimeImmutable|null The date and time of the validation of this user or Null if not validated yet + */ + #[ORM\Column(nullable: true)] + private ?DateTimeImmutable $validationDate = null; + + /** + * Initialization + */ + public function __construct () { + $this->tags = new ArrayCollection(); } - public function getUsername(): ?string - { - return $this->username; + /** + * The email + * + * @return string|null The email + */ + public function getEmail (): ?string { + return $this->email; } + /** + * Change the email + * + * @param string $email The new email + * + * @return $this + */ + public function setEmail (string $email): self { + $this->email = $email; - public function setUsername(string $username): self - { - $this->username = $username; + return $this; + } + + /** + * The hashed password + * + * @return string The hashed password + * + * @see PasswordAuthenticatedUserInterface + */ + public function getPassword (): string { + return $this->password; + } + /** + * Change the hashed password + * + * @param string $password The new hashed password + * + * @return $this + */ + public function setPassword (string $password): self { + $this->password = $password; + + return $this; + } + + /** + * The name + * + * @return string|null The name + */ + public function getName (): ?string { + return $this->name; + } + /** + * Change the name + * + * @param string|null $name The new name + * + * @return $this + */ + public function setName (?string $name): self { + $this->name = $name; return $this; } /** - * A visual identifier that represents this user. + * A visual identifier that represents this user * * @see UserInterface */ - public function getUserIdentifier(): string - { - return (string) $this->username; + public function getUserIdentifier (): string { + return (string)$this->email; } /** + * The roles + * + * @return string[] The roles + * * @see UserInterface */ - public function getRoles(): array - { + public function getRoles (): array { $roles = $this->roles; // guarantee every user at least has ROLE_USER $roles[] = 'ROLE_USER'; return array_unique($roles); } - - public function setRoles(array $roles): self - { + /** + * Set the roles + * + * @param array $roles The new roles + * + * @return void + */ + public function setRoles (array $roles): void { $this->roles = $roles; + } - return $this; + /** + * Removes sensitive data from the user + * + * @see UserInterface + */ + public function eraseCredentials () { } /** - * @see PasswordAuthenticatedUserInterface + * The {@see Tag tags} of the user + * + * @return Collection The {@see Tag tags} of the user */ - public function getPassword(): string - { - return $this->password; + public function getTags (): Collection { + return $this->tags; } + /** + * Add a {@see Tag tag} to the user + * + * Do nothing if the {@see Tag tag} already exists + * + * @param Tag $tag The new {@see Tag tag} + * + * @return $this + */ + public function addTag (Tag $tag): static { + if (!$this->tags->contains($tag)) { + $this->tags->add($tag); + $tag->setUser($this); + } - public function setPassword(string $password): self - { - $this->password = $password; + return $this; + } + /** + * Remove a {@see Tag tag} from the user + * + * Do nothing if the {@see Tag tag} don't exist + * + * @param Tag $tag The {@see Tag tag} to remove + * + * @return $this + */ + public function removeTag (Tag $tag): static { + if ($this->tags->removeElement($tag)) { + if ($tag->getUser() === $this) { + $tag->setUser(null); + } + } return $this; } /** - * @see UserInterface + * The administrator who had validated this user or Null if not validated yet + * + * @return self|null The administrator who had validated this user or Null if not validated yet + * + * @see self::validate() */ - public function eraseCredentials() + public function getValidationAdministrator (): ?self { - // If you store any temporary, sensitive data on the user, clear it here - // $this->plainPassword = null; + return $this->validationAdministrator; + } + /** + * The date and time of the validation of this user or Null if not validated yet + * + * @return DateTimeImmutable|null The date and time of the validation of this user or Null if not validated yet + * + * @see self::validate() + */ + public function getValidationDate (): ?DateTimeImmutable + { + return $this->validationDate; + } + /** + * Validate this user + * + * Set validation administrator and date-time + * + * @param User $administrator The administrator doing the validation + * + * @return $this + */ + public function validate (self $administrator): static { + $this->validationAdministrator = $administrator; + $this->validationDate = new DateTimeImmutable(); + return $this; } }