src/DocumentWorkflow/Security/DocumentWorkflowVoter.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\DocumentWorkflow\Security;
  3. use App\DocumentWorkflow\Entity\DocumentWorkflow;
  4. use App\DocumentWorkflow\Enum\DocumentWorkflowStatusEnum;
  5. use App\DocumentWorkflow\Repository\DocumentWorkflowStepRepository;
  6. use AppBundle\Entity\User;
  7. use AppBundle\Enum\FeatureAccessEnum;
  8. use InvalidArgumentException;
  9. use SettingsBundle\Enum\ModuleEnum;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. /**
  14.  * Class DocumentWorkflowVoter
  15.  */
  16. class DocumentWorkflowVoter extends Voter
  17. {
  18.     public const COMMENT 'comment';
  19.     public const DELETE 'delete';
  20.     public const DOWNLOAD_DRIVE_FILE 'download_drive_file';
  21.     public const EDIT 'edit';
  22.     public const REVISE 'revise';
  23.     public const SEE_REVISE_BUTTON_IN_VIEW 'see_revise_button_in_view';
  24.     public const SEE_VALIDATE_BUTTON_IN_VIEW 'see_validate_button_in_view';
  25.     public const VALIDATE 'validate';
  26.     public const VIEW 'view';
  27.     /**
  28.      * @var DocumentWorkflowStepRepository
  29.      */
  30.     private $documentWorkflowStepRepository;
  31.     /**
  32.      * @var AccessDecisionManagerInterface
  33.      */
  34.     private $decisionManager;
  35.     /**
  36.      * DocumentWorkflowVoter constructor.
  37.      * @param DocumentWorkflowStepRepository $documentWorkflowStepRepository
  38.      * @param AccessDecisionManagerInterface $decisionManager
  39.      */
  40.     public function __construct(
  41.         DocumentWorkflowStepRepository $documentWorkflowStepRepository,
  42.         AccessDecisionManagerInterface $decisionManager
  43.     ) {
  44.         $this->documentWorkflowStepRepository $documentWorkflowStepRepository;
  45.         $this->decisionManager $decisionManager;
  46.     }
  47.     /**
  48.      * {@inheritdoc}
  49.      */
  50.     protected function supports($attribute$subject): bool
  51.     {
  52.         if (!in_array(
  53.             $attribute,
  54.             [
  55.                 self::COMMENT,
  56.                 self::DELETE,
  57.                 self::DOWNLOAD_DRIVE_FILE,
  58.                 self::EDIT,
  59.                 self::REVISE,
  60.                 self::SEE_REVISE_BUTTON_IN_VIEW,
  61.                 self::SEE_VALIDATE_BUTTON_IN_VIEW,
  62.                 self::VALIDATE,
  63.                 self::VIEW,
  64.             ]
  65.         )) {
  66.             return false;
  67.         }
  68.         if (!$subject instanceof DocumentWorkflow) {
  69.             return false;
  70.         }
  71.         return true;
  72.     }
  73.     /**
  74.      * {@inheritdoc}
  75.      * @param DocumentWorkflow $documentWorkflow
  76.      */
  77.     protected function voteOnAttribute($attribute$documentWorkflowTokenInterface $token)
  78.     {
  79.         $user $token->getUser();
  80.         if (!$user instanceof User) {
  81.             return false;
  82.         }
  83.         if (!$user->getLawFirm()->hasModule(ModuleEnum::MODULE_DOCUMENT_WORKFLOW)) {
  84.             return false;
  85.         }
  86.         $isAllAccess $this->decisionManager->decide($token, [FeatureAccessEnum::ALL_DOCUMENT_WORKFLOWS_ACCESS]);
  87.         switch ($attribute) {
  88.             case self::DELETE:
  89.             case self::EDIT:
  90.                 return $this->canEdit($documentWorkflow$user$isAllAccess);
  91.             case self::VIEW:
  92.             case self::COMMENT:
  93.                 return $this->canView($documentWorkflow$user$isAllAccess);
  94.             case self::DOWNLOAD_DRIVE_FILE:
  95.                 return $this->canDownloadDriveFile($documentWorkflow$user$isAllAccess);
  96.             case self::REVISE:
  97.                 return $this->canRevise($documentWorkflow$user);
  98.             case self::VALIDATE:
  99.                 return $this->canValidate($documentWorkflow$user);
  100.             case self::SEE_VALIDATE_BUTTON_IN_VIEW:
  101.                 return $this->canSeeValidateButtonInView($documentWorkflow$user);
  102.             case self::SEE_REVISE_BUTTON_IN_VIEW:
  103.                 return $this->canSeeReviseButtonInView($documentWorkflow$user);
  104.             default:
  105.                 throw new InvalidArgumentException('Invalid attribute');
  106.         }
  107.     }
  108.     /**
  109.      * @param DocumentWorkflow $documentWorkflow
  110.      * @param User $user
  111.      * @return bool
  112.      */
  113.     private function canSeeReviseButtonInView(DocumentWorkflow $documentWorkflowUser $user): bool
  114.     {
  115.         if (DocumentWorkflowStatusEnum::VALIDATED === $documentWorkflow->getStatus()) {
  116.             return false;
  117.         }
  118.         // Si le user est participant, hors créateur
  119.         if (count($this->documentWorkflowStepRepository->findByDocumentWorkflowAndUserWithoutCreator($documentWorkflow$user)) < 1) {
  120.             return false;
  121.         }
  122.         return true;
  123.     }
  124.     /**
  125.      * @param DocumentWorkflow $documentWorkflow
  126.      * @param User $user
  127.      * @return bool
  128.      */
  129.     private function canSeeValidateButtonInView(DocumentWorkflow $documentWorkflowUser $user): bool
  130.     {
  131.         if (DocumentWorkflowStatusEnum::VALIDATED === $documentWorkflow->getStatus()) {
  132.             return false;
  133.         }
  134.         // Si le user est participant, créateur compris
  135.         if (count($this->documentWorkflowStepRepository->findByDocumentWorkflowAndUser($documentWorkflow$user)) < 1) {
  136.             return false;
  137.         }
  138.         return true;
  139.     }
  140.     /**
  141.      * @param DocumentWorkflow $documentWorkflow
  142.      * @param User $user
  143.      * @return bool
  144.      */
  145.     private function canRevise(DocumentWorkflow $documentWorkflowUser $user): bool
  146.     {
  147.         if (!$this->canValidate($documentWorkflow$user)) {
  148.             return false;
  149.         }
  150.         return !$documentWorkflow->getCurrentStep()->isCreator();
  151.     }
  152.     /**
  153.      * @param DocumentWorkflow $documentWorkflow
  154.      * @param User $user
  155.      * @return bool
  156.      */
  157.     private function canValidate(DocumentWorkflow $documentWorkflowUser $user): bool
  158.     {
  159.         if (null === $documentWorkflow->getCurrentStep()) {
  160.             return false;
  161.         }
  162.         return $documentWorkflow->getCurrentStep()->getUser()->getId() === $user->getId();
  163.     }
  164.     /**
  165.      * @param DocumentWorkflow $documentWorkflow
  166.      * @param User $user
  167.      * @param bool $isAllAccess
  168.      * @return bool
  169.      */
  170.     private function canDownloadDriveFile(DocumentWorkflow $documentWorkflowUser $userbool $isAllAccess): bool
  171.     {
  172.         return $this->canView($documentWorkflow$user$isAllAccess) && DocumentWorkflowStatusEnum::VALIDATED === $documentWorkflow->getStatus();
  173.     }
  174.     /**
  175.      * @param DocumentWorkflow $documentWorkflow
  176.      * @param User $user
  177.      * @param bool $isAllAccess
  178.      * @return bool
  179.      */
  180.     private function canView(DocumentWorkflow $documentWorkflowUser $userbool $isAllAccess): bool
  181.     {
  182.         if (true === $isAllAccess) {
  183.             return true;
  184.         }
  185.         if (!$this->canEdit($documentWorkflow$user$isAllAccess)) {
  186.             $userSteps $this->documentWorkflowStepRepository->findByDocumentWorkflowAndUser($documentWorkflow$user);
  187.             if (count($userSteps) < 1) {
  188.                 return false;
  189.             }
  190.         }
  191.         return true;
  192.     }
  193.     /**
  194.      * @param DocumentWorkflow $documentWorkflow
  195.      * @param User $user
  196.      * @param bool $isAllAccess
  197.      * @return bool
  198.      */
  199.     private function canEdit(DocumentWorkflow $documentWorkflowUser $userbool $isAllAccess): bool
  200.     {
  201.         return ($isAllAccess || $this->isCreator($documentWorkflow$user))
  202.              && $documentWorkflow->getStatus() !== DocumentWorkflowStatusEnum::VALIDATED;
  203.     }
  204.     /**
  205.      * @param DocumentWorkflow $documentWorkflow
  206.      * @param User $user
  207.      * @return bool
  208.      */
  209.     private function isCreator(DocumentWorkflow $documentWorkflowUser $user): bool
  210.     {
  211.         return $user->getId() === $documentWorkflow->getUser()->getId();
  212.     }
  213. }