![](/img/trans.png)
[英]drupal bootstrap script: how to get list of all nodes of type x?
[英]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.