src/Security/WikiVoter.php line 21

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/WikiVoter.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\Config\Config;
  11. use App\Entity\Config\Module;
  12. use App\Entity\HR\AccessFunction;
  13. use App\Entity\Security\Acl;
  14. use App\Entity\Security\AclPermission;
  15. use App\Entity\SocietyGroup;
  16. use App\Entity\Wiki\Page;
  17. use App\Services\Config\ModuleTools;
  18. class WikiVoter extends Voter
  19. {
  20.     const VIEW_PAGE "view_wiki_page";
  21.     const IS_GRANTED_CONSTANTS = array(
  22.         self::VIEW_PAGE,
  23.     );
  24.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleTools)
  25.     {
  26.         $this->em $doctrine->getManager();
  27.         $this->moduleTools $moduleTools;
  28.         $this->aclRepository $this->em->getRepository(Acl::class);
  29.         $this->aclPermissionRepository $this->em->getRepository(AclPermission::class);
  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.     protected function supports(string $attribute$subject): bool
  37.     {
  38.         // if the attribute isn't one we support, return false
  39.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  40.         {
  41.             return false;
  42.         }
  43.         // only vote on Template objects inside this voter
  44.         if ($subject !== null && !$subject instanceof Page)
  45.         {
  46.             return false;
  47.         }
  48.         return true;
  49.     }
  50.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  51.     {
  52.         $user $token->getUser();
  53.         if (!$user instanceof Access)
  54.         {
  55.             // the user must be logged in; if not, deny access
  56.             return false;
  57.         }
  58.         // The user must have a function; if not deny access
  59.         $function $user->getFunction();
  60.         if ($function === null)        return false;
  61.         // Plan.io Task #3710 : Get current group
  62.         $currentGroup $user->getSocietyGroup();
  63.         if ($currentGroup === null)
  64.             return false;
  65.         // you know $subject is a Page object, thanks to supports
  66.         /** @var Page $page */
  67.         $page $subject;
  68.         switch ($attribute)
  69.         {
  70.             case self::VIEW_PAGE:
  71.                 return $this->canViewPage($page$user$function$currentGroup);
  72.         }
  73.         throw new \LogicException('This code should not be reached!');
  74.     }
  75.     private function canViewPage(Page $pageAccess $accessAccessFunction $functionSocietyGroup $currentGroup)
  76.     {
  77.         // Is this page available for all ?
  78.         if ($page->isAvailableToAll())
  79.         {
  80.             return true;
  81.         }
  82.         // If we are here it means the page is not available to all society groups
  83.         // Check to see if it is available to this particular society group
  84.         if ($page->isAvailableTo($currentGroup))
  85.         {
  86.             return true;
  87.         }
  88.         // All hope is lost
  89.         return false;
  90.     }
  91. }