I used a code snippet found in the D7 api documentation for the hook_node_access hook.
This code will grant access to view "ebook" content to users that have the "view ebook" permission.
You need a new permission to control access by implementing hook_permission().
/**
* Implements hook_permission().
*/
function mymodule_permission() {
return array(
'view ebook' => array(
'title' => t('View Ebook'),
'description' => t('View Ebook nodes.'),
),
);
}
By implementing hook_node_access() Drupal can grant or deny the access to the node.
/**
* Implements hook_node_access().
*/
function mymodule_node_access($node, $op, $account) {
// Checks for an ebook node in view mode.
if (is_object($node) && $node->type === 'ebook' && $op === 'view') {
// Grants permission to view the node if the current user has an role
// with the permission 'view ebook'.
if (user_access('view ebook')) {
return NODE_ACCESS_ALLOW;
}
// Otherwise disallows access to view the node.
return NODE_ACCESS_DENY;
}
// For all other nodes and other view modes, don't affect the access.
return NODE_ACCESS_IGNORE;
}
Other permissions (edit, delete, etc) can be dealt with via the normal Drupal permissions.
Optionally you can remove the content from the admin overview by implementing hook_query_TAG_NAME_alter.
/**
* Implements hook_query_TAG_NAME_alter().
*/
function mymodule_query_node_admin_filter_alter(QueryAlterableInterface $query) {
if (!user_access('view ebook')) {
$query->condition('n.type', 'ebook', '!=');
}
}