src/Package/Admin/Tools/SecurityVoter/SecurityVoter.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Package\Admin\Tools\SecurityVoter;
  3. use Symfony\Component\HttpFoundation\RequestStack,
  4.     Symfony\Component\Security\Core\Authorization\Voter\VoterInterface,
  5.     Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use App\Package\Toolkit\ApplicationMode\ApplicationMode,
  7.     App\Package\Admin\Tools\AdminState\AdminState,
  8.     App\Package\Admin\Main\EntityInterface\AdminInterface;
  9. /**
  10.  * SecurityVoter
  11.  * 
  12.  * Security voter for administration panel
  13.  * 
  14.  * @author     Daniel Balowski <d.balowski@openform.pl> (_creator)
  15.  * @copyright  Openform
  16.  * @since      03.2019
  17.  */
  18. class SecurityVoter implements VoterInterface
  19. {
  20.     /**
  21.      * @var ApplicationMode
  22.      */
  23.     protected $applicationMode;
  24.     /**
  25.      * @var AdminState
  26.      */
  27.     protected $adminState;
  28.     /**
  29.      * @var RequestStack
  30.      */
  31.     protected $requestStack;
  32.     /**
  33.      * @param ApplicationMode  $applicationMode
  34.      * @param AdminState       $adminState
  35.      * @param RequestStack     $requestStack
  36.      */
  37.     public function __construct(ApplicationMode $applicationModeAdminState $adminStateRequestStack $requestStack
  38.     {
  39.         $this->applicationMode $applicationMode;
  40.         $this->adminState      $adminState;
  41.         $this->requestStack    $requestStack;
  42.     }
  43.     /**
  44.      * {@inheritDoc}
  45.      */
  46.     public function vote(TokenInterface $token$subject null, array $attributes = []) : int
  47.     {
  48.         if (
  49.             $this->applicationMode->getCurrentMode() !== 'admin' ||
  50.             $this->requestStack->getParentRequest()
  51.         ) {
  52.             return VoterInterface::ACCESS_GRANTED;
  53.         }
  54.         $admin $this->getAdmin($token);
  55.         if (! $admin) {
  56.             return VoterInterface::ACCESS_GRANTED;
  57.         }
  58.         $module $this->adminState->getCurrentModule();
  59.         return
  60.             ! $module || $admin->verifyAdminModuleAccess($module) ?
  61.             VoterInterface::ACCESS_GRANTED :
  62.             VoterInterface::ACCESS_DENIED;
  63.     }
  64.     /**
  65.      * Gets admin
  66.      * 
  67.      * @param TokenInterface|null  $token    (optional)
  68.      * 
  69.      * @return AdminInterface|null
  70.      */
  71.     protected function getAdmin(TokenInterface $token null) : ? AdminInterface
  72.     {
  73.         if (! $token) {
  74.             return null;
  75.         }
  76.         $admin $token->getUser();
  77.         return 
  78.             $admin && gettype($admin) !== 'string' ?
  79.             $admin :
  80.             null;
  81.     }
  82. }