<?php
namespace App\Security;
use App\Entity\User;
use LogicException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class JobProfileVoter extends Voter
{
public const VIEW_OVERVIEW = 'view_job_profile_overview';
protected ParameterBagInterface $parameterBag;
/**
* JobProfileVoter constructor.
*/
public function __construct(ParameterBagInterface $parameterBag)
{
$this->parameterBag = $parameterBag;
}
protected function supports($attribute, $subject)
{
return \in_array($attribute, [
self::VIEW_OVERVIEW,
], true);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
switch ($attribute) {
case self::VIEW_OVERVIEW:
return $this->canViewOverview($token);
}
throw new LogicException('This code may never be reached');
}
private function canViewOverview(TokenInterface $token)
{
if ($this->parameterBag->get('mobility_public_profiles')) {
return true;
}
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
return \in_array('ROLE_SITE_MOBILITY_USER', $user->getRoles(), true);
}
}