src/Delivract/Security/DocumentBailiffVoter.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Delivract\Security;
  3. use App\Delivract\Entity\DocumentBailiff;
  4. use App\Delivract\Entity\DocumentBailiffHistoryComment;
  5. use App\Delivract\Enum\DocumentBailiffStatusEnum;
  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 DocumentBailiffVoter
  15.  */
  16. class DocumentBailiffVoter extends Voter
  17. {
  18.     public const CREATE 'create';
  19.     public const COMMENT 'comment';
  20.     public const SUSPEND 'suspend';
  21.     public const CANCEL 'cancel';
  22.     public const RESUME 'resume';
  23.     public const ACCESS_DRIVE_FILE 'access_drive_file';
  24.     public const DOWNLOAD_DRIVE_FILE 'download_drive_file';
  25.     public const VIEW 'view';
  26.     /**
  27.      * @var AccessDecisionManagerInterface
  28.      */
  29.     private $decisionManager;
  30.     /**
  31.      * DocumentBailiffVoter constructor.
  32.      * @param AccessDecisionManagerInterface $decisionManager
  33.      */
  34.     public function __construct(
  35.         AccessDecisionManagerInterface $decisionManager
  36.     ) {
  37.         $this->decisionManager $decisionManager;
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     protected function supports($attribute$subject): bool
  43.     {
  44.         if (!in_array(
  45.             $attribute,
  46.             [
  47.                 self::CREATE,
  48.                 self::COMMENT,
  49.                 self::SUSPEND,
  50.                 self::CANCEL,
  51.                 self::RESUME,
  52.                 self::ACCESS_DRIVE_FILE,
  53.                 self::DOWNLOAD_DRIVE_FILE,
  54.                 self::VIEW,
  55.             ]
  56.         )) {
  57.             return false;
  58.         }
  59.         if (!$subject instanceof DocumentBailiff) {
  60.             return false;
  61.         }
  62.         return true;
  63.     }
  64.     /**
  65.      * {@inheritdoc}
  66.      */
  67.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  68.     {
  69.         if ($subject instanceof DocumentBailiffHistoryComment) {
  70.             /** @var DocumentBailiffHistoryComment $subject */
  71.             $subject $subject->getDocumentBailiff();
  72.         }
  73.         if (!$subject instanceof DocumentBailiff) {
  74.             return false;
  75.         }
  76.         $user $token->getUser();
  77.         if (!$user instanceof User) {
  78.             return false;
  79.         }
  80.         // the law firm's user need to module : MODULE_DELIVRACT
  81.         // the user need to have user module : USER_MODULE_DELIVRACT
  82.         if (!$user->getLawFirm()->hasModule(ModuleEnum::MODULE_DELIVRACT) ||
  83.             !$user->hasRole(ModuleEnum::USER_MODULE_DELIVRACT)) {
  84.             return false;
  85.         }
  86.         // only member of law firm owner can access this document
  87.         if ($subject->getLawFirm()->getId() !== $user->getLawFirm()->getId()) {
  88.             return false;
  89.         }
  90.         // Allow to see everything
  91.         /**
  92.          * @var bool $isAllAccess
  93.          */
  94.         $isAllAccess = (bool) $this->decisionManager->decide($token, [FeatureAccessEnum::ALL_DOCUMENT_BAILIFF_ACCESS]);
  95.         switch ($attribute) {
  96.             case self::CREATE:
  97.                 return $this->hasAccess($subject$user$isAllAccess);
  98.             case self::VIEW:
  99.             case self::COMMENT:
  100.                 return $this->canView($subject$user$isAllAccess);
  101.             case self::ACCESS_DRIVE_FILE:
  102.                 return $this->canAccessDriveFile($subject$user$isAllAccess);
  103.             case self::DOWNLOAD_DRIVE_FILE:
  104.                 return $this->canDownloadDriveFile($subject$user$isAllAccess);
  105.             case self::SUSPEND:
  106.                 return $this->canSuspend($subject$user$isAllAccess);
  107.             case self::CANCEL:
  108.                 return $this->canCancel($subject$user$isAllAccess);
  109.             case self::RESUME:
  110.                 return $this->canResume($subject$user$isAllAccess);
  111.             default:
  112.                 throw new InvalidArgumentException('Invalid attribute');
  113.         }
  114.     }
  115.     /**
  116.      * @param DocumentBailiff $documentBailiff
  117.      * @param User $user
  118.      * @param bool $isAllAccess
  119.      * @return bool
  120.      */
  121.     private function canSuspend(DocumentBailiff $documentBailiffUser $userbool $isAllAccess): bool
  122.     {
  123.         if ($this->hasAccess($documentBailiff$user$isAllAccess) === true) {
  124.             return DocumentBailiffStatusEnum::SENT === $documentBailiff->getStatus() ||
  125.                 DocumentBailiffStatusEnum::RECEIVED === $documentBailiff->getStatus();
  126.         }
  127.         return false;
  128.     }
  129.     /**
  130.      * @param DocumentBailiff $documentBailiff
  131.      * @param User $user
  132.      * @param bool $isAllAccess
  133.      * @return bool
  134.      */
  135.     private function canCancel(DocumentBailiff $documentBailiffUser $userbool $isAllAccess): bool
  136.     {
  137.         if ($this->hasAccess($documentBailiff$user$isAllAccess) === true) {
  138.             return DocumentBailiffStatusEnum::SENT === $documentBailiff->getStatus() ||
  139.                 DocumentBailiffStatusEnum::RECEIVED === $documentBailiff->getStatus() ||
  140.                 DocumentBailiffStatusEnum::SUSPENDED === $documentBailiff->getStatus();
  141.         }
  142.         return false;
  143.     }
  144.     /**
  145.      * @param DocumentBailiff $documentBailiff
  146.      * @param User $user
  147.      * @param bool $isAllAccess
  148.      * @return bool
  149.      */
  150.     private function canResume(DocumentBailiff $documentBailiffUser $userbool $isAllAccess): bool
  151.     {
  152.         if ($this->hasAccess($documentBailiff$user$isAllAccess) === true) {
  153.             return DocumentBailiffStatusEnum::SUSPENDED === $documentBailiff->getStatus();
  154.         }
  155.         return false;
  156.     }
  157.     /**
  158.      * @param DocumentBailiff $documentBailiff
  159.      * @param User $user
  160.      * @param bool $isAllAccess
  161.      * @return bool
  162.      */
  163.     private function canAccessDriveFile(DocumentBailiff $documentBailiffUser $userbool $isAllAccess): bool
  164.     {
  165.         if ($this->hasAccess($documentBailiff$user$isAllAccess) === true) {
  166.             return $this->canView($documentBailiff$user$isAllAccess) && DocumentBailiffStatusEnum::SERVED === $documentBailiff->getStatus();
  167.         }
  168.         return false;
  169.     }
  170.     /**
  171.      * @param DocumentBailiff $documentBailiff
  172.      * @param User $user
  173.      * @param bool $isAllAccess
  174.      * @return bool
  175.      */
  176.     private function canDownloadDriveFile(DocumentBailiff $documentBailiffUser $userbool $isAllAccess): bool
  177.     {
  178.         if ($this->hasAccess($documentBailiff$user$isAllAccess) === true) {
  179.             return $this->canView($documentBailiff$user$isAllAccess) && DocumentBailiffStatusEnum::SERVED === $documentBailiff->getStatus();
  180.         }
  181.         return false;
  182.     }
  183.     /**
  184.      * @param DocumentBailiff $documentBailiff
  185.      * @param User $user
  186.      * @param bool $isAllAccess
  187.      * @return bool
  188.      */
  189.     private function canView(DocumentBailiff $documentBailiffUser $userbool $isAllAccess): bool
  190.     {
  191.         return $this->hasAccess($documentBailiff$user$isAllAccess);
  192.     }
  193.     /**
  194.      * @param DocumentBailiff $documentBailiff
  195.      * @param User $user
  196.      * @param bool $isAllAccess
  197.      * @return bool
  198.      */
  199.     private function hasAccess(DocumentBailiff $documentBailiffUser $userbool $isAllAccess): bool
  200.     {
  201.         if (false === $isAllAccess) {
  202.             return (int) $user->getId() === (int) $documentBailiff->getUser()->getId();
  203.         }
  204.         return true;
  205.     }
  206. }