src/Security/Voter/FeatureVoter.php line 11

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