src/Security/Voter/FeatureVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Constant\Features;
  4. use App\Entity\User;
  5. use App\Exception\FeatureNotAvailableException;
  6. use App\Service\Api\Subscription\FeatureService;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class FeatureVoter extends Voter
  10. {
  11.     private $featureService;
  12.     public function __construct(FeatureService $featureService)
  13.     {
  14.         $this->featureService $featureService;
  15.     }
  16.     protected function supports(string $attribute$subject): bool
  17.     {
  18.         // Check if the attribute is a feature name or role
  19.         return (Features::findByName($attribute) || Features::findByRole($attribute)) !== null;
  20.     }
  21.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  22.     {
  23.         $user $token->getUser();
  24.         if (!$user instanceof User) {
  25.             return false;
  26.         }
  27.         // Get feature data
  28.         $feature Features::findByName($attribute) ?? Features::findByRole($attribute);
  29.         if (!$feature) {
  30.             return false;
  31.         }
  32.         // Check feature access and limits using FeatureService
  33.         if (!$this->featureService->hasAccess($user$attribute)) {
  34.             throw new FeatureNotAvailableException($attribute);
  35.         }
  36.         return true;
  37.     }
  38. }