src/Security/SettingsVoter.php line 30

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/SettingsVoter.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\HR\AccessFunction;
  11. use App\Entity\Security\Acl;
  12. use App\Entity\Security\AclPermission;
  13. class SettingsVoter extends Voter
  14. {
  15.     const EDIT "edit_settings";
  16.     const IS_GRANTED_CONSTANTS = array(
  17.         self::EDIT,
  18.     );
  19.     const ACL_PERM_EDIT "settings_edit";
  20.     public function __construct(ManagerRegistry $doctrine)
  21.     {
  22.         $this->em $doctrine->getManager();
  23.         $this->aclRepository $this->em->getRepository(Acl::class);
  24.         $this->aclPermissionRepository $this->em->getRepository(AclPermission::class);
  25.     }
  26.     // Plan.io Task #4453 [See AccessVoter for details]
  27.     public function supportsAttribute(string $attribute): bool
  28.     {
  29.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  30.     }
  31.     
  32.     protected function supports(string $attribute$subject): bool
  33.     {
  34.         // if the attribute isn't one we support, return false
  35.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  36.         {
  37.             return false;
  38.         }
  39.         return true;
  40.     }
  41.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  42.     {
  43.         $user $token->getUser();
  44.         if (!$user instanceof Access)
  45.         {
  46.             // the user must be logged in; if not, deny access
  47.             return false;
  48.         }
  49.         // The user must have a function; if not deny access
  50.         $function $user->getFunction();
  51.         if ($function === null)        return false;
  52.         // Plan.io Task #3710 : Get current group
  53.         $currentGroup $user->getSocietyGroup();
  54.         if ($currentGroup === null)
  55.             return false;
  56.         // If the access is the default admin of the group then always grant him the power
  57.         if ($currentGroup->getAdmin() !== null && $currentGroup->getAdmin()->equals($user))
  58.         {
  59.             return true;
  60.         }
  61.         switch ($attribute)
  62.         {
  63.             case self::EDIT:
  64.                 return $this->canEdit($user$function);
  65.         }
  66.         throw new \LogicException('This code should not be reached!');
  67.     }
  68.     private function canEdit(Access $accessAccessFunction $function)
  69.     {
  70.         // Get Acl_Permission
  71.         $aclPerm $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT);
  72.         if ($aclPerm === null)        return false;
  73.         // Get Acl
  74.         $acl $this->aclRepository->findOneBy(array(
  75.             'function'        =>    $function,
  76.             'permission'    =>    $aclPerm
  77.         ));
  78.         if ($acl === null)        return false;
  79.         return $acl->getValue();
  80.     }
  81. }