src/Security/StickyNoteVoter.php line 44

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/StickyNoteVoter.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 Symfony\Component\Security\Core\Security;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use App\Entity\Access;
  11. use App\Entity\Config\Config;
  12. use App\Entity\Config\Module;
  13. use App\Entity\HR\AccessFunction;
  14. use App\Entity\Media\StickyNote;
  15. use App\Entity\Security\Acl;
  16. use App\Entity\Security\AclPermission;
  17. use App\Services\Config\ModuleTools;
  18. // use Icod\ComToolboxBundle\Entity\StickyNote;
  19. class StickyNoteVoter extends Voter
  20. {
  21.     //--------------------------------------------------------------------------------
  22.     // is_granted constants
  23.     const VIEW "view_sticky_note";
  24.     const EDIT "edit_sticky_note";
  25.     const IS_GRANTED_CONSTANTS = array(
  26.         self::VIEW,
  27.         self::EDIT,
  28.     );
  29.     //--------------------------------------------------------------------------------
  30.     // acl constants
  31.     const ACL_PERM_VIEW "sticky_note_view";
  32.     const ACL_PERM_EDIT "sticky_note_edit";
  33.     //--------------------------------------------------------------------------------
  34.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleTools)
  35.     {
  36.         $this->em $doctrine->getManager();
  37.         $this->moduleTools $moduleTools;
  38.         $this->aclRepository $this->em->getRepository(Acl::class);
  39.         $this->aclPermissionRepository $this->em->getRepository(AclPermission::class);
  40.     }
  41.     // Plan.io Task #4453 [See AccessVoter for details]
  42.     public function supportsAttribute(string $attribute): bool
  43.     {
  44.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  45.     }
  46.     protected function supports(string $attribute$subject): bool
  47.     {
  48.         // if the attribute isn't one we support, return false
  49.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  50.         {
  51.             return false;
  52.         }
  53.         // only vote on StickyNote objects inside this voter
  54.         if ($subject !== null && !$subject instanceof StickyNote)
  55.         {
  56.             return false;
  57.         }
  58.         return true;
  59.     }
  60.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  61.     {
  62.         $user $token->getUser();
  63.         if (!$user instanceof Access)
  64.         {
  65.             // the user must be logged in; if not, deny access
  66.             return false;
  67.         }
  68.         // The user must have a function; if not deny access
  69.         $function $user->getFunction();
  70.         if ($function === null)        return false;
  71.         // Plan.io Task #3710 : Get current group
  72.         $currentGroup $user->getSocietyGroup();
  73.         if ($currentGroup === null)
  74.             return false;
  75.         // you know $subject is a StickyNote object, thanks to supports
  76.         /** @var StickyNote $note */
  77.         $note $subject;
  78.         // Check current group affectation
  79.         if ($subject !== null)
  80.         {
  81.             $subjectGroup $subject->getSocietyGroup();
  82.             if ($subjectGroup === null)
  83.                 return false;
  84.             if (!$currentGroup->equals($subjectGroup))
  85.                 return false;
  86.         }
  87.         switch ($attribute)
  88.         {
  89.             case self::VIEW:
  90.                 return $this->canView($note$user$function);
  91.             case self::EDIT:
  92.                 return $this->canEdit($note$user$function);
  93.         }
  94.         throw new \LogicException('This code should not be reached!');
  95.     }
  96.     // $access is the user trying to load the resource
  97.     // $note is the resource being loaded
  98.     private function canEdit(StickyNote $note nullAccess $userAccessFunction $function)
  99.     {
  100.         // Two AclPermission may exist
  101.         $aclPerm $this->aclPermissionRepository->findOneByName(self::ACL_PERM_EDIT);
  102.         // If all are null, exit
  103.         if ($aclPerm === null)
  104.             return false;
  105.         // Get First one
  106.         if ($aclPerm !== null)
  107.         {
  108.             $acl $this->aclRepository->findOneBy(array(
  109.                 'function'        =>    $function,
  110.                 'permission'    =>    $aclPerm
  111.             ));
  112.             if ($acl !== null)
  113.             {
  114.                 if ($acl->getValue())
  115.                 {
  116.                     // A single positive answer is enough
  117.                     return true;
  118.                 }
  119.             }
  120.         }
  121.         // If we are here, all hope is lost
  122.         return false;
  123.     }
  124.     private function canView(StickyNote $note nullAccess $userAccessFunction $function)
  125.     {
  126.         $aclPerm $this->aclPermissionRepository->findOneByName(self::ACL_PERM_VIEW);
  127.         // If all are null, exit
  128.         if ($aclPerm === null)
  129.             return false;
  130.         // Get First one
  131.         if ($aclPerm !== null)
  132.         {
  133.             $acl $this->aclRepository->findOneBy(array(
  134.                 'function'        =>    $function,
  135.                 'permission'    =>    $aclPerm
  136.             ));
  137.             if ($acl !== null)
  138.             {
  139.                 if ($acl->getValue())
  140.                 {
  141.                     // A single positive answer is enough
  142.                     return true;
  143.                 }
  144.             }
  145.         }
  146.         // If we are here, all hope is lost
  147.         return false;
  148.     }
  149. }