src/Security/AclVoter.php line 42

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/AclVoter.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. class AclVoter extends Voter
  16. {
  17.     //--------------------------------------------------------------------------------
  18.     // is_granted constants
  19.     const VIEW "acl_view";
  20.     const EDIT "acl_edit";
  21.     const ACCESS "acl_view_or_edit";
  22.     const IS_GRANTED_CONSTANTS = array(
  23.         self::VIEW,
  24.         self::EDIT,
  25.         self::ACCESS,
  26.     );
  27.     //--------------------------------------------------------------------------------
  28.     // acl constants
  29.     const ACL_PERM_VIEW "acl_view";
  30.     const ACL_PERM_EDIT "acl_edit";
  31.     //--------------------------------------------------------------------------------
  32.     public function __construct(ManagerRegistry $doctrine)
  33.     {
  34.         $this->em $doctrine->getManager();
  35.         $this->aclRepository $this->em->getRepository(Acl::class);
  36.         $this->aclPermissionRepository $this->em->getRepository(AclPermission::class);
  37.     }
  38.     // Plan.io Task #4453 [See AccessVoter for details]
  39.     public function supportsAttribute(string $attribute): bool
  40.     {
  41.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  42.     }
  43.     protected function supports(string $attribute$subject): bool
  44.     {
  45.         // if the attribute isn't one we support, return false
  46.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  47.         {
  48.             return false;
  49.         }
  50.         return true;
  51.     }
  52.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  53.     {
  54.         $user $token->getUser();
  55.         if (!$user instanceof Access)
  56.         {
  57.             // the user must be logged in; if not, deny access
  58.             return false;
  59.         }
  60.         // The user must have a function; if not deny access
  61.         $function $user->getFunction();
  62.         if ($function === null)        return false;
  63.         // Plan.io Task #3710 : Get current group
  64.         $currentGroup $user->getSocietyGroup();
  65.         if ($currentGroup === null)
  66.             return false;
  67.         // The Admin of the SocietyGroup should always have access to this
  68.         if ($user->equals($currentGroup->getAdmin()))
  69.         {
  70.             return true;
  71.         }
  72.         // Access to Settings Page (view or edit)
  73.         if ($attribute == self::ACCESS)
  74.         {
  75.             return $this->canDo(self::VIEW$user$function) || $this->canDo(self::EDIT$user$function);
  76.         }
  77.         // Edit or View Access
  78.         return $this->canDo($attribute$user$function);
  79.         throw new \LogicException('This code should not be reached!');
  80.     }
  81.     private function canDo($attributeAccess $userAccessFunction $function)
  82.     {
  83.         // Get AclPermission
  84.         $aclPerm $this->aclPermissionRepository->findOneByName($attribute);
  85.         if ($aclPerm === null)        return false;
  86.         // Get Acl
  87.         $acl $this->aclRepository->findOneBy(array(
  88.             'function'        =>    $function,
  89.             'permission'    =>    $aclPerm
  90.         ));
  91.         if ($acl === null)        return false;
  92.         // Since only one acl type can exist
  93.         // we can return the result of the acl_permission
  94.         return $acl->getValue();
  95.     }
  96. }