Drupal 7:通过路径限制访问

发布于 2024-12-08 18:39:56 字数 184 浏览 0 评论 0原文

我需要限制我网站的各个部分。我想通过限制对各种子路径的访问来做到这一点。路径访问模块实际上并不执行此操作。

您能否建议任何允许限制类似以下内容的机制:

members-area/editors/* 仅限具有“编辑”角色的用户。

也许有办法通过规则来做到这一点?我试过了,但找不到。

谢谢

I need to restrict various sections of my site. I would like to do this by restricting access to various subpaths. The Path Access module doesn't actually do this.

Can you suggest any mechanism that would allow for the restriction of something like:

members-area/editors/*
restricted to only users with 'editor' role.

Maybe there's a way to do this with rules? I've tried, but can't find one.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌倦 2024-12-15 18:39:56

为此,您需要一个自定义模块,这并不太困难。这将是它的关键:

// Implements hook_init()
function mymodule_init() {
  $restrictions = mymodule_get_restrictions();
  global $user;
  foreach ($restrictions as $path => $roles) {
    // See if the current path matches any of the patterns provided.
    if (drupal_match_path($_GET['q'], $path)) {
      // It matches, check the current user has any of the required roles
      $valid = FALSE;
      foreach ($roles as $role) {
        if (in_array($role, $user->roles)) {
          $valid = TRUE;
          break;
        }
      }

      if (!$valid) {
        drupal_access_denied();
      }
    }
  }
}

function mymodule_get_restrictions() {
  // Obviously this data could come from anywhere (database, config file, etc.)
  // This array will be keyed by path and contain an array of allowed roles for that path
  return array(
    'members-area/editors/*' => array('editor'),
    'another-path/*' => array('editor', 'other_role'),
  );
}

You'll need a custom module for this, it's not too difficult. This would be the crux of it:

// Implements hook_init()
function mymodule_init() {
  $restrictions = mymodule_get_restrictions();
  global $user;
  foreach ($restrictions as $path => $roles) {
    // See if the current path matches any of the patterns provided.
    if (drupal_match_path($_GET['q'], $path)) {
      // It matches, check the current user has any of the required roles
      $valid = FALSE;
      foreach ($roles as $role) {
        if (in_array($role, $user->roles)) {
          $valid = TRUE;
          break;
        }
      }

      if (!$valid) {
        drupal_access_denied();
      }
    }
  }
}

function mymodule_get_restrictions() {
  // Obviously this data could come from anywhere (database, config file, etc.)
  // This array will be keyed by path and contain an array of allowed roles for that path
  return array(
    'members-area/editors/*' => array('editor'),
    'another-path/*' => array('editor', 'other_role'),
  );
}
孤城病女 2024-12-15 18:39:56

路径访问模块为站点管理员提供了额外的一层
对 Drupal 站点所有页面的访问控制。

http://drupal.org/project/path_access

The Path Access module gives site administrators an additional layer
of access control to all pages of a Drupal site.

http://drupal.org/project/path_access

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文