<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface {
const USER_ROLE_USER = 'ROLE_USER';
const USER_ROLE_ADMIN = 'ROLE_ADMIN';
const USER_ROLE_TEACHER = 'ROLE_TEACHER';
const USER_ROLE_STUDENT = 'ROLE_STUDENT';
const USER_ROLE_ADMIN_AUX = 'ROLE_ADMIN_AUX';
const USER_IS_ACTIVE = 'USER_ACTIVE';
const USER_IS_UNACTIVE = 'USER_UNACTIVE';
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180)
*/
private $fullname;
/**
* @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="boolean", options={"default" : true})
*/
private $isActive = true;
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
if(!in_array('ROLE_USER', $roles)) {
$roles[] = 'ROLE_USER';
}
return array_unique($roles);
}
public function setRoles(array $roles): self {
$roles[] = 'ROLE_USER';
$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;
}
public function setFullname($fullname) {
$this->fullname = $fullname;
}
public function getFullname() {
return $this->fullname;
}
public function setIsActive($isActive) {
$this->isActive = $isActive;
}
public function getIsActive() {
return $this->isActive;
}
public function getStrIsActive() {
return $this->isActive == self::USER_IS_ACTIVE ? 'Activo' : 'Inactivo';
}
/**
* 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 __toString() {
return $this->getFullname()? : '-';
}
public function getUserRoles() {
$arrRoles = [];
foreach($this->getRoles() as $role) {
switch ($role) {
case self::USER_ROLE_USER:
$arrRoles[] = "Usuario";
break;
case self::USER_ROLE_ADMIN:
$arrRoles[] = "Administrador";
break;
case self::USER_ROLE_TEACHER:
$arrRoles[] = "Profesor";
break;
case self::USER_ROLE_STUDENT:
$arrRoles[] = "Studiante";
break;
case self::USER_ROLE_ADMIN_AUX:
$arrRoles[] = "Recepcionista";
break;
default:
break;
}
}
return implode(', ', $arrRoles);
}
}