Update user entity: add validation administrator and date-time

master
Julien Rosset 1 year ago
parent f8ea16fbc9
commit 5fa381695b

File diff suppressed because one or more lines are too long

@ -0,0 +1,64 @@
<?php
namespace App\Entity;
use DateTimeInterface;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Mapping\Annotation\Timestampable;
/**
* Implementation for the base implementation of an entity: id, creation and last update date and time
*/
trait TEntityBase {
/**
* @var int|null The internal id
*
* @noinspection PhpPropertyNamingConventionInspection
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
/**
* @var DateTimeInterface|null The creation date and time
*/
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: false)]
#[Gedmo\Timestampable(on: 'create')]
private ?DateTimeInterface $creation = null;
/**
* @var DateTimeInterface|null The last modification date and time
*/
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: false)]
#[Timestampable(on: 'update')]
private ?DateTimeInterface $lastModification = null;
/**
* The internal id
*
* @return int|null The internal id
*/
public function getId (): ?int {
return $this->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;
}
}

@ -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<int, Tag> 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<int, Tag> 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;
}
}

Loading…
Cancel
Save