src/Security/Voter/ProcessVoter.php line 12

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\Process;
  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 ProcessVoter extends Voter
  9. {
  10.     public const VIEW 'view';
  11.     public const EDIT 'edit';
  12.     public const DELETE 'delete';
  13.     /**
  14.      * @param string $attribute
  15.      * @param mixed  $subject
  16.      *
  17.      * @return bool
  18.      */
  19.     protected function supports($attribute$subject): bool
  20.     {
  21.         $supports true;
  22.         if (!\in_array($attribute, [self::VIEWself::EDITself::DELETE], true)) {
  23.             $supports false;
  24.         }
  25.         if (!$subject instanceof Process) {
  26.             $supports false;
  27.         }
  28.         return $supports;
  29.     }
  30.     /**
  31.      * @param string         $attribute
  32.      * @param mixed          $subject
  33.      * @param TokenInterface $token
  34.      *
  35.      * @return bool
  36.      */
  37.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  38.     {
  39.         $user $token->getUser();
  40.         $hasVotePassed false;
  41.         if (!$user instanceof User) {
  42.             return $hasVotePassed;
  43.         }
  44.         switch ($attribute) {
  45.             case self::VIEW:
  46.                 $hasVotePassed $this->canShow($user$subject);
  47.                 break;
  48.             case self::EDIT:
  49.                 $hasVotePassed $this->canEdit($user);
  50.                 break;
  51.             case self::DELETE:
  52.                 $hasVotePassed $this->canDelete($user);
  53.                 break;
  54.         }
  55.         return $hasVotePassed;
  56.     }
  57.     private function canShow(User $userProcess $process): bool
  58.     {
  59.         if ($user->isSystemUser()) {
  60.             return true;
  61.         }
  62.         return $process->isActive();
  63.     }
  64.     /**
  65.      * @param User $user
  66.      *
  67.      * @return bool
  68.      */
  69.     private function canEdit(User $user): bool
  70.     {
  71.          return $user->isSystemUser();
  72.     }
  73.     /**
  74.      * @param User $user
  75.      *
  76.      * @return bool
  77.      */
  78.     private function canDelete(User $user): bool
  79.     {
  80.         return $user->isSystemUser();
  81.     }
  82. }