src/Security/Voter/ProductVoter.php line 12

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