<?php
namespace App\Http\Admin\Security;
use App\Domain\Auth\Entity\User;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class AdminVoter extends Voter
{
private $security;
public function __construct(
Security $security
)
{
$this->security = $security;
}
protected function supports(string $attribute, $subject)
{
if(is_null($attribute) || $attribute == '') return false;
if(strpos($attribute, 'ROLE_') !== false ) return false;
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
if($user->hasRole('ROLE_ADMIN') && $subject != 'only') return true;
$rights = $user->getModulesRightSlug();
foreach($rights as $r) {
if($attribute == $r) return true;
}
return false;
}
}