src/Security/Voter/MaterialVoter.php line 12

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\Material;
  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 MaterialVoter extends Voter
  9. {
  10.     public const VIEW 'view';
  11.     public const EDIT 'edit';
  12.     public const ARCHIVE 'archive';
  13.     public const DELETE 'delete';
  14.     public const INDIVIDUAL_PARAMETERS 'individual-parameters';
  15.     public const UPLOAD_FILES 'upload-files';
  16.     public const SHOW_FILES 'show-files';
  17.     /**
  18.      * @param string $attribute
  19.      * @param mixed  $subject
  20.      *
  21.      * @return bool
  22.      */
  23.     protected function supports($attribute$subject): bool
  24.     {
  25.         $supports true;
  26.         if (!\in_array($attribute, [
  27.             self::VIEW,
  28.             self::EDIT,
  29.             self::ARCHIVE,
  30.             self::DELETE,
  31.             self::INDIVIDUAL_PARAMETERS,
  32.             self::UPLOAD_FILES,
  33.             self::SHOW_FILES
  34.         ], true)) {
  35.             $supports false;
  36.         }
  37.         if (!$subject instanceof Material) {
  38.             $supports false;
  39.         }
  40.         return $supports;
  41.     }
  42.     /**
  43.      * @param string         $attribute
  44.      * @param mixed          $subject
  45.      * @param TokenInterface $token
  46.      *
  47.      * @return bool
  48.      */
  49.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  50.     {
  51.         $user $token->getUser();
  52.         $hasVotePassed false;
  53.         if (!$user instanceof User) {
  54.             return $hasVotePassed;
  55.         }
  56.         /** @var Material $material */
  57.         $material $subject;
  58.         switch ($attribute) {
  59.             case self::VIEW:
  60.                 $hasVotePassed =  true;
  61.                 break;
  62.             case self::EDIT:
  63.                 $hasVotePassed =  $this->canEdit($material$user);
  64.                 break;
  65.             case self::ARCHIVE:
  66.                 $hasVotePassed =  $this->canArchive($material$user);
  67.                 break;
  68.             case self::DELETE:
  69.                 $hasVotePassed =  $this->canDelete($user);
  70.                 break;
  71.             case self::INDIVIDUAL_PARAMETERS:
  72.                 $hasVotePassed =  $this->canIndividualParameters($material$user);
  73.                 break;
  74.             case self::UPLOAD_FILES:
  75.                 $hasVotePassed $this->canUploadFiles($material);
  76.                 break;
  77.             case self::SHOW_FILES:
  78.                 $hasVotePassed $this->canShowFiles();
  79.                 break;
  80.         }
  81.         return $hasVotePassed;
  82.     }
  83.     /**
  84.      * @param Material $material
  85.      * @param User     $user
  86.      *
  87.      * @return bool
  88.      */
  89.     private function canEdit(Material $materialUser $user): bool
  90.     {
  91.          return $user->isSystemUser() && $material->isActive();
  92.     }
  93.     /**
  94.      * @param Material $material
  95.      * @param User     $user
  96.      *
  97.      * @return bool
  98.      */
  99.     private function canArchive(Material $materialUser $user): bool
  100.     {
  101.         return $user->isSystemUser() && $material->isActive();
  102.     }
  103.     /**
  104.      * @param User     $user
  105.      *
  106.      * @return bool
  107.      */
  108.     private function canDelete(User $user): bool
  109.     {
  110.         return $user->isSystemUser();
  111.     }
  112.     /**
  113.      * @param Material $material
  114.      * @param User     $user
  115.      *
  116.      * @return bool
  117.      */
  118.     private function canIndividualParameters(Material $materialUser $user): bool
  119.     {
  120.         return (!$user->isSystemUser()) && $material->isActive();
  121.     }
  122.     /**
  123.      * @param Material $material
  124.      *
  125.      * @return bool
  126.      */
  127.     private function canUploadFiles(Material $material): bool
  128.     {
  129.         return $material->isActive();
  130.     }
  131.     /**
  132.      * @return bool
  133.      */
  134.     private function canShowFiles(): bool
  135.     {
  136.         return true;
  137.     }
  138. }