将匿名用户重定向到登录页面


14

我想如果用户未登录并访问我网站上除REST VIEWS路由器以外的任何页面,我将他/她重定向到drupal 8中的登录页面,我找到了drupal 7的此解决方案,但没有找到Drupal 8的任何解决方案


2
在代码中,您将编写一个事件订阅者以对KernelEvents::REQUEST事件进行操作,然后将响应设置为登录页面的RedirectResponse。
mradcliffe

1
我想到了别的东西。您可以将403页面设置为/user,如果他们是匿名用户,它将带到/user/login,但是这样做的副作用是将经过身份验证的用户带到他们的用户个人资料,而不会告诉他们拒绝访问他们正在寻找的页面。
mradcliffe

1
使用以下模块为匿名用户添加登录重定向,它具有排除路径的设置,因此我认为您可以在其中添加其余路径。drupal.org/project/anonymous_redirect
Daniel Harper '18

1
这个模块也可以完成这项工作:drupal.org/project/anonymous_login
Joris Lucius

Answers:


21

您可以在预订KernelEvents :: REQUEST的自定义模块中与事件订阅者一起非常早地测试用户的状态。

首先,将事件订阅者注册mymodule.services.yml在模块文件夹中:

services:
  mymodule.event_subscriber:
    class: Drupal\mymodule\EventSubscriber\RedirectAnonymousSubscriber
    arguments: []
    tags:
      - {name: event_subscriber}

然后RedirectAnonymousSubscriber.php/src/EventSubscriber/文件夹中的模块中为您的自定义事件订阅者添加。

namespace Drupal\mymodule\EventSubscriber;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Event subscriber subscribing to KernelEvents::REQUEST.
 */
class RedirectAnonymousSubscriber implements EventSubscriberInterface {

  public function __construct() {
    $this->account = \Drupal::currentUser();
  }

  public function checkAuthStatus(GetResponseEvent $event) {

    if ($this->account->isAnonymous() && \Drupal::routeMatch()->getRouteName() != 'user.login') {

      // add logic to check other routes you want available to anonymous users,
      // otherwise, redirect to login page.
      $route_name = \Drupal::routeMatch()->getRouteName();
      if (strpos($route_name, 'view') === 0 && strpos($route_name, 'rest_') !== FALSE) {
        return;
      }

      $response = new RedirectResponse('/user/login', 301);
      $event->setResponse($response);
      $event->stopPropagation();
    }
  }

  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('checkAuthStatus');
    return $events;
  }

}

感谢@mradcliffe提供给活动订阅者的想法
oknate,2013年

1
他应该创建RedirectAnonymousSubscriber.php,我更新您的答案。
优素福

@oknate如果用户在/ registration或/ forget-password或/ logout路由上怎么办?它是否应该在if条件下也检查那些路线?按照这种方式,用户可能会在注册或请求新密码时被重定向。
Juyal Jee

2
这里的另一个重要细节是订户的“优先级”。我发现一些路由会在上述重定向生效之前解决。但是设置优先级(优先级越高,优先级越高)有助于解决该问题。在上面的示例中,使用array('checkAuthStatus',100)。有关订阅者和优先级的文档:symfony.com/doc/current/event_dispatcher.html
BWagner

6

在Drupal 8.3.3上,此代码导致无限重定向。我通过添加来解决此问题。

..
$response = new RedirectResponse('/user/login', 301);
$response->send();
..

6

首先,为您的活动订阅者创建服务 module-name.services.yml

代码 -

services:
    [MODULE-NAME]_event_subscriber:
        class: Drupal\MODULE-NAME\EventSubscriber\[Event-Subscriber-class]
        tags:
        - {name: event_subscriber}

modules/module-name/src/EventSubscriber目录内创建自己的eventsubscriber类:

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class Event-Subscriber-class implements EventSubscriberInterface {

  private $redirectCode = 301;

  public function checkForRedirection2(GetResponseEvent $event) {
    $account = \Drupal::currentUser(); 
    if (empty($account->id()) {
      $response = new RedirectResponse('/', $this->redirectCode);
      $response->send();
      exit(0);
    }
  }

  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('checkForRedirection2');
    return $events;
  }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.