<?php
namespace App\Security\Voters;
use App\Service\SystemServices\ISystemAccessService;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
class UserSystemAccessVoter extends Voter
{
private $security;
private $systemService;
const SUPER_ADMIN_CONTROLLER_ACCESS = 'FLEET_MGT_CONTROLLER_ACCESS';
/**
* @param Security $security
* @param ISystemAccessService $systemService
*/
public function __construct(Security $security, ISystemAccessService $systemService)
{
$this->security = $security;
$this->systemService = $systemService;
}
protected function supports(string $attribute, $subject): bool
{
if ($attribute != self::SUPER_ADMIN_CONTROLLER_ACCESS) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface || empty($subject)) {
return false;
}
if (!$this->security->isGranted($user->getRole()->getMetaCode())) {
return false;
}
/**
* super system access level check
*/
return $this->systemService->subSystemModuleAccessValidation($user, $subject);
//todo generate access log
}
}