diff -u b/src/EventSubscriber/RestrictIpEventSubscriber.php b/src/EventSubscriber/RestrictIpEventSubscriber.php --- b/src/EventSubscriber/RestrictIpEventSubscriber.php +++ b/src/EventSubscriber/RestrictIpEventSubscriber.php @@ -31,25 +31,25 @@ protected $loggerFactory; /** - * The Module Handler service + * The Module Handler service. * * @var \Drupal\Core\Extension\ModuleHandlerInterface */ protected $moduleHandler; /** - * The Url Generator service + * The Url Generator service. * * @var \Drupal\Core\Routing\UrlGeneratorInterface */ protected $urlGenerator; - /** - * The Restrict IP configuration settings - * - * @var \Drupal\Core\Config\ImmutableConfig - */ - protected $config; + /** + * The config factory service. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; /** * The messenger service. @@ -79,8 +79,7 @@ $this->moduleHandler = $moduleHandler; $this->urlGenerator = $urlGenerator; $this->messenger = $messenger; - - $this->config = $configFactory->get('restrict_ip.settings'); + $this->configFactory = $configFactory; } /** @@ -97,6 +96,7 @@ public function checkIpRestricted(GetResponseEvent $event) { unset($_SESSION['restrict_ip']); + $restrict_config = $this->configFactory->get('restrict_ip.settings'); $this->restrictIpService->testForBlock(); @@ -104,28 +104,34 @@ { if($this->restrictIpService->getCurrentPath() != '/restrict_ip/access_denied') { - if($this->moduleHandler->moduleExists('dblog') && $this->config->get('dblog')) + if($this->moduleHandler->moduleExists('dblog') && $restrict_config->get('dblog')) { $this->loggerFactory->get('Restrict IP')->warning('Access to the path %path was blocked for the IP address %ip_address', ['%path' => $this->restrictIpService->getCurrentPath(), '%ip_address' => $this->restrictIpService->getCurrentUserIp()]); } - if($this->config->get('allow_role_bypass') && $this->config->get('bypass_action') === 'redirect_login_page') + if($restrict_config->get('allow_role_bypass') && $restrict_config->get('bypass_action') === 'redirect_login_page') { // Redirect the user to the change password page $url = Url::fromRoute('user.login'); $event->setResponse(new RedirectResponse($url->toString())); } - elseif(in_array($this->config->get('white_black_list'), [0, 1])) + elseif(in_array($restrict_config->get('white_black_list'), [0, 1])) { $url = Url::fromRoute('restrict_ip.access_denied_page'); $event->setResponse(new RedirectResponse($url->toString())); } - elseif($this->config->get('access_denied_message_status') !== 0) + elseif($restrict_config->get('access_denied_message_status') !== 0) { - $url = $this->urlGenerator->generateFromRoute(''); - $this->messenger->addError($this->config->get('access_denied_message')); + if($restrict_config->get('default_access_page') === 1) { + $access_denied_page = $this->configFactory->get('system.site')->get('page')['403']; + $url = Url::fromUserInput($access_denied_page,[])->toString(); + } else { + $url = $this->urlGenerator->generateFromRoute(''); + } + + $this->messenger->addError($restrict_config->get('access_denied_message')); $event->setResponse(new RedirectResponse($url)); } else { diff -u b/src/Form/ConfigForm.php b/src/Form/ConfigForm.php --- b/src/Form/ConfigForm.php +++ b/src/Form/ConfigForm.php @@ -298,6 +298,13 @@ ]; } + $form['access_denied_message'] = [ + '#title' => $this->t('Text to display when access is restricted.'), + '#type' => 'textarea', + '#default_value' => $config->get('access_denied_message'), + '#description' => $this->t('Text that will be shown when access is denied.'), + ]; + $form['default_access_page'] = [ '#title' => $this->t('Default access denied page'), '#type' => 'checkbox', @@ -305,20 +312,13 @@ '#description' => $this->t('When this box is checked, default Drupal access denied page is shown. You can modify it in basic site settings.'), ]; - $form['access_denied_message'] = [ - '#title' => $this->t('Message to display when access is restricted.'), - '#type' => 'textarea', - '#default_value' => $config->get('access_denied_message'), - '#description' => $this->t('Message that will be shown when access is denied.'), - ]; $form['access_denied_message_status'] = [ - '#title' => $this->t('Enables or disables access denied messsages'), + '#title' => $this->t('Enables or disables access denied notification.'), '#type' => 'checkbox', '#default_value' => $config->get('access_denied_message_status'), - '#description' => $this->t('When this box is checked, access denied messages will show.'), + '#description' => $this->t('When this box is checked, access denied notification will show.'), ]; - return parent::buildForm($form, $form_state); }