php / Drupal - 列出给定用户编辑权限的所有节点

时间:2011-03-12 22:46:59

标签: api drupal module

我有一个列出系统中所有节点的功能。我想对此进行优化,以仅显示当前用户能够编辑的节点 - 使用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;
    }
}

4 个答案:

答案 0 :(得分:1)

这是不可能做到的。可以通过钩子指定节点访问,因此唯一的泛型方法是检索每个。单。节点。并对它们运行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"),但这不会太过分。

答案 1 :(得分:0)

不太确定Drupal 6(检查db_rewrite_sql)的正确方法,但对于Drupal 7,在构建查询时,将addTag('node_access')添加到查询中,并将其限制为仅限节点用户有权编辑。如果您转到上面的db_rewrite_sql链接,请务必查看注释。

答案 2 :(得分:0)

db_query + db_rewrite_sql:仅返回允许登录用户查看的行。

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

答案 3 :(得分:0)

这是Module Grants Monitor模块用于http://drupal.org/project/module_grants的内容。从项目页面:“点击它可以显示登录用户在您的网站上安装的内容访问模块应用访问控制后访问的所有内容的摘要(即查看,编辑)”。我今天安装并测试了它,它似乎工作。有没有人对这个模块有任何意见或经验?

看起来这也应该可以通过视图或规则...但也许这只是因为一切似乎都可能与他们...