src/Security/Voter/SponsorPermissionVoter.php line 11

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\Sponsor;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class SponsorPermissionVoter extends Voter
  8. {
  9.     public const FULL 'full';
  10.     public const FULL_OR_SIMPLE 'full_or_simple';
  11.     public const SIMPLE 'simple';
  12.     public const RESTRICTED 'restricted';
  13.     private const LIST = [
  14.         self::FULL,
  15.         self::FULL_OR_SIMPLE,
  16.         self::SIMPLE,
  17.         self::RESTRICTED,
  18.     ];
  19.     protected function supports(string $attribute$subject): bool
  20.     {
  21.         return in_array($attributeself::LIST, true) && $subject instanceof Sponsor;
  22.     }
  23.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  24.     {
  25.         $user $token->getUser();
  26.         if (!$user instanceof Sponsor) {
  27.             return false;
  28.         }
  29.         if ($attribute === self::FULL_OR_SIMPLE) {
  30.             return $user->hasFullPermission() || $user->hasSimplePermission();
  31.         }
  32.         return $user->hasPermission($attribute);
  33.     }
  34. }