You are here

private.module in Private 7

Same filename and directory in other branches
  1. 5 private.module
  2. 6 private.module
  3. 7.2 private.module

A tremendously simple access control module -- it allows users to mark individual nodes as private; users with 'access private content' perms can read these nodes, while others cannot.

File

private.module
View source
<?php

/**
 * @file
 * A tremendously simple access control module -- it allows users to mark
 * individual nodes as private; users with 'access private content' perms can
 * read these nodes, while others cannot.
 */
define('PRIVATE_DISABLED', 0);
define('PRIVATE_ALLOWED', 1);
define('PRIVATE_AUTOMATIC', 2);
define('PRIVATE_ALWAYS', 3);
define('PRIVATE_GRANT_ALL', 1);

/**
 * Implements hook_enable().
 *
 * A node access module needs to force a rebuild of the node access table
 * when it is enabled to ensure that things are set up.
 */
function private_enable() {
  node_access_needs_rebuild(TRUE);
}

/**
 * Implements hook_disable().
 *
 * A node access module needs to force a rebuild of the node access table
 * when it is disabled to ensure that its entries are removed from the table.
 */
function private_disable() {
  private_disabling(TRUE);
  node_access_needs_rebuild(TRUE);
}

/**
 * Simple function to make sure we don't respond with grants when disabling
 * ourselves.
 */
function private_disabling($set = NULL) {
  static $disabling = FALSE;
  if ($set !== NULL) {
    $disabling = $set;
  }
  return $disabling;
}

/**
 * Implements hook_permission().
 *
 * In this example, we will use a simple permission to determine whether a user
 * has access to "private" content. This permission is defined here.
 */
function private_permission() {
  return array(
    'mark content as private' => array(
      'title' => t('Mark content as private'),
      'description' => t('Make content only viewable by people with access to view private content'),
    ),
    'access private content' => array(
      'title' => t('Access private content'),
      'description' => t('Access any content marked as private'),
    ),
    'edit private content' => array(
      'title' => t('Edit private content'),
      'description' => t('Edit content marked as private'),
    ),
  );
}

/**
 * Implements hook_node_grants().
 *
 * Tell the node access system what GIDs the user belongs to for each realm.
 * In this example, we are providing two realms: the example realm, which
 * has just one group id (1) and the user is either a member or not depending
 * upon the operation and the access permission set.
 *
 * We are also setting up a realm for the node author, though, to give it
 * special privileges. That has 1 GID for every UID, and each user is
 * automatically a member of the group where GID == UID.
 *
 */
function private_node_grants($account, $op) {

  // First grant a grant to the author for own content.
  $grants['private_author'] = array(
    $account->uid,
  );
  if ($op == 'view' && user_access('access private content', $account)) {
    $grants['private_view'] = array(
      PRIVATE_GRANT_ALL,
    );
  }
  if (($op == 'update' || $op == 'delete') && user_access('edit private content', $account)) {
    $grants['private_edit'] = array(
      PRIVATE_GRANT_ALL,
    );
  }
  return $grants;
}

/**
 * Implements hook_node_access_records().
 *
 * All node access modules must implement this hook. If the module is
 * interested in the privacy of the node passed in, return a list
 * of node access values for each grant ID we offer.
 */
function private_node_access_records($node) {
  if (private_disabling()) {
    return;
  }

  // We only care about the node if it's been marked private. If not, it is
  // treated just like any other node and we completely ignore it.
  if (isset($node->private) && $node->private == 1) {
    $grants = array();
    $grants[] = array(
      'realm' => 'private_view',
      'gid' => PRIVATE_GRANT_ALL,
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
      'priority' => 0,
    );
    $grants[] = array(
      'realm' => 'private_edit',
      'gid' => PRIVATE_GRANT_ALL,
      'grant_view' => 1,
      'grant_update' => 1,
      'grant_delete' => 1,
      'priority' => 0,
    );
    $grants[] = array(
      'realm' => 'private_author',
      'gid' => $node->uid,
      'grant_view' => 1,
      'grant_update' => 1,
      'grant_delete' => 1,
      'priority' => 0,
    );
    return $grants;
  }
}

/**
 * Implements hook_form_alter().
 *
 * This module adds a simple checkbox to the node form labeled private. If the
 * checkbox is labelled, only the node author and users with 'access private content'
 * privileges may see it.
 */
function private_form_alter(&$form, &$form_state, $form_id) {
  if (!empty($form['#node_edit_form'])) {
    $node = $form['#node'];
    $default = variable_get('private_' . $node->type, PRIVATE_ALLOWED);
    if ($default != PRIVATE_DISABLED || !empty($node->private)) {
      if (empty($node->nid)) {
        $privacy = $default > PRIVATE_ALLOWED;
      }
      else {
        $privacy = isset($node->private) ? $node->private : 0;
      }
      if (user_access('mark content as private') && $default != PRIVATE_ALWAYS) {
        if (user_access('administer nodes')) {
          $form['options']['private'] = array(
            '#type' => 'checkbox',
            '#title' => t('Make this post private'),
            '#attributes' => array(
              'title' => t('When checked, only users with proper access permissions will be able to see this post.'),
            ),
            '#default_value' => $privacy,
          );
        }
        else {
          $form['private'] = array(
            '#type' => 'checkbox',
            '#title' => t('Make this post private'),
            '#attributes' => array(
              'title' => t('When checked, only users with proper access permissions will be able to see this post.'),
            ),
            '#default_value' => $privacy,
            '#weight' => 99,
          );
        }
      }
      else {
        $form['private'] = array(
          '#type' => 'value',
          '#value' => $privacy,
        );
      }
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function private_form_node_type_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['type'])) {
    $form['workflow']['private'] = array(
      '#type' => 'radios',
      '#title' => t('Privacy'),
      '#options' => array(
        PRIVATE_DISABLED => t('Disabled (always public)'),
        PRIVATE_ALLOWED => t('Enabled (public by default)'),
        PRIVATE_AUTOMATIC => t('Enabled (private by default)'),
        PRIVATE_ALWAYS => t('Hidden (always private)'),
      ),
      '#default_value' => variable_get('private_' . $form['#node_type']->type, PRIVATE_ALLOWED),
    );
  }
}

/**
 * Implements hook_node_load().
 */
function private_node_load($nodes, $types) {
  $result = db_query('SELECT * FROM {private} WHERE nid IN(:nids)', array(
    ':nids' => array_keys($nodes),
  ));
  foreach ($result as $record) {
    $nodes[$record->nid]->private = $record->private;
  }
}

/**
 * Implements hook_node_delete().
 */
function private_node_delete($node) {
  db_delete('private')
    ->condition('nid', $node->nid)
    ->execute();
}

/**
 * Implements hook_node_insert().
 */
function private_node_insert($node) {
  private_node_update($node);
}

/**
 * Implements hook_node_update().
 */
function private_node_update($node) {
  if (isset($node->private)) {
    db_merge('private')
      ->key(array(
      'nid' => $node->nid,
    ))
      ->fields(array(
      'nid' => $node->nid,
      'private' => (int) $node->private,
    ))
      ->execute();
  }
}

/**
 * Implements hook_node_view().
 */
function private_node_view($node, $view_mode) {
  if (isset($node->private) && $node->private == 1) {
    $links['private_icon']['title'] = theme('private_node_link');
    $links['private_icon']['html'] = TRUE;
    $node->content['links']['private'] = array(
      '#theme' => 'links__node__private',
      '#links' => $links,
      '#attributes' => array(
        'class' => array(
          'links',
          'inline',
        ),
      ),
    );
  }
}

/**
 * Implements hook_theme().
 */
function private_theme() {
  return array(
    'private_node_link' => array(
      'variables' => array(),
    ),
  );
}

/**
 * Custom theme function
 * @see private_theme()
 */
function theme_private_node_link() {
  $vars = array(
    'path' => drupal_get_path('module', 'private') . '/icon_key.gif',
    'width' => '16',
    'height' => '16',
    'alt' => t('Private'),
    'title' => t('This content is private.'),
  );
  return theme('image', $vars);
}

/**
 * Implements hook_action_info().
 */
function private_action_info() {
  return array(
    'private_set_private_action' => array(
      'type' => 'node',
      'label' => t('Make post private'),
      'configurable' => FALSE,
      'triggers' => array(
        'node_insert',
        'node_update',
      ),
    ),
    'private_set_public_action' => array(
      'type' => 'node',
      'label' => t('Make post public'),
      'configurable' => FALSE,
      'triggers' => array(
        'node_insert',
        'node_update',
      ),
    ),
  );
}

/**
 * Implementation of a Drupal action.
 */
function private_set_public_action(&$node, $context = array()) {
  $node->private = FALSE;
  $nids = array(
    $node->nid,
  );
  private_node_mark_public($nids);
}

/**
 * Implementation of a Drupal action.
 */
function private_set_private_action(&$node, $context = array()) {
  $node->private = TRUE;
  $nids = array(
    $node->nid,
  );
  private_node_mark_private($nids);
}

/**
 * Implements hook_node_operations().
 */
function private_node_operations() {
  $operations = array(
    'private_mark_as_private' => array(
      'label' => t('Mark as private'),
      'callback' => 'private_node_mark_private',
    ),
    'private_mark_as_public' => array(
      'label' => t('Mark as public'),
      'callback' => 'private_node_mark_public',
    ),
  );
  return $operations;
}

/**
 * Callback for 'Mark as private' node operation
 */
function private_node_mark_private($nids) {
  foreach ($nids as $nid) {
    db_merge('private')
      ->key(array(
      'nid' => $nid,
    ))
      ->fields(array(
      'nid' => $nid,
      'private' => 1,
    ))
      ->execute();
  }
}

/**
 * Callback for 'Mark as public' node operation
 */
function private_node_mark_public($nids) {
  foreach ($nids as $nid) {
    db_merge('private')
      ->key(array(
      'nid' => $nid,
    ))
      ->fields(array(
      'nid' => $nid,
      'private' => 0,
    ))
      ->execute();
  }
}

/**
 * Tell Views that we're down with it, yo.
 */
function private_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'private'),
  );
}

Functions

Namesort descending Description
private_action_info Implements hook_action_info().
private_disable Implements hook_disable().
private_disabling Simple function to make sure we don't respond with grants when disabling ourselves.
private_enable Implements hook_enable().
private_form_alter Implements hook_form_alter().
private_form_node_type_form_alter Implements hook_form_FORM_ID_alter().
private_node_access_records Implements hook_node_access_records().
private_node_delete Implements hook_node_delete().
private_node_grants Implements hook_node_grants().
private_node_insert Implements hook_node_insert().
private_node_load Implements hook_node_load().
private_node_mark_private Callback for 'Mark as private' node operation
private_node_mark_public Callback for 'Mark as public' node operation
private_node_operations Implements hook_node_operations().
private_node_update Implements hook_node_update().
private_node_view Implements hook_node_view().
private_permission Implements hook_permission().
private_set_private_action Implementation of a Drupal action.
private_set_public_action Implementation of a Drupal action.
private_theme Implements hook_theme().
private_views_api Tell Views that we're down with it, yo.
theme_private_node_link Custom theme function

Constants

Namesort descending Description
PRIVATE_ALLOWED
PRIVATE_ALWAYS
PRIVATE_AUTOMATIC
PRIVATE_DISABLED @file A tremendously simple access control module -- it allows users to mark individual nodes as private; users with 'access private content' perms can read these nodes, while others cannot.
PRIVATE_GRANT_ALL