src/Security/Voter/ToolVoter.php line 12

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\Tool;
  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 ToolVoter extends Voter
  9. {
  10.     public const VIEW 'view';
  11.     public const EDIT 'edit';
  12.     public const DELETE 'delete';
  13.     public const VIEW_PARAMETERS 'view-parameters';
  14.     public const EDIT_PARAMETERS 'edit-parameters';
  15.     public const ACCEPT_PARAMETERS 'accept-parameters';
  16.     public const INDIVIDUAL_PARAMETERS 'individual-parameters';
  17.     public const UPLOAD_FILES 'upload-files';
  18.     public const SHOW_FILES 'show-files';
  19.     /**
  20.      * @param string $attribute
  21.      * @param mixed  $subject
  22.      *
  23.      * @return bool
  24.      */
  25.     protected function supports($attribute$subject): bool
  26.     {
  27.         $supports true;
  28.         if (!\in_array($attribute, [
  29.             self::VIEW,
  30.             self::EDIT,
  31.             self::VIEW_PARAMETERS,
  32.             self::EDIT_PARAMETERS,
  33.             self::ACCEPT_PARAMETERS,
  34.             self::DELETE,
  35.             self::INDIVIDUAL_PARAMETERS,
  36.             self::UPLOAD_FILES,
  37.             self::SHOW_FILES
  38.         ], true)) {
  39.             $supports false;
  40.         }
  41.         if (!$subject instanceof Tool) {
  42.             $supports false;
  43.         }
  44.         return $supports;
  45.     }
  46.     /**
  47.      * @param string         $attribute
  48.      * @param mixed          $subject
  49.      * @param TokenInterface $token
  50.      *
  51.      * @return bool
  52.      */
  53.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  54.     {
  55.         $user $token->getUser();
  56.         $hasVotePassed false;
  57.         if (!$user instanceof User) {
  58.             return $hasVotePassed;
  59.         }
  60.         /** @var Tool $tool */
  61.         $tool $subject;
  62.         switch ($attribute) {
  63.             case self::VIEW:
  64.             case self::VIEW_PARAMETERS:
  65.                 $hasVotePassed =  true;
  66.                 break;
  67.             case self::EDIT:
  68.                 $hasVotePassed $this->canEdit($tool$user);
  69.                 break;
  70.             case self::EDIT_PARAMETERS:
  71.                 $hasVotePassed $this->canEditParameters($tool$user);
  72.                 break;
  73.             case self::ACCEPT_PARAMETERS:
  74.                 $hasVotePassed $this->canAcceptParameters($tool$user);
  75.                 break;
  76.             case self::DELETE:
  77.                 $hasVotePassed $this->canDelete($tool);
  78.                 break;
  79.             case self::INDIVIDUAL_PARAMETERS:
  80.                 $hasVotePassed $this->canIndividualParameters($tool$user);
  81.                 break;
  82.             case self::UPLOAD_FILES:
  83.                 $hasVotePassed $this->canUploadFiles($tool$user);
  84.                 break;
  85.             case self::SHOW_FILES:
  86.                 $hasVotePassed $this->canShowFiles($tool$user);
  87.                 break;
  88.         }
  89.         return $hasVotePassed;
  90.     }
  91.     /**
  92.      * @param Tool $tool
  93.      * @param User $user
  94.      *
  95.      * @return bool
  96.      */
  97.     private function canEdit(Tool $toolUser $user): bool
  98.     {
  99.         if ($user->isSystemUser()) {
  100.             $canUserEdit = (null === $tool->getCreatedBy()) || $tool->getCreatedBy()->isSystemUser();
  101.         } else {
  102.             $canUserEdit $tool->isInUserOrganization($user);
  103.         }
  104.         return $canUserEdit;
  105.     }
  106.     /**
  107.      * @param Tool $tool
  108.      * @param User $user
  109.      *
  110.      * @return bool
  111.      */
  112.     private function canEditParameters(Tool $toolUser $user): bool
  113.     {
  114.         return $this->canEdit($tool$user)
  115.             && ($tool->isStatusNewWithoutParameters() || $tool->isStatusNewWithParameters() || $tool->isStatusActive());
  116.     }
  117.     /**
  118.      * @param Tool $tool
  119.      * @param User $user
  120.      *
  121.      * @return bool
  122.      */
  123.     private function canAcceptParameters(Tool $toolUser $user): bool
  124.     {
  125.         return $this->canEdit($tool$user) && ($tool->isStatusNewWithParameters());
  126.     }
  127.     /**
  128.      * @param Tool $tool
  129.      *
  130.      * @return bool
  131.      */
  132.     private function canDelete(Tool $tool): bool
  133.     {
  134.         // TODO Wykonać dostępy na podstawie roli użytkownika
  135.         // Admin i Projekt widzi i może robić wszystko
  136.         // Default/użytkownik może widzieć element i detale
  137.         return true;
  138.     }
  139.     /**
  140.      * @param Tool $tool
  141.      * @param User $user
  142.      *
  143.      * @return bool
  144.      */
  145.     private function canIndividualParameters(Tool $toolUser $user): bool
  146.     {
  147.         return (!$user->isSystemUser()) && $tool->isInUserOrganization($user);
  148.     }
  149.     /**
  150.      * @param Tool $tool
  151.      * @param User $user
  152.      *
  153.      * @return bool
  154.      */
  155.     private function canUploadFiles(Tool $toolUser $user): bool
  156.     {
  157.         return ($tool->isInUserOrganization($user) || $tool->isSystemTool()) && $tool->isStatusActive();
  158.     }
  159.     /**
  160.      * @param Tool $tool
  161.      * @param User $user
  162.      *
  163.      * @return bool
  164.      */
  165.     private function canShowFiles(Tool $toolUser $user): bool
  166.     {
  167.         return $tool->isInUserOrganization($user) || $tool->isSystemTool();
  168.     }
  169. }