src/Security/ExternalObjectVoter.php line 44

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/ExternalObjectVoter.php
  4. //------------------------------------------------------------------------------
  5. namespace App\Security;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use App\Entity\Access;
  11. use App\Entity\Config\Config;
  12. use App\Entity\Config\Module;
  13. use App\Entity\HR\AccessFunction;
  14. use App\Entity\Platform\Devis\Devis;
  15. use App\Entity\Platform\Devis\ExternalDevis;
  16. use App\Entity\Platform\Invoice\ExternalInvoice;
  17. use App\Entity\Planning\ExternalTask;
  18. use App\Entity\Security\Acl;
  19. use App\Entity\Webapp\ExternalDocument;
  20. class ExternalObjectVoter extends Voter
  21. {
  22.     //--------------------------------------------------------------------------------
  23.     // is_granted constants
  24.     const VIEW "view_external_object";
  25.     const VIEW_DEVIS "view_external_devis";
  26.     const VIEW_INVOICE "view_external_invoice";
  27.     const VIEW_TASK "view_external_task";
  28.     const VIEW_DOC "view_external_document";
  29.     const IS_GRANTED_CONSTANTS = array(
  30.         self::VIEW,
  31.         self::VIEW_DEVIS,
  32.         self::VIEW_INVOICE,
  33.         self::VIEW_TASK,
  34.         self::VIEW_DOC,
  35.     );
  36.     //--------------------------------------------------------------------------------
  37.     public function __construct(AccessDecisionManagerInterface $accessDecisionManagerManagerRegistry $doctrine)
  38.     {
  39.         $this->accessDecisionManager $accessDecisionManager;
  40.         $this->em $doctrine->getManager();
  41.     }
  42.     // Plan.io Task #4453 [See AccessVoter for details]
  43.     public function supportsAttribute(string $attribute): bool
  44.     {
  45.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  46.     }
  47.     
  48.     protected function supports(string $attribute$subject null): bool
  49.     {
  50.         // if the attribute isn't one we support, return false
  51.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  52.         {
  53.             return false;
  54.         }
  55.         if ($subject !== null && !($subject instanceof ExternalDevis || $subject instanceof ExternalInvoice || $subject instanceof ExternalDocument || $subject instanceof ExternalTask))
  56.         {
  57.             return false;
  58.         }
  59.         return true;
  60.     }
  61.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  62.     {
  63.         $user $token->getUser();
  64.         if (!$user instanceof Access)
  65.         {
  66.             // the user must be logged in; if not, deny access
  67.             return false;
  68.         }
  69.         // The user must have a function; if not deny access
  70.         $function $user->getFunction();
  71.         if ($function === null)        return false;
  72.         // Plan.io Task #3710 : Get current group
  73.         $currentGroup $user->getSocietyGroup();
  74.         if ($currentGroup === null)
  75.             return false;
  76.         $this->currentGroup $currentGroup;
  77.         // Plan.io Task #3230
  78.         // In order to take sharing into account,
  79.         // decide the viewing of the ExternalObject based on its Mission
  80.         $mission $subject->getMission();
  81.         if ($mission === null)
  82.         {
  83.             // This should not happen
  84.             return false;
  85.         }
  86.         return $this->accessDecisionManager->decide($token, ['view_mission'], $mission);
  87.         throw new \LogicException('This code should not be reached!');
  88.     }
  89. }