Commit 0cd5e096 authored by Kurucz István's avatar Kurucz István
Browse files

Issue #3229727 by singularo, nevergone: Masquerade is interfered with by redirection

parent 5c736b2c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
  "license": "GPL-2.0-or-later",
  "minimum-stability": "dev",
  "require-dev": {
    "drupal/masquerade": "^2.0@beta",
    "drupal/token": "^1.10"
  }
}
+5 −0
Original line number Diff line number Diff line
@@ -26,6 +26,11 @@ function redirect_after_logout_help($route_name, RouteMatchInterface $route_matc
 * Implements hook_user_logout().
 */
function redirect_after_logout_user_logout($account) {
  $session = \Drupal::service('session');
  if (isset($session) && $session->has('masquerading')) {
    return;
  }

  if ($account->hasPermission('redirect user after logout')) {
    $config = \Drupal::config('redirect_after_logout.settings');
    $logout_destination = $config->get('destination');
+63 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\redirect_after_logout\Functional;

use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;

/**
 * Test redirecting with masquerade module.
 *
 * @group redirect_after_logout
 */
class MasqueradeTest extends TestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'masquerade',
    'node',
    'redirect_after_logout',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    // Add user login block and account menu
    $this->placeBlock('masquerade', [
      'id' => $this->defaultTheme . '_' . strtolower($this->randomMachineName(8)),
      'label' => 'masquerade',
    ]);
    $this->placeBlock('system_menu_block:account', [
      'id' => $this->defaultTheme . '_' . strtolower($this->randomMachineName(8)),
      'label' => 'system_menu_block:account',
    ]);
    // Add neccessary permission for authenticated user role.
    $this->grantPermissions(Role::load(RoleInterface::AUTHENTICATED_ID), ['masquerade as any user']);
  }

  /**
   * Avoid redirect, if masquerade module is enabled.
   */
  public function testRedirectingWithMasquerade() {
    $message = $this->randomMachineName();
    $this->drupalLogin($this->adminUser);
    $this->setRedirectConfig('/foobar-example', $message);
    $node = $this->drupalCreateNode([
      'type' => 'page',
      'title' => 'Node for masquerade test',
    ]);
    $this->drupalGet($node->toUrl()->toString());
    // Masquerade regular user.
    $edit = [
      'masquerade_as' => $this->regularUser->getAccountName(),
    ];
    $this->submitForm($edit, 'Switch');
    // Unmasquerade and check path.
    $this->clickLink('Unmasquerade');
    $this->assertSession()->addressNotEquals('/foobar-example');
  }
}