src/Web/Security/PasswordChangeRequiredHandler.php line 42

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace SpringerNature\CPS\AMEDReviewTracker\Web\Security;
  4. use FOS\UserBundle\Controller\SecurityController;
  5. use SpringerNature\CPS\AMEDReviewTracker\Web\Controller\Admin\MyAccountChangePassword;
  6. use SpringerNature\CPS\AMEDReviewTracker\Web\Controller\Admin\MyAccountRedirectToPasswordChange;
  7. use SpringerNature\CPS\AMEDReviewTracker\Web\Entity\WebUser;
  8. use Symfony\Bundle\TwigBundle\Controller\ExceptionController;
  9. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  10. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  11. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  12. use Symfony\Component\Security\Core\Security;
  13. /**
  14.  * Override any action to the password change page if it is required.
  15.  */
  16. class PasswordChangeRequiredHandler implements ContainerAwareInterface
  17. {
  18.     use ContainerAwareTrait;
  19.     /**
  20.      * @var Security
  21.      */
  22.     private $security;
  23.     /**
  24.      * PasswordChangeRequiredHandler constructor.
  25.      *
  26.      * @param Security $security
  27.      */
  28.     public function __construct(Security $security)
  29.     {
  30.         $this->security $security;
  31.     }
  32.     /**
  33.      * @param ControllerEvent $event
  34.      */
  35.     public function onKernelController(ControllerEvent $event): void
  36.     {
  37.         $eventControler $event->getController();
  38.         if (
  39.             $eventControler instanceof MyAccountChangePassword ||
  40.             is_array($eventControler) && (
  41.                 'Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController' == get_class($eventControler[0]) ||
  42.                 $eventControler[0] instanceof SecurityController ||
  43.                 $eventControler[0] instanceof ExceptionController
  44.             )
  45.         ) {
  46.             return;
  47.         }
  48.         if ( ! null === $this->security->getToken() && ! $this->security->isGranted('ROLE_USER')) {
  49.             return;
  50.         }
  51.         $user $this->security->getUser();
  52.         if ( ! $user instanceof WebUser) {
  53.             return;
  54.         }
  55.         if ($user->isPasswordChangeRequired()) {
  56.             $event->setController($this->container->get(MyAccountRedirectToPasswordChange::class));
  57.         }
  58.     }
  59. }