You are here

node_privacy_byrole_quick_grant.module in node privacy byrole 5

Addon to Node Privacy By Role to mass grant access to a specific node type where Rebuild Permissions isn't available or appropriate.

Sponsored by Classic Graphics, Charlotte, NC

@author David Kent Norman

File

node_privacy_byrole_quick_grant.module
View source
<?php

/**
 * @file
 * Addon to Node Privacy By Role to mass grant access to a specific
 * node type where Rebuild Permissions isn't available or appropriate.
 *
 * Sponsored by Classic Graphics, Charlotte, NC
 *
 * @author David Kent Norman
 */
function node_privacy_byrole_quick_grant_help($section = '') {
  switch ($section) {
    case 'admin/node/node_privacy_byrole_quick_grant':
      return t('This only supports setting the node permission specifically to the node type and roles you select. It will not touch permissions for roles you do not select.');
  }
}

/**
 * Impelementation of hook_menu().
 *
 * @param bool $may_cache
 * @return array
 */
function node_privacy_byrole_quick_grant_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'title' => t('Quick grant'),
      'path' => 'admin/content/node_privacy_byrole_quick_grant',
      'description' => t('Quick fix for when permissions get mistakenly changed for a specific node type.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'node_privacy_byrole_quick_grant_admin_content',
      ),
      'access' => user_access('administer nodes'),
    );
  }
  return $items;
}

/**
 * @return array
 */
function node_privacy_byrole_quick_grant_admin_content() {
  $form = array();
  $node_types = array();
  $result = db_query('SELECT DISTINCT(type) FROM {node} ORDER BY type');
  while ($node_type = db_fetch_object($result)) {
    $node_types[$node_type->type] = $node_type->type;
  }
  $form['node_type'] = array(
    '#title' => t('Node type'),
    '#type' => 'select',
    '#options' => $node_types,
    '#required' => true,
  );
  $form['roles'] = array(
    '#title' => t('Roles'),
    '#type' => 'select',
    '#multiple' => true,
    '#options' => user_roles(),
    '#required' => true,
  );
  $form['grant'] = array(
    '#title' => 'Permission',
    '#type' => 'select',
    '#multiple' => true,
    '#options' => drupal_map_assoc(array(
      'view',
      'update',
      'delete',
    )),
    '#required' => true,
    '#description' => t('Permissions not selected will not be granted. Not granted is to be defined from deny since a grant in a different role will allow members of that role to view the node type.'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Change permissions'),
  );
  return $form;
}
function node_privacy_byrole_quick_grant_admin_content_submit($form_id, &$form_values) {
  $nodes = array();
  $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", $form_values['node_type']);
  $grant_view = empty($form_values['grant']['view']) ? 0 : 1;
  $grant_update = empty($form_values['grant']['update']) ? 0 : 1;
  $grant_delete = empty($form_values['grant']['delete']) ? 0 : 1;
  while ($node = db_fetch_object($result)) {
    reset($form_values['roles']);
    while (list(, $rid) = each($form_values['roles'])) {
      db_query("REPLACE INTO {node_privacy_byrole} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, 'node_privacy_byrole_role', {$grant_view}, {$grant_update}, {$grant_delete})", $node->nid, $rid);
    }
  }
  drupal_set_message(t('Updated node access permissions on node type !type', array(
    '!type' => $form_values['node_type'],
  )), 'status');
}

Functions

Namesort descending Description
node_privacy_byrole_quick_grant_admin_content
node_privacy_byrole_quick_grant_admin_content_submit
node_privacy_byrole_quick_grant_help @file Addon to Node Privacy By Role to mass grant access to a specific node type where Rebuild Permissions isn't available or appropriate.
node_privacy_byrole_quick_grant_menu Impelementation of hook_menu().