You are here

node_view_permissions.module in Node View Permissions 7

Same filename and directory in other branches
  1. 8 node_view_permissions.module

File

node_view_permissions.module
View source
<?php

/**
 * @file
 * Provides permissions defenition forcontent access.
 */
require_once 'includes/node_view_permissions.api.inc';

/**
 * Implements hook_menu().
 */
function node_view_permissions_menu() {
  $items = array();
  $items['admin/config/content/node-view-permissions'] = array(
    'title' => 'Node view permissions',
    'description' => 'Adds extra permissions "View own content" and "View any content" for each content type.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_view_permissions_settings_page_form',
    ),
    'file' => 'includes/pages/node_view_permissions.settings.inc',
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function node_view_permissions_permission() {
  $perms = array();
  if (node_view_permissions_get_configured_types() != NULL) {
    foreach (node_view_permissions_get_configured_types() as $type) {
      $info = node_type_get_type($type);
      $perms += array(
        "view own {$type} content" => array(
          'title' => t('%type_name: View own content', array(
            '%type_name' => $info->name,
          )),
        ),
        "view any {$type} content" => array(
          'title' => t('%type_name: View any content', array(
            '%type_name' => $info->name,
          )),
        ),
      );
    }
  }
  return $perms;
}

/**
 * Implements hook_node_access_records().
 */
function node_view_permissions_node_access_records($node) {
  $grants = array();

  // We only care about the node if it's published.
  // We don't want to override view published permissions.
  if ($node->status == 1) {
    $grants[] = array(
      'realm' => 'view_any_' . $node->type . '_content',
      'gid' => 1,
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
      'priority' => variable_get('node_view_permissions_override', FALSE) ? 1 : 0,
    );
    $grants[] = array(
      'realm' => 'view_own_' . $node->type . '_content',
      'gid' => $node->uid,
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
      'priority' => variable_get('node_view_permissions_override', FALSE) ? 1 : 0,
    );
    return $grants;
  }
}

/**
 * Implements hook_node_grants().
 */
function node_view_permissions_node_grants($account, $op) {
  $grants = array();
  if ($op == 'view' && !empty($configure_types = node_view_permissions_get_configured_types())) {
    foreach ($configure_types as $type) {
      if (user_access('view any ' . $type . ' content', $account)) {
        $grants['view_any_' . $type . '_content'] = array(
          1,
        );
      }
      if (user_access('view own ' . $type . ' content', $account)) {
        $grants['view_own_' . $type . '_content'] = array(
          $account->uid,
        );
      }
    }
  }
  return $grants;
}