<?php
declare(strict_types = 1);
namespace App\Security\Voter;
use App\Entity\Campaigns;
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\Security\ACLManager;
class CampaignsVoter 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, ['EDIT_CAMPAIGN', 'GET_CAMPAIGN']) && $subject instanceof Campaigns;
}
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 'EDIT_CAMPAIGN':
case 'GET_CAMPAIGN':
//get access from database
$hasAccess = $this->acl->hasAccessToModule($user, Modules::COOP_REQUESTS);
if ($hasAccess) {
return true;
}
return false;
break;
}
throw new \Exception(sprintf('Unhandled attribute "%s"', $attribute));
}
}