<?php
declare(strict_types = 1);
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
use App\Model\Constants\Modules;
use App\Entity\Dealers;
use App\Security\ACLManager;
class DealersVoter extends Voter {
private $acl;
public function __construct(ACLManager $acl) {
$this->acl = $acl;
}
protected function supports($attribute, $subject) {
//replace with your own logic
//https://symfony.com/doc/current/security/voters.html
return in_array($attribute, ['GET_DEALER']) && $subject instanceof Dealers;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) {
$user = $token->getUser();
//if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
// ... (check conditions and return true to grant permission) ...
switch ($attribute) {
case 'GET_DEALER':
//get access from database
$hasAccess = $this->acl->hasAccessToModule($user, Modules::DEALERS);
if ($hasAccess) {
return true;
}
return false;
break;
}
throw new \Exception(sprintf('Unhandled attribute "%s"', $attribute));
}
}