Drupal 7:通过路径限制访问

Dyl*_*mes 2 php drupal drupal-7

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

你能否建议任何可以限制以下内容的机制:

members-area/editors/*仅限于具有"编辑"角色的用户.

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

谢谢

Cli*_*ive 7

你需要一个自定义模块,这不是太难.这将是它的关键:

// 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'),
  );
}
Run Code Online (Sandbox Code Playgroud)