src/Security/Voter/AdminOrganizationVoter.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\Admin;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class AdminOrganizationVoter 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 Admin) {
  44.             return $hasVotePassed;
  45.         }
  46.         /** @var Organization $entity */
  47.         $entity $subject;
  48.         switch ($attribute) {
  49.             case self::VIEW:
  50.                 $hasVotePassed =  $this->canView($entity);
  51.                 break;
  52.             case self::EDIT:
  53.                 $hasVotePassed =  $this->canEdit($entity);
  54.                 break;
  55.             case self::BLOCK:
  56.                 $hasVotePassed =  $this->canBlock($entity);
  57.                 break;
  58.             case self::DELETE:
  59.                 $hasVotePassed =  $this->canDelete($entity);
  60.                 break;
  61.             case self::ACTIVE:
  62.                 $hasVotePassed =  $this->canActivate($entity);
  63.                 break;
  64.         }
  65.         return $hasVotePassed;
  66.     }
  67.     /**
  68.      * @param Organization $entity
  69.      *
  70.      * @return bool
  71.      */
  72.     private function canView(Organization $entity): bool
  73.     {
  74.         // TODO Wykonać dostępy na podstawie roli użytkownika
  75.         // Admin i Projekt widzi i może robić wszystko
  76.         // Default/użytkownik może widzieć element i detale
  77.         return true;
  78.     }
  79.     /**
  80.      * @param Organization $entity
  81.      *
  82.      * @return bool
  83.      */
  84.     private function canEdit(Organization $entity): bool
  85.     {
  86.         // TODO Wykonać dostępy na podstawie roli użytkownika
  87.         // Admin i Projekt widzi i może robić wszystko
  88.         // Default/użytkownik może widzieć element i detale
  89.          return true;
  90.     }
  91.     /**
  92.      * @param Organization $entity
  93.      *
  94.      * @return bool
  95.      */
  96.     private function canBlock(Organization $entity): bool
  97.     {
  98.         // TODO Wykonać dostępy na podstawie roli użytkownika
  99.         // Admin i Projekt widzi i może robić wszystko
  100.         // Default/użytkownik może widzieć element i detale
  101.         return true;
  102.     }
  103.     /**
  104.      * @param Organization $entity
  105.      *
  106.      * @return bool
  107.      */
  108.     private function canDelete(Organization $entity): bool
  109.     {
  110.         // TODO Wykonać dostępy na podstawie roli użytkownika
  111.         // Admin i Projekt widzi i może robić wszystko
  112.         // Default/użytkownik może widzieć element i detale
  113.         return true;
  114.     }
  115.     /**
  116.      * @param Organization $entity
  117.      *
  118.      * @return bool
  119.      */
  120.     private function canActivate(Organization $entity): bool
  121.     {
  122.         return true;
  123.     }
  124. }