<?php
namespace App\Security\Voter;
use App\Entity\Company;
use App\Entity\User;
use App\Service\Utils\Constante\AbonnementConst;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class FeaturesVoter extends Voter
{
public const FEAT_SECOURS= 'FEAT_SECOURS_ACCESS';
protected function supports(string $attribute, $subject)
{
return in_array($attribute, [self::FEAT_SECOURS]);
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
{
//utilisateur connecté
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
switch ($attribute) {
case self::FEAT_SECOURS:
return $this->canShowSecours($user);
}
return false;
}
private function canShowSecours(UserInterface $userTarget): bool
{
if(in_array(AbonnementConst::FEAT_SECOURS,$userTarget->getCompany()->getFeatures())){
return true;
}
return false;
}
}