Hello,

i wonder if it´s possible to create a list of all nodes that a user is allowed to edit.

Comments

jonas28’s picture

Status: Active » Closed (fixed)

Yes, it´s possible to to create a list of all nodes a user is allowed to edit:

<?php
global $user;
$links = db_query("SELECT * FROM {node_access} WHERE gid=$user->uid AND realm='nodeaccess_uid'");
while ($link = db_fetch_object($links)) {
  $allowed_node = node_load($link->nid);
  print '<a href="/node/' . $allowed_node->nid . '">' . $allowed_node->title . '</a><br />';
}
?>
ymmatt’s picture

This code didn't work for me, I prefer to use "db_rewrite_sql" as I think it is suggested by the developers.

I also like to use "pager_query" so that if you have a lot of editable nodes it is more manageable.

I believe that I expanded the above functionality quite a bit, especially if you use roles.

The following code works for me but it may not be pretty, clean it up as you wish:

<?php 
  global $user;

$roles = user_load(array('uid' => $user->uid));
unset($roles->roles[2]); //I don't care about 'authenticated' role

if (count($roles->roles) > 1) print "You have more than one role, this page only displays pages that you authored, and pages that you can edit according to the ". current($roles->roles) ." role that you are assigned to";

  $listlength="20";
  $urid = key($roles->roles);

if (count($roles->roles >= 1)) { //The user has at least one role associated with them
  $result1 = pager_query(db_rewrite_sql("SELECT n.nid FROM {node} n INNER JOIN {node_access} t ON t.nid=n.nid WHERE ( t.gid=$user->uid OR t.gid=$urid ) AND n.status=1 AND t.grant_update=1 ORDER BY n.sticky DESC, n.created DESC"), $listlength);
}
else { //The user has no roles associated with them
  $result1 = pager_query(db_rewrite_sql("SELECT n.nid FROM {node} n INNER JOIN {node_access} t ON t.nid=n.nid WHERE n.status=1 AND t.gid=$user->uid AND t.grant_update=1 ORDER BY n.sticky DESC, n.created DESC"), $listlength);
}

  while ($node = db_fetch_object($result1)) {
    $thisNode = node_load(array('nid' => $node->nid));
    $output2 .= l($thisNode->title, drupal_get_path_alias("node/".$thisNode->nid)); ;
    $output2 .= "<br><br>";
  };
  print $output2;
  print theme('pager', NULL, 20);
?>
process91’s picture

Thanks for your help! I thought I would change just a couple things about it. When I added it as the administrator I got several MySQL errors since the administrator can edit everything. So I added a line that checks for the administrator and also added some additional output lines.

  global $user;
if ($user->uid == 1) // This must be the administrator, who can edit everything.
print 'You are the administrator. You can edit everything. This page will not display all the content, use the content tab under the "Administration" section on the left to see what content you can edit.';

else
{
$roles = user_load(array('uid' => $user->uid));
unset($roles->roles[2]); //I don't care about 'authenticated' role

if (count($roles->roles) > 1) print "You have more than one role, this page only displays pages that you authored, and pages that you can edit according to the ". current($roles->roles) ." role that you are assigned to";

  $listlength="20";
  $urid = key($roles->roles);
if (count($roles->roles >= 1)) { //The user has at least one role associated with them
  $result1 = pager_query(db_rewrite_sql("SELECT n.nid FROM {node} n INNER JOIN {node_access} t ON t.nid=n.nid WHERE ( t.gid=$user->uid OR t.gid=$urid ) AND n.status=1 AND t.grant_update=1 ORDER BY n.sticky DESC, n.created DESC"), $listlength);
}
else { //The user has no roles associated with them
  $result1 = pager_query(db_rewrite_sql("SELECT n.nid FROM {node} n INNER JOIN {node_access} t ON t.nid=n.nid WHERE n.status=1 AND t.gid=$user->uid AND t.grant_update=1 ORDER BY n.sticky DESC, n.created DESC"), $listlength);
}

  while ($node = db_fetch_object($result1)) {
    $thisNode = node_load(array('nid' => $node->nid));
    
    $output2 .= l($thisNode->title, drupal_get_path_alias("node/".$thisNode->nid));

    //The following lines determine output format. You can comment a line out to remove it from being added to the list.
    $output2 .= ' - ';
    $output2 .= '<b>Type:</b> '.$thisNode->type.' ';
    $output2 .= '<b>Date Created:</b> '.date('m/d/y',$thisNode->created).' ';
    $output2 .= '<b>Last Modified:</b> '.date('m/d/y',$thisNode->changed).' ';

    $output2 .= "<br/><br/>";
  };
  print $output2;
  print theme('pager', NULL, 20);
}
shanefjordan’s picture

Looking through this, the issue I have is that it only displays for the very first group. Shouldn't this be setup to display for each group that i'm in. I will work on it and see what I can come up with.

Thanks,
Shane

shanefjordan’s picture

Once again, there is probably a better way to do this, but this works and displays it for all groups that the user has. One thing to note, is that this is giving me more pages than what I have content on. I imagine this is a setting somewhere in one of the functions, but i'm not sure which one and didn't dig that far into it.

<?php
  global $user;
if ($user->uid == 1) // This must be the administrator, who can edit everything.
print 'You are the administrator. You can edit everything. This page will not display all the content, use the content tab under the "Administration" section on the left to see what content you can edit.';

else
{
$roles = user_load(array('uid' => $user->uid));
unset($roles->roles[2]); //I don't care about 'authenticated' role

if (count($roles->roles) > 1) print "You have more than one role, this page only displays pages that you authored, and pages that you can edit according to the ". current($roles->roles) ." role that you are assigned to";

  $listlength="20";
  while (count($roles->roles) >= 1) {
    $urid = $urid.key($roles->roles).', ';
    unset($roles->roles[key($roles->roles)]);
  };
  $urid = substr($urid,0,strlen($urid)-2);

if (count($roles->roles >= 1)) { //The user has at least one role associated with them
  $result1 = pager_query(db_rewrite_sql("SELECT n.nid FROM {node} n INNER JOIN {node_access} t ON t.nid=n.nid WHERE ( t.gid=$user->uid OR t.gid in ($urid) ) AND n.status=1 AND t.grant_update=1 ORDER BY n.sticky DESC, n.created DESC"), $listlength);
}
else { //The user has no roles associated with them
  $result1 = pager_query(db_rewrite_sql("SELECT n.nid FROM {node} n INNER JOIN {node_access} t ON t.nid=n.nid WHERE n.status=1 AND t.gid=$user->uid AND t.grant_update=1 ORDER BY n.sticky DESC, n.created DESC"), $listlength);
}

  while ($node = db_fetch_object($result1)) {
    $thisNode = node_load(array('nid' => $node->nid));
    
    $output2 .= l($thisNode->title, drupal_get_path_alias("node/".$thisNode->nid));

    //The following lines determine output format. You can comment a line out to remove it from being added to the list.
    $output2 .= ' - ';
    $output2 .= '<b>Type:</b> '.$thisNode->type.' ';
    $output2 .= '<b>Date Created:</b> '.date('m/d/y',$thisNode->created).' ';
    $output2 .= '<b>Last Modified:</b> '.date('m/d/y',$thisNode->changed).' ';

    $output2 .= "<br/><br/>";
  };
  print $output2;
  print theme('pager', NULL, 20);
}
?>