src/Security/Voter/AdminUserVoter.php line 12

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\User;
  5. use App\Entity\Admin;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class AdminUserVoter extends Voter
  9. {
  10.     public const VIEW 'view';
  11.     public const EDIT 'edit';
  12.     public const PASSWORD_CHANGE 'password-change';
  13.     public const DELETE 'delete';
  14.     public const BLOCK 'block';
  15.     public const ACTIVE 'active';
  16.     /**
  17.      * @param string $attribute
  18.      * @param mixed  $subject
  19.      *
  20.      * @return bool
  21.      */
  22.     protected function supports($attribute$subject): bool
  23.     {
  24.         $supports true;
  25.         if (!\in_array($attribute, [self::VIEWself::EDITself::PASSWORD_CHANGEself::DELETEself::BLOCKself::ACTIVE], true)) {
  26.             $supports false;
  27.         }
  28.         if (!$subject instanceof User) {
  29.             $supports false;
  30.         }
  31.         return $supports;
  32.     }
  33.     /**
  34.      * @param string         $attribute
  35.      * @param mixed          $subject
  36.      * @param TokenInterface $token
  37.      *
  38.      * @return bool
  39.      */
  40.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  41.     {
  42.         $user $token->getUser();
  43.         $hasVotePassed false;
  44.         if (!$user instanceof Admin) {
  45.             return $hasVotePassed;
  46.         }
  47.         /** @var User $entity */
  48.         $entity $subject;
  49.         switch ($attribute) {
  50.             case self::VIEW:
  51.                 $hasVotePassed =  $this->canView($entity);
  52.                 break;
  53.             case self::EDIT:
  54.             case self::PASSWORD_CHANGE:
  55.                 $hasVotePassed =  $this->canEdit($entity);
  56.                 break;
  57.             case self::BLOCK:
  58.                 $hasVotePassed =  $this->canBlock($entity);
  59.                 break;
  60.             case self::DELETE:
  61.                 $hasVotePassed =  $this->canDelete($entity);
  62.                 break;
  63.             case self::ACTIVE:
  64.                 $hasVotePassed =  $this->canActivate($entity);
  65.                 break;
  66.         }
  67.         return $hasVotePassed;
  68.     }
  69.     /**
  70.      * @param User $entity
  71.      *
  72.      * @return bool
  73.      */
  74.     private function canView(User $entity): bool
  75.     {
  76.         // TODO Wykonać dostępy na podstawie roli użytkownika
  77.         // Admin i Projekt widzi i może robić wszystko
  78.         // Default/użytkownik może widzieć element i detale
  79.         return true;
  80.     }
  81.     /**
  82.      * @param User $entity
  83.      *
  84.      * @return bool
  85.      */
  86.     private function canEdit(User $entity): bool
  87.     {
  88.         // TODO Wykonać dostępy na podstawie roli użytkownika
  89.         // Admin i Projekt widzi i może robić wszystko
  90.         // Default/użytkownik może widzieć element i detale
  91.          return true;
  92.     }
  93.     /**
  94.      * @param User $entity
  95.      *
  96.      * @return bool
  97.      */
  98.     private function canBlock(User $entity): bool
  99.     {
  100.         // TODO Wykonać dostępy na podstawie roli użytkownika
  101.         // Admin i Projekt widzi i może robić wszystko
  102.         // Default/użytkownik może widzieć element i detale
  103.         return true;
  104.     }
  105.     /**
  106.      * @param User $entity
  107.      *
  108.      * @return bool
  109.      */
  110.     private function canDelete(User $entity): bool
  111.     {
  112.         // TODO Wykonać dostępy na podstawie roli użytkownika
  113.         // Admin i Projekt widzi i może robić wszystko
  114.         // Default/użytkownik może widzieć element i detale
  115.         return true;
  116.     }
  117.     /**
  118.      * @param User $entity
  119.      *
  120.      * @return bool
  121.      */
  122.     private function canActivate(User $entity): bool
  123.     {
  124.         return true;
  125.     }
  126. }