<?php
namespace App\Voters;
use App\Security\SecurityConfig;
use App\Security\User;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class CampaignVoter extends Voter
{
const CAMPAIGN_CREATE = 'campaign_create';
/**
* @var Security
*/
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject): bool
{
return in_array($attribute, [
self::CAMPAIGN_CREATE,
], true);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
switch ($attribute) {
case self::CAMPAIGN_CREATE:
return $this->canCreateCampaigns();
}
throw new LogicException('This should never happen');
}
private function canAccessModuleCampaign(): bool
{
return $this->security->isGranted(SecurityConfig::MODULE_CAMPAIGN);
}
private function canCreateCampaigns(): bool
{
return $this->canAccessModuleCampaign();
}
}