You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
3.8 KiB
PHP

<?php
namespace App\Entity;
use App\Repository\UserRepository;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Stringable;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
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)]
#[UniqueEntity(fields: ['email'], message: 'Il existe déjà un compte avec cette adresse mail')]
class User implements UserInterface, PasswordAuthenticatedUserInterface, Stringable {
use TEntityBase;
/**
* @var string The email
*/
#[ORM\Column(length: 100, unique: true)]
#[Assert\NotBlank(message: 'Veuillez saisir un email')]
#[Assert\Email(message: 'Veuillez saisir un email valide')]
private string $email;
/**
* @var string The hashed password
*/
#[ORM\Column(length: 255)]
private string $password;
/**
* @var string|null The name
*/
#[ORM\Column(length: 100, nullable: true)]
private ?string $name = null;
/**
* @var string[] The roles
*/
#[ORM\Column]
private array $roles = [];
/**
* @inheritDoc
*/
public function __toString (): string {
return $this->getName() ?? $this->getEmail();
}
/**
* 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;
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
*
* @see UserInterface
*/
public function getUserIdentifier (): string {
return $this->email;
}
/**
* Has a role ?
*
* @param string $role The role
*
* @return bool True if the user has the role, else False
*/
public function hasRole (string $role): bool {
return in_array($role, $this->getRoles());
}
/**
* The roles
*
* @return string[] The roles
*
* @see UserInterface
*/
public function getRoles (): array {
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
/**
* Set the roles
*
* @param array $roles The new roles
*
* @return void
*/
public function setRoles (array $roles): void {
$this->roles = $roles;
}
/**
* Removes sensitive data from the user
*
* @see UserInterface
*/
public function eraseCredentials (): void {
}
}