src/EventSubscriber/AuthSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Domain\Exception\Restrict\RestrictException;
  4. use App\Services\Clients\ClientSystemState\ClientSystemStateService;
  5. use App\Services\Auth\TokenClientUserService;
  6. use App\Entity\OA2User;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. class AuthSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var TokenClientUserService
  15.      */
  16.     private $tokenService;
  17.     /**
  18.      * @var ClientSystemStateService
  19.      */
  20.     private $systemStateService;
  21.     /**
  22.      * @var ContainerInterface
  23.      */
  24.     private $container;
  25.     private $allowedForPasswordExpired = [
  26.         'loginBankUser'// /bank/login
  27.         'updateBankUserByOwner'// /bank/updateUser
  28.         'bankUserProfile'// /bank/getProfile
  29.     ];
  30.     public function __construct(
  31.         TokenClientUserService $tokenService,
  32.         ClientSystemStateService $systemStateService,
  33.         ContainerInterface $container
  34.     ) {
  35.         $this->tokenService $tokenService;
  36.         $this->systemStateService $systemStateService;
  37.         $this->container $container;
  38.     }
  39.     public function onKernelController(FilterControllerEvent $event)
  40.     {
  41.         $controller $event->getController();
  42.         /*
  43.          * $controller passed can be either a class or a Closure.
  44.          * This is not usual in Symfony but it may happen.
  45.          * If it is a class, it comes in array format
  46.          */
  47.         if (!is_array($controller)) {
  48.             return;
  49.         }
  50.         $route $event->getRequest()->attributes->get('_route');
  51.         /** @var TokenStorageInterface $tokenStorage */
  52.         $tokenStorage $this->container->get('security.token_storage');
  53.         $token $tokenStorage->getToken();
  54.         if ($token === null) {
  55.             return;
  56.         }
  57.         /** @var OA2User $user */
  58.         $user $token->getUser();
  59.         if (!$user instanceof OA2User || !$user->isBankUser()) {
  60.             return;
  61.         }
  62.         // If bank user was authorized by permanent token we don't need to check expiration password
  63.         if ($user->getBankUser()->permanentTokenIsActive($event->getRequest()->headers->get('BANK-AUTH-TOKEN'))) {
  64.             return;
  65.         }
  66.         if ($user->isPasswordExpired() && !in_array($route$this->allowedForPasswordExpired)) {
  67.             throw new RestrictException('Password is expired');
  68.         }
  69.     }
  70.     public static function getSubscribedEvents()
  71.     {
  72.         return [
  73.             'kernel.controller' => 'onKernelController',
  74.         ];
  75.     }
  76. }