簡體   English   中英

php / Drupal-列出給定用戶有權編輯的所有節點

[英]php/Drupal - list all nodes that a given user to permission to edit

我有一個列出系統上所有節點的函數。 我想對此進行改進,以僅顯示當前用戶能夠編輯的節點-使用API​​或SQL語句。 (Drupal 6)

function fnGetNodeTypes($typeOfNodes) {
    $string = "";
    $types_of_nodes  = array_keys(node_get_types());
    $string .= "<select name='typeOfNodes'>";
    $string .= "<option value=''>Please select</option> ";
    $string .= "<option value='all'>All</option> ";

    foreach($types_of_nodes as $node){      
        if($typeOfNodes == $node ){
            $selected = "selected";
        }
        else{
            $selected = "";
        }       
        $string .= "<option $selected value=\"" . $node . "\">" . $node ;
        $string .= "</option>\n";
    }
    $string .= "</select\n>";
    return $string;
}

更新:

按照@chx的建議,我嘗試弄亂用戶,users_roles和權限。 讓我知道是否有更多的Drupal方法可以做到這一點。

//----------------------------------------------
// Contruct select/option box of node types
//----------------------------------------------
function fnGetNodeTypes($typeOfNodes) {
    $string = "";
    $types_of_nodes  = array_keys(node_get_types());
    $string .= "<select name='typeOfNodes'>";
    $string .= "<option value=''>Please select</option> ";
    //$string .= "<option value='all'>All</option> ";
    foreach($types_of_nodes as $node_type){         
        if (fnInArray($node_type))
        {
            if($typeOfNodes == $node_type ){
                $selected = "selected";
            }
            else{
                $selected = "";
            }       
            $string .= "<option $selected value=\"" . $node_type . "\">" . $node_type ;
            $string .= "</option>\n";
        }
    }
    $string .= "</select\n>";
    return $string;
}

//---------------------------------------------------------------------
//  function fnInArray - see if user is allowed to edit this node type
//---------------------------------------------------------------------

function fnInArray($node_type)
{
    global $user;

    if ($user->name == 'admin') { return TRUE; }

    // get list of all nodes that user is allowed to access
    // 
    $string =   " SELECT permission.perm as permission_perm "  .
        " from users " .
        " join users_roles  on ( users_roles.uid = users.uid ) " .
        " join permission on (permission.rid = users_roles.rid) " .
        " where  users.name = '" . $user->name . "'";

    $result = db_query($string);
    while ($row = db_fetch_object($result)) {           
        $pieces = explode(", " , $row->permission_perm);        
        $node_name = "edit any " . trim($node_type) . " content";
        if (in_array($node_name, $pieces )) 
        {
            return TRUE;        
        }
        return FALSE;
    }
}

這是完全不可能做到的。 可以通過鈎子指定節點訪問權限,因此唯一的通用方法是檢索每個節點。 單。 節點。 並在其上運行node_access($node, 'update') 不太快。 您可以根據站點的設置和模塊的使用方式弄亂節點類型,權限,節點訪問表等。 如果我們假設控制節點的唯一事情就是權限,並且了解這一點,那么這種假設並不總是正確的,那么在Drupal 6及以下版本中(我懷疑從node_get_types()您沒有使用D7)您的確會遍歷node_get_types()並檢查user_access("edit own $type content") || user access("edit any $type content") user_access("edit own $type content") || user access("edit any $type content")但這不會太過分。

不太確定Drupal 6的正確方法(請檢查db_rewrite_sql ),但對於Drupal 7來說,不確定的是在構建查詢時,將addTag('node_access')添加到查詢中,這會將其限制為僅用戶有權編輯的節點。 如果轉到上面的db_rewrite_sql鏈接,請確保查看注釋。

db_query + db_rewrite_sql :僅返回允許登錄用戶查看的行。

$results = db_query(db_rewrite_sql($query), $args);

這就是http://drupal.org/project/module_grants的“模塊贈款監視器”模塊。 在項目頁面上:“單擊它可以顯示登錄的用戶在您站點上安裝的內容訪問模塊應用訪問控制后可以訪問(即查看,編輯)的所有內容的摘要”。 我今天安裝並測試了它,它似乎可以工作。 有人對這個模塊有意見或經驗嗎?

似乎也可以通過“視圖”或“規則”來實現……但這也許僅僅是因為一切似乎都可以實現……

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM