<?php
namespace AppBundle\Security;
use AppBundle\Entity\User;
use AppBundle\Enum\FeatureAccessEnum;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
* Check if the current use can access certain profile-based features
*/
class FeatureVoter extends Voter
{
protected function supports($attribute, $subject): bool
{
return \in_array($attribute, FeatureAccessEnum::getChoices(), true);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
return $user->hasRole($attribute);
}
}