src/Entity/User.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  6. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. /**
  9.  * @ORM\Entity(repositoryClass=UserRepository::class)
  10.  * @UniqueEntity(fields={"username"}, message="Il existe déjà un compte avec ce nom d'utilisateur")
  11.  */
  12. class User implements UserInterfacePasswordAuthenticatedUserInterface
  13. {
  14.     /**
  15.      * @ORM\Id
  16.      * @ORM\GeneratedValue
  17.      * @ORM\Column(type="integer")
  18.      */
  19.     private $id;
  20.     /**
  21.      * @ORM\Column(type="string", length=180, unique=true)
  22.      */
  23.     private $username;
  24.     /**
  25.      * @ORM\Column(type="json")
  26.      */
  27.     private $roles = [];
  28.     /**
  29.      * @var string The hashed password
  30.      * @ORM\Column(type="string")
  31.      */
  32.     private $password;
  33.     /**
  34.      * @ORM\Column(type="string", length=255)
  35.      */
  36.     private $fullname;
  37.     /**
  38.      * @ORM\Column(type="string", length=255, nullable=true)
  39.      */
  40.     private $email;
  41.     /**
  42.      * @ORM\Column(type="boolean")
  43.      */
  44.     private $isActive true;
  45.     /**
  46.      * @ORM\Column(type="datetime_immutable")
  47.      */
  48.     private $createdAt;
  49.     /**
  50.      * @ORM\Column(type="datetime_immutable", nullable=true)
  51.      */
  52.     private $updatedAt;
  53.     /**
  54.      * @ORM\Column(type="datetime_immutable", nullable=true)
  55.      */
  56.     private $lastLoginAt;
  57.     /**
  58.      * @ORM\ManyToOne(targetEntity=School::class, inversedBy="users")
  59.      * @ORM\JoinColumn(nullable=true)
  60.      */
  61.     private $school;
  62.     public function __construct()
  63.     {
  64.         $this->createdAt = new \DateTimeImmutable();
  65.         // Rôle par défaut
  66.         $this->roles = ['ROLE_USER'];
  67.     }
  68.     public function getId(): ?int
  69.     {
  70.         return $this->id;
  71.     }
  72.     /**
  73.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  74.      */
  75.     public function getUsername(): string
  76.     {
  77.         return (string) $this->username;
  78.     }
  79.     public function setUsername(string $username): self
  80.     {
  81.         $this->username $username;
  82.         return $this;
  83.     }
  84.     /**
  85.      * A visual identifier that represents this user.
  86.      *
  87.      * @see UserInterface
  88.      */
  89.     public function getUserIdentifier(): string
  90.     {
  91.         return (string) $this->username;
  92.     }
  93.     /**
  94.      * @see UserInterface
  95.      */
  96.     public function getRoles(): array
  97.     {
  98.         $roles $this->roles;
  99.         // guarantee every user at least has ROLE_USER
  100.         $roles[] = 'ROLE_USER';
  101.         return array_unique($roles);
  102.     }
  103.     public function setRoles(array $roles): self
  104.     {
  105.         $this->roles $roles;
  106.         return $this;
  107.     }
  108.     /**
  109.      * @see PasswordAuthenticatedUserInterface
  110.      */
  111.     public function getPassword(): string
  112.     {
  113.         return $this->password;
  114.     }
  115.     public function setPassword(string $password): self
  116.     {
  117.         $this->password $password;
  118.         return $this;
  119.     }
  120.     /**
  121.      * Returning a salt is only needed, if you are not using a modern
  122.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  123.      *
  124.      * @see UserInterface
  125.      */
  126.     public function getSalt(): ?string
  127.     {
  128.         return null;
  129.     }
  130.     /**
  131.      * @see UserInterface
  132.      */
  133.     public function eraseCredentials()
  134.     {
  135.         // If you store any temporary, sensitive data on the user, clear it here
  136.         // $this->plainPassword = null;
  137.     }
  138.     public function getFullname(): ?string
  139.     {
  140.         return $this->fullname;
  141.     }
  142.     public function setFullname(string $fullname): self
  143.     {
  144.         $this->fullname $fullname;
  145.         return $this;
  146.     }
  147.     public function getEmail(): ?string
  148.     {
  149.         return $this->email;
  150.     }
  151.     public function setEmail(?string $email): self
  152.     {
  153.         $this->email $email;
  154.         return $this;
  155.     }
  156.     public function getIsActive(): ?bool
  157.     {
  158.         return $this->isActive;
  159.     }
  160.     public function setIsActive(bool $isActive): self
  161.     {
  162.         $this->isActive $isActive;
  163.         return $this;
  164.     }
  165.     public function getCreatedAt(): ?\DateTimeImmutable
  166.     {
  167.         return $this->createdAt;
  168.     }
  169.     public function setCreatedAt(\DateTimeImmutable $createdAt): self
  170.     {
  171.         $this->createdAt $createdAt;
  172.         return $this;
  173.     }
  174.     public function getUpdatedAt(): ?\DateTimeImmutable
  175.     {
  176.         return $this->updatedAt;
  177.     }
  178.     public function setUpdatedAt(?\DateTimeImmutable $updatedAt): self
  179.     {
  180.         $this->updatedAt $updatedAt;
  181.         return $this;
  182.     }
  183.     public function getLastLoginAt(): ?\DateTimeImmutable
  184.     {
  185.         return $this->lastLoginAt;
  186.     }
  187.     public function setLastLoginAt(?\DateTimeImmutable $lastLoginAt): self
  188.     {
  189.         $this->lastLoginAt $lastLoginAt;
  190.         return $this;
  191.     }
  192.     public function getSchool(): ?School
  193.     {
  194.         return $this->school;
  195.     }
  196.     public function setSchool(?School $school): self
  197.     {
  198.         $this->school $school;
  199.         return $this;
  200.     }
  201.     /**
  202.      * Vérifie si l'utilisateur est super admin
  203.      */
  204.     public function isSuperAdmin(): bool
  205.     {
  206.         return in_array('ROLE_SUPER_ADMIN'$this->getRoles());
  207.     }
  208.     /**
  209.      * Vérifie si l'utilisateur est admin d'école
  210.      */
  211.     public function isAdmin(): bool
  212.     {
  213.         return in_array('ROLE_ADMIN'$this->getRoles());
  214.     }
  215.     /**
  216.      * Vérifie si l'utilisateur peut gérer les utilisateurs
  217.      */
  218.     public function canManageUsers(): bool
  219.     {
  220.         return $this->isSuperAdmin() || $this->isAdmin();
  221.     }
  222.     /**
  223.      * Vérifie si l'utilisateur peut gérer les écoles
  224.      */
  225.     public function canManageSchools(): bool
  226.     {
  227.         return $this->isSuperAdmin();
  228.     }
  229.     /**
  230.      * Vérifie si l'utilisateur peut créer des utilisateurs
  231.      */
  232.     public function canCreateUsers(): bool
  233.     {
  234.         return $this->isSuperAdmin() || $this->isAdmin();
  235.     }
  236.     /**
  237.      * Définit le rôle selon le type d'utilisateur
  238.      */
  239.     public function setRoleByType(string $type): self
  240.     {
  241.         switch ($type) {
  242.             case 'super_admin':
  243.                 $this->setRoles(['ROLE_SUPER_ADMIN']);
  244.                 break;
  245.             case 'admin':
  246.                 $this->setRoles(['ROLE_ADMIN']);
  247.                 break;
  248.             case 'user':
  249.             default:
  250.                 $this->setRoles(['ROLE_USER']);
  251.                 break;
  252.         }
  253.         
  254.         return $this;
  255.     }
  256.     public function __toString(): string
  257.     {
  258.         return sprintf(
  259.             '%s (%s)',
  260.             $this->getFullname(),
  261.             $this->getUsername()
  262.         );
  263.     }