src/Security/Voters/CompanyAdminSystemAccessVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Service\SystemServices\ISystemAccessService;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Security;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. class CompanyAdminSystemAccessVoter extends Voter
  9. {
  10.     private $security;
  11.     private $systemService;
  12.     const SUPER_ADMIN_CONTROLLER_ACCESS 'COMPANY_ADMIN_CONTROLLER_ACCESS';
  13.     /**
  14.      * @param Security $security
  15.      * @param ISystemAccessService $systemService
  16.      */
  17.     public function __construct(Security $securityISystemAccessService $systemService)
  18.     {
  19.         $this->security $security;
  20.         $this->systemService $systemService;
  21.     }
  22.     protected function supports(string $attribute$subject): bool
  23.     {
  24.         if ($attribute != self::SUPER_ADMIN_CONTROLLER_ACCESS) {
  25.             return false;
  26.         }
  27.         return true;
  28.     }
  29.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  30.     {
  31.         $user $token->getUser();
  32.         if (!$user instanceof UserInterface || empty($subject)) {
  33.             return false;
  34.         }
  35.         /**
  36.          * ROLE_SUPER_ADMIN, ROLE_ADMIN & ROLE_LEGAL_ENTITY_ADMIN
  37.          */
  38.         if (!$this->security->isGranted('ROLE_SUPER_ADMIN') && !$this->security->isGranted('ROLE_ADMIN') &&
  39.             !$this->security->isGranted('ROLE_LEGAL_ENTITY_ADMIN')) {
  40.             return false;
  41.         }
  42.         /**
  43.          * super system access level check
  44.          */
  45.         return $this->systemService->subSystemModuleAccessValidation($user$subject);
  46.         //todo generate access log
  47.     }
  48. }