User::isInRole nefunguje s IRole
- Kori
- Člen | 73
Ahoj,
existuje nejaky duvod proc metoda User::isInRole()
nepracuje s rozhranim IRole?
Ted jsem na to po delsi dobe opet narazil. Drive to slo vyresit vlastni implementaci metody isInRole(), ale ta je ted z nejakeho duvodu od v.3 final
Priklad:
class AdminRole implements \Nette\Security\IRole
{
public function getRoleId(): string
{
return 'admin';
}
}
class Authenticator implements Nette\Security\IAuthenticator
{
public function authenticate(array $credentials): Nette\Security\IIdentity
{
.....
$roles[] = new AdminRole();
return new Nette\Security\Identity($id, $roles, $data);
}
}
$this->user->isInRole('admin'); //false
Jednoducha implementace by mohla vypadat nejak takhle
class User extends \Nette\Security\User
{
public function isInRole($role): bool
{
$roles = [];
foreach ($this->getRoles() as $role)
{
if ($role instanceof \Nette\Security\IRole) {
$roles[] = $role->getRoleId();
} else {
$roles[] = $role;
}
}
return in_array($role, $roles, true);
}
}
Diky za info