src/Security/Voter/FrameworkProcessVoter.php line 12

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