src/Security/Voter/OrganizationVoter.php line 12

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\Organization;
  5. use App\Entity\User;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class OrganizationVoter extends Voter
  9. {
  10.     public const VIEW 'view';
  11.     public const EDIT 'edit';
  12.     public const DELETE 'delete';
  13.     public const BLOCK 'block';
  14.     public const ACTIVE 'active';
  15.     /**
  16.      * @param string $attribute
  17.      * @param mixed  $subject
  18.      *
  19.      * @return bool
  20.      */
  21.     protected function supports($attribute$subject): bool
  22.     {
  23.         $supports true;
  24.         if (!\in_array($attribute, [self::VIEWself::EDITself::DELETEself::BLOCKself::ACTIVE], true)) {
  25.             $supports false;
  26.         }
  27.         if (!$subject instanceof Organization) {
  28.             $supports false;
  29.         }
  30.         return $supports;
  31.     }
  32.     /**
  33.      * @param string         $attribute
  34.      * @param mixed          $subject
  35.      * @param TokenInterface $token
  36.      *
  37.      * @return bool
  38.      */
  39.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  40.     {
  41.         $user $token->getUser();
  42.         $hasVotePassed false;
  43.         if (($user instanceof User) && $user->isSystemUser()) {
  44.             $hasVotePassed true;
  45.         }
  46.         return $hasVotePassed;
  47.     }
  48. }