src/Entity/User.php line 13

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\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. /**
  8.  * @ORM\Entity(repositoryClass=UserRepository::class)
  9.  */
  10. class User implements UserInterfacePasswordAuthenticatedUserInterface {
  11.     const USER_ROLE_USER 'ROLE_USER';
  12.     const USER_ROLE_ADMIN 'ROLE_ADMIN';
  13.     const USER_ROLE_TEACHER 'ROLE_TEACHER';
  14.     const USER_ROLE_STUDENT 'ROLE_STUDENT';
  15.     const USER_ROLE_ADMIN_AUX 'ROLE_ADMIN_AUX';
  16.     const USER_IS_ACTIVE 'USER_ACTIVE';
  17.     const USER_IS_UNACTIVE 'USER_UNACTIVE';
  18.     
  19.     /**
  20.      * @ORM\Id
  21.      * @ORM\GeneratedValue
  22.      * @ORM\Column(type="integer")
  23.      */
  24.     private $id;
  25.     /**
  26.      * @ORM\Column(type="string", length=180)
  27.      */
  28.     private $fullname;
  29.     
  30.     /**
  31.      * @ORM\Column(type="string", length=180, unique=true)
  32.      */
  33.     private $username;
  34.     /**
  35.      * @ORM\Column(type="json")
  36.      */
  37.     private $roles = [];
  38.     /**
  39.      * @var string The hashed password
  40.      * @ORM\Column(type="string")
  41.      */
  42.     private $password;
  43.     
  44.     /**
  45.      * @ORM\Column(type="boolean", options={"default" : true})
  46.      */
  47.     private $isActive true;
  48.     public function getId(): ?int {
  49.         return $this->id;
  50.     }
  51.     /**
  52.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  53.      */
  54.     public function getUsername(): string {
  55.         return (string) $this->username;
  56.     }
  57.     public function setUsername(string $username): self {
  58.         $this->username $username;
  59.         return $this;
  60.     }
  61.     /**
  62.      * A visual identifier that represents this user.
  63.      *
  64.      * @see UserInterface
  65.      */
  66.     public function getUserIdentifier(): string {
  67.         return (string) $this->username;
  68.     }
  69.     /**
  70.      * @see UserInterface
  71.      */
  72.     public function getRoles(): array {
  73.         $roles $this->roles;
  74.         // guarantee every user at least has ROLE_USER
  75.         if(!in_array('ROLE_USER'$roles)) {
  76.             $roles[] = 'ROLE_USER';
  77.         }
  78.         return array_unique($roles);
  79.     }
  80.     public function setRoles(array $roles): self {
  81.         $roles[] = 'ROLE_USER';
  82.         $this->roles $roles;
  83.         return $this;
  84.     }
  85.     /**
  86.      * @see PasswordAuthenticatedUserInterface
  87.      */
  88.     public function getPassword(): string {
  89.         return $this->password;
  90.     }
  91.     public function setPassword(string $password): self {
  92.         $this->password $password;
  93.         return $this;
  94.     }
  95.     
  96.     public function setFullname($fullname) {
  97.         $this->fullname $fullname;
  98.     }
  99.     
  100.     public function getFullname() {
  101.         return $this->fullname;
  102.     }
  103.     public function setIsActive($isActive) {
  104.         $this->isActive $isActive;
  105.     }
  106.     
  107.     public function getIsActive() {
  108.         return $this->isActive;
  109.     }
  110.     
  111.     public function getStrIsActive() {
  112.         return $this->isActive == self::USER_IS_ACTIVE 'Activo' 'Inactivo';
  113.     }
  114.     /**
  115.      * Returning a salt is only needed, if you are not using a modern
  116.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  117.      *
  118.      * @see UserInterface
  119.      */
  120.     public function getSalt(): ?string {
  121.         return null;
  122.     }
  123.     /**
  124.      * @see UserInterface
  125.      */
  126.     public function eraseCredentials() {
  127.         // If you store any temporary, sensitive data on the user, clear it here
  128.         // $this->plainPassword = null;
  129.     }
  130.     public function __toString() {
  131.         return $this->getFullname()? : '-';
  132.     }
  133.     public function getUserRoles() {
  134.         $arrRoles = [];
  135.         foreach($this->getRoles() as $role) {
  136.             
  137.             switch ($role) {
  138.                 case self::USER_ROLE_USER:
  139.                     $arrRoles[] = "Usuario";
  140.                     break;
  141.                 case self::USER_ROLE_ADMIN:
  142.                     $arrRoles[] = "Administrador";
  143.                     break;
  144.                 case self::USER_ROLE_TEACHER:
  145.                     $arrRoles[] = "Profesor";
  146.                     break;
  147.                 case self::USER_ROLE_STUDENT:
  148.                     $arrRoles[] = "Studiante";
  149.                     break;
  150.                 case self::USER_ROLE_ADMIN_AUX:
  151.                     $arrRoles[] = "Recepcionista";
  152.                     break;
  153.                 default:
  154.                     break;
  155.             }
  156.         }
  157.         return implode(', '$arrRoles);
  158.     }
  159.     
  160.     
  161. }