<?php
namespace App\Security\Voter;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class AllowContestTrainerRoutesVoter extends Voter
{
private $requestStack;
private $params;
public function __construct(RequestStack $requestStack, ParameterBagInterface $params)
{
$this->requestStack = $requestStack;
$this->params = $params;
}
protected function supports($attribute, $subject): bool
{
// if the attribute isn't one we support, return false
if ($attribute != 'ALLOW_CONTEST_TRAINER_ROUTES') {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
$request = $this->requestStack->getCurrentRequest();
$routeName = $request->attributes->get('_route');
if (!empty($routeName) && $this->params->get('allow.contest.trainer.routes') != '') {
$allows = array_map('trim', explode(',', $this->params->get('allow.contest.trainer.routes')));
if ($user->getCompanyType() == 'Contest' && in_array('ROLE_CLOUD_TRAINER', $user->getRoles()) && !in_array($routeName, $allows)) {
return false;
}
}
return true;
}
}