src/Security/Voter/ResourceAccessVoter.php line 11
<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authorization\Voter\Voter,
Symfony\Component\Security\Core\Role\RoleHierarchyInterface,
Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use App\Lib\Roles,
App\Entity\Admin;
final class ResourceAccessVoter extends Voter
{
private RoleHierarchyInterface $roleHierarchy;
public function __construct(RoleHierarchyInterface $roleHierarchy)
{
$this->roleHierarchy = $roleHierarchy;
}
protected function supports($attribute, $subject): bool
{
if ($attribute && str_starts_with($attribute, 'App')) {
return true;
}
return false;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
/** @var Admin */
$admin = $token->getUser();
if ($admin->getIsPasswordResetRequired()) {
return false;
}
$accessable = array_intersect(
$this->getRoles($admin),
[Roles::ROLE_SUPER_ADMIN, $attribute]
);
if (count($accessable) < 1) {
return false;
}
return true;
}
private function getRoles(Admin $admin): array
{
return $this->roleHierarchy->getReachableRoleNames($admin->getRoles());
}
}