开发者

Drupal 7: restrict access by path

开发者 https://www.devze.com 2023-04-12 03:05 出处:网络
I need to restrict various sections of my site. I woul开发者_如何学JAVAd like to do this by restricting access to various subpaths. The Path Access module doesn\'t actually do this.

I need to restrict various sections of my site. I woul开发者_如何学JAVAd 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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消