src/AppBundle/Security/FeatureVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Security;
  3. use AppBundle\Entity\User;
  4. use AppBundle\Enum\FeatureAccessEnum;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. /**
  8.  * Check if the current use can access certain profile-based features
  9.  */
  10. class FeatureVoter extends Voter
  11. {
  12.     protected function supports($attribute$subject): bool
  13.     {
  14.         return \in_array($attributeFeatureAccessEnum::getChoices(), true);
  15.     }
  16.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  17.     {
  18.         $user $token->getUser();
  19.         if (!$user instanceof User) {
  20.             return false;
  21.         }
  22.         return $user->hasRole($attribute);
  23.     }
  24. }