src/Security/ClientVoter.php line 22

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/ClientVoter.php
  4. //------------------------------------------------------------------------------
  5. namespace App\Security;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use App\Entity\Access;
  12. use App\Entity\Client\Client;
  13. use App\Entity\Config\Config;
  14. use App\Entity\Config\Module;
  15. use App\Entity\HR\AccessFunction;
  16. use App\Entity\Security\Acl;
  17. use App\Entity\Security\AclPermission;
  18. use App\Services\Config\ModuleTools;
  19. class ClientVoter extends Voter
  20. {
  21.     //--------------------------------------------------------------------------------
  22.     // is_granted constants
  23.     const IS_ACTIVE "client_is_active";
  24.     const ADD "add_client";
  25.     const LISTING "list_clients";
  26.     const VIEW "view_client";
  27.     const EDIT "edit_client";
  28.     const IS_GRANTED_CONSTANTS = array(
  29.         self::IS_ACTIVE,
  30.         self::ADD,
  31.         self::LISTING,
  32.         self::VIEW,
  33.         self::EDIT,
  34.     );
  35.     public function __construct(AccessDecisionManagerInterface $accessDecisionManagerManagerRegistry $doctrineModuleTools $moduleTools)
  36.     {
  37.         $this->accessDecisionManager $accessDecisionManager;
  38.         $this->em $doctrine->getManager();
  39.         $this->moduleTools $moduleTools;
  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 Client objects inside this voter
  54.         if ($subject !== null && !$subject instanceof Client)
  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.         // Module activated ?
  76.         if ($this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_CLIENT) && $this->moduleTools->isInactiveByCode($currentGroupModule::MODULE_CLIENT_LIGHT))
  77.         {
  78.             return false;
  79.         }
  80.         // you know $subject is a Client object, thanks to supports
  81.         /** @var Client $client */
  82.         $client $subject;
  83.         switch ($attribute)
  84.         {
  85.             case self::IS_ACTIVE:
  86.                 return true;
  87.             case self::ADD:
  88.                 return $this->canAdd($client$token);
  89.             case self::LISTING:
  90.                 return $this->canList();
  91.             case self::VIEW:
  92.                 return $this->canView($client$token);
  93.             case self::EDIT:
  94.                 return $this->canEdit($client$token);
  95.         }
  96.         throw new \LogicException('This code should not be reached!');
  97.     }
  98.     private function canList()
  99.     {
  100.         return true;
  101.     }
  102.     private function canAdd(Client $client null$token)
  103.     {
  104.         if ($client->getIndividual() !== null)
  105.         {
  106.             return $this->accessDecisionManager->decide($token, ['add_individual']);
  107.         }
  108.         if ($client->getStore() !== null)
  109.         {
  110.             return $this->accessDecisionManager->decide($token, ['add_store']);
  111.         }
  112.         return false;
  113.     }
  114.     private function canView(Client $client$token)
  115.     {
  116.         if ($client->getIndividual() !== null)
  117.         {
  118.             return $this->accessDecisionManager->decide($token, ['view_individual'], $client->getIndividual());
  119.         }
  120.         if ($client->getStore() !== null)
  121.         {
  122.             return $this->accessDecisionManager->decide($token, ['view_store'], $client->getStore());
  123.         }
  124.         return false;
  125.     }
  126.     private function canEdit(Client $client$token)
  127.     {
  128.         if ($client->getIndividual() !== null)
  129.         {
  130.             return $this->accessDecisionManager->decide($token, ['edit_individual'], $client->getIndividual());
  131.         }
  132.         if ($client->getStore() !== null)
  133.         {
  134.             return $this->accessDecisionManager->decide($token, ['edit_store'], $client->getStore());
  135.         }
  136.         return false;
  137.     }
  138. }