This patch adds support for hook_node_operations to allow permissions to be set in bulk on the admin/content/node page like other operations such as promote, sticky, etc.

Note that the patch also removes the close php tag ?> which is not needed for module code and actually not recommended.

CommentFileSizeAuthor
node_operations.patch2.54 KBdldege

Comments

deekayen’s picture

Version: 5.x-1.x-dev »
Status: Needs review » Needs work

This will go in Drupal 6 and I'd appreciate it if you'd re-roll it for that. Actually, it's been the only thing hanging me up from committing the Drupal 6 changes.

In particular, the global for $form_values isn't valid anymore and I don't think there ever was a valid check against user_access('administer content'), so I'm assuming that's some custom setting on your site.

deekayen’s picture

Assigned: Unassigned » deekayen

Nevermind, I've already started doing it.

deekayen’s picture

Status: Needs work » Patch (to be ported)

Alright, I'm giving up again. My changes to the source don't work - I get this error

notice: Undefined index: grant-6-edit in /Users/davidnorman/Sites/drupal6/modules/node/node.admin.inc on line 314.

In this code, the arguments are incomplete, but I also think the way you had it before doesn't look "proper" with the hijacking of a global to set a callback. The exit() in here doesn't ever get called to spit out the value of $GLOBALS, so the way I'm doing this callback isn't correct either since the callback isn't ever called.

I think the easiest way to get this implemented is to ditch the menu labels that separate each role out to separate sections of permissions, but I like the thinking there so I want to make that feature work.

function node_privacy_byrole_node_operations() {
  $operations = array();

  foreach (user_roles() as $rid => $role) {
    $grants = array();
    $revokes = array();
    foreach (array('view', 'edit', 'delete') as $priv) {
      $grants['grant-'.$rid.'-'.$priv] = t('Grant !priv for !role', array('!priv' => $priv, '!role' => $role));
      $revokes['revoke-'.$rid.'-'.$priv] = t('Revoke !priv for !role', array('!priv' => $priv, '!role' => $role));
    }
    $operations[t('Access for role !role', array('!role' => $role))] = array(
      'label' => array_merge($grants, $revokes),
      'callback' => 'node_privacy_byrole_node_operations_update',
      'callback arguments' => array('rid' => $role)
    );
  }

  return $operations;
}

function node_privacy_byrole_node_operations_update($nodes, $rid) {
  exit(var_dump($GLOBALS));
  $roles = user_roles();
  $sql = 'UPDATE {node_privacy_byrole} SET ';
  switch ($priv) {
    case 'view' :
      $sql .= 'grant_view = %d';
      break;
    case 'edit' :
      $sql .= 'grant_update = %d';
      break;
    case 'delete' :
      $sql .= 'grant_delete = %d';
      break;
  }
  $sql .= ' WHERE nid IN(%s) AND gid = %d AND realm = "node_privacy_byrole_role"';
  db_query($sql, (int)$op, implode(',', $nodes), $rid);
}
deekayen’s picture

Assigned: deekayen » Unassigned
deekayen’s picture

Version: » 5.x-1.x-dev
Status: Patch (to be ported) » Fixed

I committed the original patch.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

deekayen’s picture

It turns out this patch only sort of worked. Fixes are in CVS revisions 1.28.2.12 and 1.32.2.6. I wasn't able to figure out how to salvage the optgroup nesting in 6.x, so it is kind of ugly now.

deekayen’s picture

For future reference, hook_node_access_grants() also needed to be called in addition to the update to the node_privacy_byrole table. While node_privacy_byrole table stores what should be checked off in the checkbox, node_access still needs to be told what to set for other permissions checks, and hook_node_access_grants() is the way contrib modules do that.

The only instance where I saw that hook was invoked was during node_save(). Since node_save() also activates hook_nodeapi() update, it was actually much simpler to just node_load(), mod the one object value and node_save().