<?php
namespace App\Voters;
use App\Model\Module\Module;
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;
class ModuleVoter extends Voter
{
/**
* @param $attribute
* @param $subject
*
* @return bool
*/
protected function supports($attribute, $subject): bool
{
return in_array($attribute, [
Module::MODULE_PRODUCT,
Module::MODULE_QUOTE_LINE,
Module::MODULE_PRODUCT_CUSTOMIZE,
], true);
}
/**
* @param $attribute
* @param $subject
* @param TokenInterface $token
*
* @return bool
*
* @throws LogicException
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
switch ($attribute) {
case SecurityConfig::MODULE_QUOTE_LINE:
case SecurityConfig::MODULE_PRODUCT:
case SecurityConfig::MODULE_PRODUCT_CUSTOMIZE:
return in_array($attribute, $user->getRoles(), true);
}
throw new LogicException('This should never happen');
}
}