src/Security/DashboardVoter.php line 18

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/DashboardVoter.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\Module;
  11. use App\Entity\HR\AccessFunction;
  12. use App\Entity\Security\Acl;
  13. use App\Entity\Security\AclPermission;
  14. use App\Services\Config\ModuleTools;
  15. class DashboardVoter extends Voter
  16. {
  17.     //--------------------------------------------------------------------------------
  18.     // is_granted constants
  19.     const VIEW "view_dashboard";
  20.     const IS_ACTIVE "dashboard_is_active";
  21.     const IS_GRANTED_CONSTANTS = array(
  22.         self::VIEW,
  23.         self::IS_ACTIVE,
  24.     );
  25.     //--------------------------------------------------------------------------------
  26.     // acl constants
  27.     const ACL_PERM_VIEW "dashboard_view";
  28.     //--------------------------------------------------------------------------------
  29.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleTools)
  30.     {
  31.         $this->em $doctrine->getManager();
  32.         $this->moduleTools $moduleTools;
  33.         $this->aclRepository $this->em->getRepository(Acl::class);
  34.         $this->aclPermissionRepository $this->em->getRepository(AclPermission::class);
  35.     }
  36.     // Plan.io Task #4453 [See AccessVoter for details]
  37.     public function supportsAttribute(string $attribute): bool
  38.     {
  39.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  40.     }
  41.     protected function supports(string $attribute$subject null): bool
  42.     {
  43.         // if the attribute isn't one we support, return false
  44.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  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.         // Module activated ?
  66.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_DASHBOARD))
  67.         {
  68.             return false;
  69.         }
  70.         // // you know $subject is a Dashboard object, thanks to supports
  71.         // /** @var Dashboard $devis */
  72.         // $devis = $subject;
  73.         //
  74.         // // Check current group affectation
  75.         // if ($subject !== null)
  76.         // {
  77.         //     $subjectSociety = $subject->getSociety();
  78.         //     if ($subjectSociety === null)
  79.         //         return false;
  80.         //     $subjectGroup = $subjectSociety->getGroup();
  81.         //     if ($subjectGroup === null)
  82.         //         return false;
  83.         //     if (!$currentGroup->equals($subjectGroup))
  84.         //         return false;
  85.         // }
  86.         switch ($attribute)
  87.         {
  88.             case self::IS_ACTIVE:
  89.                 return true;
  90.             case self::VIEW:
  91.                 return $this->canView($user$function);
  92.         }
  93.         throw new \LogicException('This code should not be reached!');
  94.     }
  95.     private function canView(Access $userAccessFunction $function)
  96.     {
  97.         // Get Acl_Permission
  98.         $aclPerm $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW);
  99.         if ($aclPerm === null)        return false;
  100.         // Get Acl
  101.         $acl $this->aclRepository->findOneBy(array(
  102.             'function'        =>    $function,
  103.             'permission'    =>    $aclPerm
  104.         ));
  105.         if ($acl === null)        return false;
  106.         // Since only one acl type can exist
  107.         // we can return the result of the acl_permission
  108.         return $acl->getValue();
  109.     }
  110. }