<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity(fields={"username"}, message="Il existe déjà un compte avec ce nom d'utilisateur")
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $username;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\Column(type="string", length=255)
*/
private $fullname;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $email;
/**
* @ORM\Column(type="boolean")
*/
private $isActive = true;
/**
* @ORM\Column(type="datetime_immutable")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $updatedAt;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $lastLoginAt;
/**
* @ORM\ManyToOne(targetEntity=School::class, inversedBy="users")
* @ORM\JoinColumn(nullable=true)
*/
private $school;
public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
// Rôle par défaut
$this->roles = ['ROLE_USER'];
}
public function getId(): ?int
{
return $this->id;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->username;
}
/**
* @see UserInterface
*/
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
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $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 getFullname(): ?string
{
return $this->fullname;
}
public function setFullname(string $fullname): self
{
$this->fullname = $fullname;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
public function getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getLastLoginAt(): ?\DateTimeImmutable
{
return $this->lastLoginAt;
}
public function setLastLoginAt(?\DateTimeImmutable $lastLoginAt): self
{
$this->lastLoginAt = $lastLoginAt;
return $this;
}
public function getSchool(): ?School
{
return $this->school;
}
public function setSchool(?School $school): self
{
$this->school = $school;
return $this;
}
/**
* Vérifie si l'utilisateur est super admin
*/
public function isSuperAdmin(): bool
{
return in_array('ROLE_SUPER_ADMIN', $this->getRoles());
}
/**
* Vérifie si l'utilisateur est admin d'école
*/
public function isAdmin(): bool
{
return in_array('ROLE_ADMIN', $this->getRoles());
}
/**
* Vérifie si l'utilisateur peut gérer les utilisateurs
*/
public function canManageUsers(): bool
{
return $this->isSuperAdmin() || $this->isAdmin();
}
/**
* Vérifie si l'utilisateur peut gérer les écoles
*/
public function canManageSchools(): bool
{
return $this->isSuperAdmin();
}
/**
* Vérifie si l'utilisateur peut créer des utilisateurs
*/
public function canCreateUsers(): bool
{
return $this->isSuperAdmin() || $this->isAdmin();
}
/**
* Définit le rôle selon le type d'utilisateur
*/
public function setRoleByType(string $type): self
{
switch ($type) {
case 'super_admin':
$this->setRoles(['ROLE_SUPER_ADMIN']);
break;
case 'admin':
$this->setRoles(['ROLE_ADMIN']);
break;
case 'user':
default:
$this->setRoles(['ROLE_USER']);
break;
}
return $this;
}
public function __toString(): string
{
return sprintf(
'%s (%s)',
$this->getFullname(),
$this->getUsername()
);
}
}