View source
<?php
require_once 'includes/node_view_permissions.api.inc';
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;
}
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;
}
function node_view_permissions_node_access_records($node) {
$grants = array();
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;
}
}
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;
}