Commit 068af6a9 authored by Léo Prada's avatar Léo Prada Committed by Léo Prada
Browse files

Issue #3022651 by SachinT1996, Nixou, abhaysaraf, rutel95: Support redirect for the listed pages

parent e4ba5b69
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -10,5 +10,6 @@ default_redirect_code: 307
add_noindex_header: false
destination_parameter_override: ''
match_noredirect_pages: ''
match_noredirect_negate: 0
redirect_to_destination: true
langcode: en
+4 −1
Original line number Diff line number Diff line
@@ -37,7 +37,10 @@ r4032login.settings:
      label: 'Custom final destination parameter'
    match_noredirect_pages:
      type: string
      label: 'Only the listed pages'
      label: 'Listed pages for which to redirect or not'
    match_noredirect_negate:
      type: integer
      label: 'Allow to negate the redirect pages condition'
    redirect_to_destination:
      type: boolean
      label: 'Redirect user to the page they tried to access after login'
+12 −4
Original line number Diff line number Diff line
@@ -158,14 +158,21 @@ function r4032login_form_system_site_information_settings_alter(&$form, FormStat
  $form['error_page']['r4032login']['matching_paths'] = [
    '#type' => 'details',
    '#title' => t('Skip redirect for matching pages'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#open' => TRUE,
  ];
  $form['error_page']['r4032login']['matching_paths']['r4032login_match_noredirect_negate'] = [
    '#type' => 'radios',
    '#options' => [
      t('Skip redirect for listed pages'),
      t('Allow redirect for listed pages'),
    ],
    '#default_value' => !empty($config->get('match_noredirect_negate')) ? $config->get('match_noredirect_negate') : 0,
  ];
  $form['error_page']['r4032login']['matching_paths']['r4032login_match_noredirect_pages'] = [
    '#type' => 'textarea',
    '#title' => '<span class="element-invisible">' . t('Only the listed pages') . '</span>',
    '#title' => '<span class="element-invisible">' . t('Pages') . '</span>',
    '#default_value' => $config->get('match_noredirect_pages'),
    '#description' => t('Instead of redirecting, the user will get an access defined response and see the standard login form. This may be useful when the response code is important - such as for removing outdated content from search engines.') . ' ' . t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", [
    '#description' => t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", [
      '%blog' => '/blog',
      '%blog-wildcard' => '/blog/*',
      '%front' => '<front>',
@@ -227,6 +234,7 @@ function r4032login_form_system_site_information_settings_form_submit($form, For
    ->set('destination_parameter_override', $form_state->getValue('r4032login_destination_parameter_override'))
    ->set('match_noredirect_pages', $form_state->getValue('r4032login_match_noredirect_pages'))
    ->set('add_noindex_header', $form_state->getValue('r4032login_add_noindex_header'))
    ->set('match_noredirect_negate', $form_state->getValue('r4032login_match_noredirect_negate'))
    ->save();
}

+3 −1
Original line number Diff line number Diff line
@@ -112,7 +112,8 @@ class R4032LoginSubscriber extends HttpExceptionSubscriberBase {

    // Check if the path should be ignored.
    if (($noRedirectPages = trim($config->get('match_noredirect_pages')))
      && $this->pathMatcher->matchPath($currentPath, $noRedirectPages)
      && (($this->pathMatcher->matchPath($currentPath, $noRedirectPages) && !$config->get('match_noredirect_negate'))
      || (!$this->pathMatcher->matchPath($currentPath, $noRedirectPages) && $config->get('match_noredirect_negate')))
    ) {
      return;
    }
@@ -141,6 +142,7 @@ class R4032LoginSubscriber extends HttpExceptionSubscriberBase {
          $destination = Url::fromUserInput($currentPath, [
            'absolute' => TRUE,
          ])->toString();

          if ($queryString = $request->getQueryString()) {
            $destination .= '?' . $queryString;
          }
+33 −1
Original line number Diff line number Diff line
@@ -41,12 +41,16 @@ class SkipRedirectTest extends BrowserTestBase {
   *   Response status code.
   * @param string $destination
   *   Resulting URL.
   * @param int $negate
   *   Negate the skip redirection condition.
   *
   * @dataProvider skipRedirectDataProvider
   *
   * @throws \Behat\Mink\Exception\ExpectationException
   */
  public function testSkipRedirect($path, $code, $destination) {
  public function testSkipRedirect($path, $code, $destination, $negate) {
    $this->config('r4032login.settings')->set('match_noredirect_negate', $negate)->save();

    $this->drupalGet($path);
    $this->assertSession()->statusCodeEquals($code);
    $this->assertSession()->addressEquals($destination);
@@ -61,21 +65,49 @@ class SkipRedirectTest extends BrowserTestBase {
        'admin/config/development',
        403,
        'admin/config/development',
        0,
      ],
      [
        'admin/config/development',
        200,
        'user/login',
        1,
      ],
      [
        'admin/config',
        200,
        'user/login',
        0,
      ],
      [
        'admin/config',
        403,
        'admin/config',
        1,
      ],
      [
        'admin/modules',
        403,
        'admin/modules',
        0,
      ],
      [
        'admin/modules',
        200,
        'user/login',
        1,
      ],
      [
        'admin/modules/uninstall',
        200,
        'user/login',
        0,
      ],
      [
        'admin/modules/uninstall',
        403,
        'admin/modules/uninstall',
        1,
      ],
    ];
  }