src/Security/ClientPlatform/CommonVoter.php line 45

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/CommonVoter.php
  4. // OK @ Plan.io Task #3710
  5. //------------------------------------------------------------------------------
  6. namespace App\Security\ClientPlatform;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use App\Entity\AccessClient\AccessClient;
  12. use App\Entity\Common\Attachment;
  13. use App\Entity\Platform\Devis\Devis;
  14. use App\Entity\Platform\Invoice\Invoice;
  15. use App\Entity\ProjectManager\ProjectNotebook;
  16. use App\Entity\Webapp\Document;
  17. use App\Services\AccessClient\AccessClientTools;
  18. use App\Services\LogTools;
  19. class CommonVoter extends Voter
  20. {
  21.     //--------------------------------------------------------------------------------
  22.     // is_granted constants
  23.     const VIEW_ATTACHMENT "client_platform_view_attachment";
  24.     const VIEW_DEVIS_PDF "client_platform_view_devis_pdf";
  25.     const VIEW_DOCUMENT_PDF "client_platform_view_document_pdf";
  26.     const VIEW_INVOICE_PDF "client_platform_view_invoice_pdf";
  27.     // Plan.io Task #4624 #4654 
  28.     const VIEW_PROJECT_NOTEBOOK_PDF "client_platform_view_project_notebook_pdf";
  29.     const IS_GRANTED_CONSTANTS = array(
  30.         self::VIEW_ATTACHMENT,
  31.         self::VIEW_DEVIS_PDF,
  32.         self::VIEW_DOCUMENT_PDF,
  33.         self::VIEW_INVOICE_PDF,
  34.         self::VIEW_PROJECT_NOTEBOOK_PDF,
  35.     );
  36.     //--------------------------------------------------------------------------------
  37.     // acl constants
  38.     //--------------------------------------------------------------------------------
  39.     public function __construct(Security $securityManagerRegistry $doctrineAccessClientTools $accessClientToolsLogTools $logTools)
  40.     {
  41.         $this->security $security;
  42.         $this->em $doctrine->getManager();
  43.         $this->accessClientTools $accessClientTools;
  44.         $this->logTools $logTools;
  45.     }
  46.     protected function supports(string $attribute$subject): bool
  47.     {
  48.         // if the attribute isn't one we support, return false
  49.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  50.         {
  51.             return false;
  52.         }
  53.         // only vote on Attachment|Devis|Document|Invoice|ProjectNotebook objects inside this voter
  54.         if ($subject !== null)
  55.         {
  56.             if (!($subject instanceof Attachment) &&
  57.                 !($subject instanceof Devis) &&
  58.                 !($subject instanceof Document) &&
  59.                 !($subject instanceof Invoice) &&
  60.                 !($subject instanceof ProjectNotebook))
  61.             return false;
  62.         }
  63.         return true;
  64.     }
  65.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  66.     {
  67.         // Only AccessClient with ROLE_CLIENT
  68.         if (!$this->security->isGranted('ROLE_CLIENT'))
  69.             return false;
  70.         $user $token->getUser();
  71.         if (!$user instanceof AccessClient)
  72.         {
  73.             // the user must be logged in; if not, deny access
  74.             return false;
  75.         }
  76.         // Check AccessClient affectation
  77.         if ($subject !== null)
  78.         {
  79.             $receiver $subject->getReceiver();
  80.             if ($receiver === null)
  81.             {
  82.                 $mission $subject->getMission();
  83.                 if ($mission === null)
  84.                 {
  85.                     return false;
  86.                 }
  87.                 $receiver $mission->getReceiver();
  88.                 if ($receiver === null)
  89.                 {
  90.                     return false;
  91.                 }
  92.             }
  93.             if (!$this->accessClientTools->areLinked($receiver$user))
  94.             {
  95.                 return false;
  96.             }
  97.         }
  98.         switch ($attribute)
  99.         {
  100.             case self::VIEW_ATTACHMENT:
  101.                 return true;
  102.             case self::VIEW_DOCUMENT_PDF:
  103.                 return true;
  104.             case self::VIEW_INVOICE_PDF:
  105.                 return true;
  106.             case self::VIEW_DEVIS_PDF:
  107.                 return true;
  108.             case self::VIEW_PROJECT_NOTEBOOK_PDF:
  109.                 return true;
  110.         }
  111.         throw new \LogicException('This code should not be reached!');
  112.     }
  113. }