2011-10-10 59 views
2

我需要限制我的網站的各個部分。我想通過限制對各種子路徑的訪問來做到這一點。 Path Access模塊​​實際上沒有這樣做。Drupal 7:限制訪問路徑

你可以建議將允許的類似限制任何機制:

會員區/僅限於用戶「編輯」的角色編輯/ * 。

也許有辦法用規則做到這一點?我試過了,但找不到一個。

謝謝

回答

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

好,感謝這個,我會嘗試一下。唯一的問題是我不應該爲我當前的項目使用任何自定義代碼。 –

+0

你看過[Content Access模塊​​](http://drupal.org/project/content_access)嗎?你可能會擠壓你想要做的事情:-) – Clive