<?php
namespace App\Security\Voters;
use App\Entity\Manufacturer;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class MessageVoter extends Voter
{
private Security $security;
public const CAN_SEND = 'message.can_send';
private const ATTRIBUTES = [
self::CAN_SEND,
];
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject): bool
{
return in_array($attribute, self::ATTRIBUTES);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
switch ($attribute) {
case self::CAN_SEND:
return $this->canSend();
}
throw new \LogicException('Invalid attribute: '.$attribute);
}
private function canSend(): bool
{
return $this->security->isGranted('ROLE_MODULE_USERS');
}
}