Drupal 7:通过路径限制访问


Drupal 7: restrict access by path

我需要限制我的网站的各个部分。我想通过限制对不同子路径的访问来实现这一点。Path Access模块实际上不会这样做。

你能建议一种机制来允许像

这样的限制吗?

会员区/编辑/*

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

谢谢

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

// 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'),
  );
}

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

http://drupal.org/project/path_access