Loading src/EventSubscriber/EnforceUserFieldsSubscriber.php +23 −11 Changes for src/EventSubscriber/EnforceUserFieldsSubscriber.php: 23 added lines, 11 removed lines. Original line number Diff line number Diff line Loading @@ -10,11 +10,11 @@ use Drupal\Core\Url; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\KernelEvents; /** * Class EnforceUserFieldsSubscriber. * Event subscriber for check that user have filled all required fields. */ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { Loading Loading @@ -62,8 +62,10 @@ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { * The route match. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack. * @param \Drupal\Core\Messenger\MessengerInterface * @param \Drupal\Core\Messenger\MessengerInterface $messenger * The messenger. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory service. */ public function __construct(AccountProxyInterface $current_user, RouteMatchInterface $route_match, RequestStack $request_stack, MessengerInterface $messenger, ConfigFactoryInterface $config_factory) { $this->currentUser = $current_user; Loading @@ -73,14 +75,18 @@ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { $this->globalConfig = $config_factory->get('enforce_user_fields.settings'); } public function checkForUserFields(GetResponseEvent $event) { /** * Check that user have filled all required fields. */ public function checkForUserFields(RequestEvent $event) { $route_name = $this->routeMatch->getRouteName(); if ($this->currentUser->isAnonymous() || enforce_user_fields_user_has_admin_role($this->currentUser) || in_array($route_name, [ $skip_route = in_array($route_name, [ 'entity.user.edit_form', 'user.logout', 'image.style_public', 'image.style_private', ])) { ]); if ($this->currentUser->isAnonymous() || enforce_user_fields_user_has_admin_role($this->currentUser) || $skip_route) { return; } $is_ajax = $this->requestStack->getCurrentRequest()->isXmlHttpRequest(); Loading @@ -89,20 +95,26 @@ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { } if (!empty($_SESSION['enforce_user_fields'])) { $this->messenger->addMessage($this->globalConfig->get('message'), MessengerInterface::TYPE_ERROR); $event->setResponse(new RedirectResponse(Url::fromRoute('entity.user.edit_form', [ $route_params = [ 'user' => $this->currentUser->id(), 'destination' => Url::fromRouteMatch($this->routeMatch)->toString(), ])->toString())); ]; // If a user used one-time login token, append the token to the destination. $session_key = 'pass_reset_' . $this->currentUser->id(); if (isset($_SESSION[$session_key])) { $route_params['pass-reset-token'] = $_SESSION[$session_key]; } $redirect_url = Url::fromRoute('entity.user.edit_form', $route_params); $event->setResponse(new RedirectResponse($redirect_url->toString())); } } /** * {@inheritdoc} */ static function getSubscribedEvents() { $events[KernelEvents::REQUEST][] = array('checkForUserFields'); public static function getSubscribedEvents() { $events[KernelEvents::REQUEST][] = ['checkForUserFields']; return $events; } } Loading
src/EventSubscriber/EnforceUserFieldsSubscriber.php +23 −11 Changes for src/EventSubscriber/EnforceUserFieldsSubscriber.php: 23 added lines, 11 removed lines. Original line number Diff line number Diff line Loading @@ -10,11 +10,11 @@ use Drupal\Core\Url; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\KernelEvents; /** * Class EnforceUserFieldsSubscriber. * Event subscriber for check that user have filled all required fields. */ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { Loading Loading @@ -62,8 +62,10 @@ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { * The route match. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack. * @param \Drupal\Core\Messenger\MessengerInterface * @param \Drupal\Core\Messenger\MessengerInterface $messenger * The messenger. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory service. */ public function __construct(AccountProxyInterface $current_user, RouteMatchInterface $route_match, RequestStack $request_stack, MessengerInterface $messenger, ConfigFactoryInterface $config_factory) { $this->currentUser = $current_user; Loading @@ -73,14 +75,18 @@ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { $this->globalConfig = $config_factory->get('enforce_user_fields.settings'); } public function checkForUserFields(GetResponseEvent $event) { /** * Check that user have filled all required fields. */ public function checkForUserFields(RequestEvent $event) { $route_name = $this->routeMatch->getRouteName(); if ($this->currentUser->isAnonymous() || enforce_user_fields_user_has_admin_role($this->currentUser) || in_array($route_name, [ $skip_route = in_array($route_name, [ 'entity.user.edit_form', 'user.logout', 'image.style_public', 'image.style_private', ])) { ]); if ($this->currentUser->isAnonymous() || enforce_user_fields_user_has_admin_role($this->currentUser) || $skip_route) { return; } $is_ajax = $this->requestStack->getCurrentRequest()->isXmlHttpRequest(); Loading @@ -89,20 +95,26 @@ class EnforceUserFieldsSubscriber implements EventSubscriberInterface { } if (!empty($_SESSION['enforce_user_fields'])) { $this->messenger->addMessage($this->globalConfig->get('message'), MessengerInterface::TYPE_ERROR); $event->setResponse(new RedirectResponse(Url::fromRoute('entity.user.edit_form', [ $route_params = [ 'user' => $this->currentUser->id(), 'destination' => Url::fromRouteMatch($this->routeMatch)->toString(), ])->toString())); ]; // If a user used one-time login token, append the token to the destination. $session_key = 'pass_reset_' . $this->currentUser->id(); if (isset($_SESSION[$session_key])) { $route_params['pass-reset-token'] = $_SESSION[$session_key]; } $redirect_url = Url::fromRoute('entity.user.edit_form', $route_params); $event->setResponse(new RedirectResponse($redirect_url->toString())); } } /** * {@inheritdoc} */ static function getSubscribedEvents() { $events[KernelEvents::REQUEST][] = array('checkForUserFields'); public static function getSubscribedEvents() { $events[KernelEvents::REQUEST][] = ['checkForUserFields']; return $events; } }