<?php
namespace App\Voter;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class MarketingVoter extends Voter
{
const SEGMENT_LIST = "MARKETING_SEGMENT_LIST";
const SEGMENT_CREATE = "MARKETING_SEGMENT_CREATE";
const SEGMENT_UPDATE = "MARKETING_SEGMENT_UPDATE";
const SEGMENT_REMOVE = "MARKETING_SEGMENT_REMOVE";
const SEGMENT_EXPORT_CONTACTS = "MARKETING_SEGMENT_EXPORT_CONTACTS";
protected ParameterBagInterface $parameterBag;
public function __construct(ParameterBagInterface $parameterBag)
{
$this->parameterBag = $parameterBag;
}
protected function supports($attribute, $subject): bool
{
return in_array($attribute, [
self::SEGMENT_LIST,
self::SEGMENT_CREATE,
self::SEGMENT_UPDATE,
self::SEGMENT_REMOVE,
self::SEGMENT_EXPORT_CONTACTS
]);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$loggedUser = $token->getUser();
if (!$loggedUser instanceof UserInterface) {
return false;
}
return in_array($attribute, $loggedUser->getRoles());
}
}