src/Security/InodeNineVoter.php line 32

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/InodeNineVoter.php
  4. //------------------------------------------------------------------------------
  5. namespace App\Security;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use App\Entity\Access;
  10. use App\Entity\CloudNine\Inode;
  11. use App\Entity\SocietyGroup;
  12. use App\Entity\Config\Config;
  13. use App\Entity\Config\Module;
  14. use App\Entity\HR\AccessFunction;
  15. use App\Entity\Security\Acl;
  16. use App\Entity\Security\AclPermission;
  17. use App\Services\Config\ModuleTools;
  18. // use Icod\CloudNineBundle\Entity\Inode;
  19. // use Icod\CloudBundle\Entity\Config as CloudConfig;
  20. class InodeNineVoter extends Voter
  21. {
  22.     const VIEW "view_inode_nine";
  23.     const IS_GRANTED_CONSTANTS = array(
  24.         self::VIEW,
  25.     );
  26.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleTools)
  27.     {
  28.         $this->em $doctrine->getManager();
  29.         $this->moduleTools $moduleTools;
  30.     }
  31.     // Plan.io Task #4453 [See AccessVoter for details]
  32.     public function supportsAttribute(string $attribute): bool
  33.     {
  34.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  35.     }
  36.     
  37.     protected function supports(string $attribute$subject): bool
  38.     {
  39.         // if the attribute isn't one we support, return false
  40.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  41.         {
  42.             return false;
  43.         }
  44.         // only vote on Inode objects inside this voter
  45.         if ($subject !== null)
  46.         {
  47.             if (!$subject instanceof Inode)
  48.             {
  49.                 return false;
  50.             }
  51.         }
  52.         return true;
  53.     }
  54.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  55.     {
  56.         $user $token->getUser();
  57.         if (!$user instanceof Access)
  58.         {
  59.             // the user must be logged in; if not, deny access
  60.             return false;
  61.         }
  62.         // The user must have a function; if not deny access
  63.         $function $user->getFunction();
  64.         if ($function === null)        return false;
  65.         // Plan.io Task #3710 : Get current group
  66.         $currentGroup $user->getSocietyGroup();
  67.         if ($currentGroup === null)
  68.             return false;
  69.         // you know $subject is a Inode object, thanks to supports
  70.         /** @var Inode $inode */
  71.         $inode $subject;
  72.         // Does this society group have access to the cloud ?
  73.         // Module activated ?
  74.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_CLOUD))
  75.         {
  76.             return false;
  77.         }
  78.         switch ($attribute)
  79.         {
  80.             case self::VIEW:
  81.                 return $this->canView($inode$currentGroup);
  82.         }
  83.         throw new \LogicException('This code should not be reached!');
  84.     }
  85.     private function canView(Inode $inodeSocietyGroup $societyGroup)
  86.     {
  87.         $acls $inode->getAcls();
  88.         foreach ($acls as $acl)
  89.         {
  90.             if ($acl->getSocietyGroup() === null)
  91.             {
  92.                 // Shared with all society group
  93.                 return true;
  94.             }
  95.             if ($acl->getSocietyGroup()->equals($societyGroup))
  96.             {
  97.                 // Shared with this group
  98.                 return true;
  99.             }
  100.         }
  101.         // Find a parent that is shared with this society group
  102.         if ($inode->isRoot())
  103.         {
  104.             return false;
  105.         }
  106.         else
  107.         {
  108.             return $this->canView($inode->getParent(), $societyGroup);
  109.         }
  110.         // All hope is lost
  111.         return false;
  112.     }
  113. }