<?php
namespace App\Security;
use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
/**
* メニューアクセス権限所持チェック
*
*/
class MenuVoter extends Voter
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports(string $attribute, $subject)
{
return strpos(strtoupper($attribute), "MENU_") !== false;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token):bool
{
if($this->security->isGranted('ROLE_ADMIN'))
{
return true;
}
if(in_array(preg_replace("/^MENU_/", "", $attribute), $token->getAttribute('menu'))) return true;
return false;
}
}