src/Security/IkeaKitchenPlannerVoter.php line 35

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/IkeaKitchenPlannerVoter.php
  4. //------------------------------------------------------------------------------
  5. namespace App\Security;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use App\Entity\Access;
  10. use App\Entity\APIRest\AccessAPI;
  11. use App\Entity\Config\Module;
  12. use App\Entity\HR\AccessFunction;
  13. use App\Entity\Ikea\KitchenPlannerProject;
  14. use App\Entity\Security\Acl;
  15. use App\Entity\Security\AclPermission;
  16. use App\Services\Config\ModuleTools;
  17. class IkeaKitchenPlannerVoter extends Voter
  18. {
  19.     const IS_ACTIVE "ikea_kitchen_planner_is_active";
  20.     const IS_GRANTED_CONSTANTS = array(
  21.         self::IS_ACTIVE,            
  22.     );
  23.     //--------------------------------------------------------------------------------
  24.     // acl constants (matrix_acl.yml lines 3354-3390) - Removed
  25.     //--------------------------------------------------------------------------------
  26.     public function __construct(ManagerRegistry $doctrineModuleTools $moduleTools)
  27.     {
  28.         $this->em $doctrine->getManager();
  29.         $this->moduleTools $moduleTools;
  30.         $this->aclRepository $this->em->getRepository(Acl::class);
  31.         $this->aclPermissionRepository $this->em->getRepository(AclPermission::class);
  32.     }
  33.     public function supportsAttribute(string $attribute): bool
  34.     {
  35.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  36.     }
  37.     protected function supports(string $attribute$subject): bool
  38.     {
  39.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  40.         {
  41.             return false;
  42.         }
  43.         if ($subject !== null && !$subject instanceof KitchenPlannerProject)
  44.         {
  45.             return false;
  46.         }
  47.         return true;
  48.     }
  49.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  50.     {
  51.         $user $token->getUser();
  52.         if ($user instanceof AccessAPI)
  53.         {
  54.             if ($user->getAccess() === null)
  55.             {
  56.                 return false;
  57.             }
  58.             $user $user->getAccess();
  59.         }
  60.         if (!$user instanceof Access)
  61.         {
  62.             return false;
  63.         }
  64.         $function $user->getFunction();
  65.         if ($function === null)
  66.         {
  67.             return false;
  68.         }
  69.         $currentGroup $user->getSocietyGroup();
  70.         if ($currentGroup === null)
  71.         {
  72.             return false;
  73.         }
  74.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_IKEA_KITCHEN_PLANNER))
  75.         {
  76.             return false;
  77.         }
  78.         if ($subject !== null)
  79.         {
  80.             /** @var KitchenPlannerProject $project */
  81.             $project $subject;
  82.             $projectGroupId $project->getSocietyGroupId();
  83.             if ($projectGroupId === null)
  84.             {
  85.                 return false;
  86.             }
  87.             if ($currentGroup->getId() !== $projectGroupId)
  88.             {
  89.                 return false;
  90.             }
  91.         }
  92.         switch ($attribute)
  93.         {
  94.             case self::IS_ACTIVE:
  95.                 return true;
  96.         }
  97.         throw new \LogicException('This code should not be reached!');
  98.     }
  99. }