简体   繁体   English

如何根据用户角色编写重定向?

[英]How to write a redirect based on user role?

I'm having an issue with writing a redirect based on whether or not the current user's role matches the one listed or not 我在根据当前用户的角色是否与列出的角色匹配来编写重定向时遇到问题

I've been trying to find solutions related to getting the current path for Drupal 8. I'm not sure if that is one of the issues or not. 我一直在努力寻找与获取Drupal 8的当前路径相关的解决方案。我不确定这是否是问题之一。

class FacultyStaffRedirectSubscriber implements EventSubscriberInterface {


  public function checkForRedirection(GetResponseEvent $event) {
    $request = \Drupal::request();
    $requestUrl = \Drupal::service('path.current')->getPath();
    $current_user = \Drupal::currentUser();
    $roles = $current_user->getRoles();

    if ($requestUrl == '/faculty-staff') {
        if (
          !in_array('faculty', $roles) ||
          !in_array('staff', $roles)
        ) {

            $path =  '/404'; // \Drupal::service('path.alias_manager')->getAliasByPath('/404')
        $response = new RedirectResponse($path);
        $response->send();
        return;
          /* $event->setResponse(new RedirectResponse($path, 302)); */
            }
    }
  }
 public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('checkForRedirection');
    return $events;
  }

} 

What I'm wanting is to redirect a user to 404 if they attempt to access the path without having the proper role assigned to them. 我想要的是如果用户尝试访问路径而没有分配适当的角色,则将用户重定向到404。 Currently, nothing happens at all and logged in users can see the page. 目前,没有任何事情发生,登录用户可以看到该页面。

If I got this correctly, you would like to return 404 or 403 error if user does not have permission to see the page. 如果我正确地得到了这个,如果用户没有查看该页面的权限,您希望返回404或403错误。 There is lot of options to do this. 有很多选择可以做到这一点。 I am listing just few. 我列举的很少。

Among contributed modules I usually use permission by term . 在贡献的模块中,我通常使用术语许可 You can simply add term field to your content, set role permissions per term and you are ready to go (check their docs and tutorials for more info). 您可以简单地将术语字段添加到您的内容中,设置每个术语的角色权限,然后您就可以开始了(查看他们的文档和教程以获取更多信息)。 For more complex structures there are organic groups but usually this is overkill for most cases (but it is very cool module doe). 对于更复杂的结构,有有机基团,但对于大多数情况通常这是过度杀伤(但它是非常酷的模块doe)。 There is a lot of other modules like: 还有很多其他模块,如:

For custom routes you can simply determine permission for access and give that permission to proper role: 对于自定义路由,您只需确定访问权限并授予适当角色的权限:

e_index.content:
  path: '/your/path'
  defaults:
    _title: 'Title'
  requirements:
    _permission: 'your permission'

You can use hook_node_access() or hook_entity_access() if your path is representing an entity. 如果路径表示实体,则可以使用hook_node_access()或hook_entity_access()。

/**
 * Implements hook_node_access().
 */
function yourmodule_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account) {

  if ($your_condition) {
    ...
    $access_value = TRUE/FALSE;
  }

  if ($access_value === TRUE) {
    $access = new AccessResultAllowed();
  } elseif ($access_value === FALSE) {
    $access = new AccessResultForbidden();
  } else {
    $access = new AccessResultNeutral();
  }

  $access->addCacheableDependency($node);
  return $access;
}

Or you can simply throw Symfony exception: 或者你可以简单地抛出Symfony异常:

use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class MyClass {

  public fucntion myMethod() {
    throw new AccessDeniedHttpException();
    // or
    throw new NotFoundHttpException();
  } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM