SPRA-2 add admin; add user list; add user; change permissions
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Enum\UserPermission;
|
||||
use App\Repository\UserRepository;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
@@ -31,6 +33,12 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
|
||||
#[ORM\Column]
|
||||
private ?string $password = null;
|
||||
|
||||
#[ORM\Column(length: 255, nullable: true)]
|
||||
private ?string $activationHash = null;
|
||||
|
||||
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
|
||||
private ?\DateTimeInterface $dateActivationHash = null;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
@@ -82,6 +90,33 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addRoles(string $role): void
|
||||
{
|
||||
if ($role === 'ROLE_USER') {
|
||||
return;
|
||||
}
|
||||
$roles = $this->getRoles();
|
||||
$roles[] = $role;
|
||||
|
||||
$this->roles = $roles;
|
||||
}
|
||||
|
||||
public function removeRoles(string $roleRemove): void
|
||||
{
|
||||
if ($roleRemove === 'ROLE_USER') {
|
||||
return;
|
||||
}
|
||||
$roles = $this->getRoles();
|
||||
foreach ($roles as $key => $role) {
|
||||
if ($role === $roleRemove) {
|
||||
unset($roles[$key]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
$roles = array_values($roles);
|
||||
$this->setRoles($roles);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see PasswordAuthenticatedUserInterface
|
||||
*/
|
||||
@@ -105,4 +140,42 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
|
||||
// If you store any temporary, sensitive data on the user, clear it here
|
||||
// $this->plainPassword = null;
|
||||
}
|
||||
|
||||
public function getEnumRoles(): array
|
||||
{
|
||||
$tmp=[];
|
||||
foreach ($this->getRoles() as $role) {
|
||||
$tmp[]=UserPermission::from($role);
|
||||
}
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
public function isGranted(UserPermission $permission): bool
|
||||
{
|
||||
return in_array($permission, $this->getEnumRoles(), true);
|
||||
}
|
||||
|
||||
public function getActivationHash(): ?string
|
||||
{
|
||||
return $this->activationHash;
|
||||
}
|
||||
|
||||
public function setActivationHash(?string $activationHash): static
|
||||
{
|
||||
$this->activationHash = $activationHash;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDateActivationHash(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->dateActivationHash;
|
||||
}
|
||||
|
||||
public function setDateActivationHash(?\DateTimeInterface $dateActivationHash): static
|
||||
{
|
||||
$this->dateActivationHash = $dateActivationHash;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user