Drupal - Limit which roles can view a node basing on its content type
You need to install Content Access module to add this access control.
This module allows you to manage permissions for content types by role and author. It allows you to specifiy custom view, edit and delete permissions for each content type.
Here is a simple way protect node pages (node/NID) from unprivileged users.
/**
* Implements hook_menu_alter().
*/
function mymodule_menu_alter(&$items) {
$items['node/%node']['access callback'] = 'mymodule_check_node_access';
}
/**
* Determines whether the current user may perform the operation on the node.
*/
function mymodule_check_node_access($op, $node) {
if ($node->type == 'protected_type' && !user_access('administer site configuration')) {
return FALSE;
}
return node_access($op, $node);
}
For drupal 7, try using hook_node_access():
/**
* Implements hook_node_access().
*/
function YOURMODULE_node_access($node, $op, $account) {
if (
$node->type == 'ebook' &&
$op == 'view' &&
!in_array('MonthlySubscriber', $account->roles)
) { return NODE_ACCESS_DENY; }
return NODE_ACCESS_IGNORE;
}