hook_node_acess($node, $op, $account)node_access($op, $node, $account = NULL)/**
* Implements hook_node_access().
*/
function example_node_access($node, $op, $account) {
// Check for the content type.
if ($node->type == 'premium') {
// Check for the operation.
if ($op == 'create') {
// Get the account wrapper.
$account_wrapper = entity_metadata_wrapper('user', $account);
// If is premium let see the content, else deny.
if (!empty($account_wrapper->field_is_premium->value()) &&
$account_wrapper->field_is_premium->value()) {
return NODE_ACCESS_ALLOW;
}
else {
return NODE_ACCESS_DENY;
}
}
}
return NODE_ACCESS_IGNORE;
}$query->addTag('node_access');! No tag => security violation
hook_node_access_records($node);
hook_node_grants($account, $op);hook_node_access_records($node)node_save($node);node_access_acquire_grants($node);$grants = module_invoke_all('node_access_records', $node);node_access_write_grants($node, $grants, NULL, $delete);/**
* Implements hook_node_access_records().
*/
function hook_node_access_records($node) {
// We only care about the node if it has been marked private. If not, it is
// treated just like any other node and we completely ignore it.
if ($node->private) {
$grants = array();
// Only published nodes should be viewable to all users. If we allow access
// blindly here, then all users could view an unpublished node.
if ($node->status) {
$grants[] = array(
'realm' => 'example',
'gid' => 1,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 0,
);
}
// For the example_author array, the GID is equivalent to a UID, which
// means there are many groups of just 1 user.
// Note that an author can always view his or her nodes, even if they
// have status unpublished.
$grants[] = array(
'realm' => 'example_author',
'gid' => $node->uid,
'grant_view' => 1,
'grant_update' => 1,
'grant_delete' => 1,
'priority' => 0,
);
return $grants;
}
}hook_node_grants($account, $op)node_access_grants($op, $account = NULL)hook_query_TAG_alter();
node_access();
node_access_view_all_nodes();
_node_query_node_access_alter();/**
* Implements hook_node_grants().
*/
function hook_node_grants($account, $op) {
if (user_access('access private content', $account)) {
$grants['example'] = array(1);
}
$grants['example_author'] = array($account->uid);
return $grants;
}/**
* Implements hook_node_access_records_alter().
*/
function hook_node_access_records_alter(&$grants, $node) {
// Our module allows editors to mark specific articles with the 'is_preview'
// field. If the node being saved has a TRUE value for that field, then only
// our grants are retained, and other grants are removed. Doing so ensures
// that our rules are enforced no matter what priority other grants are given.
if ($node->is_preview) {
// Our module grants are set in $grants['example'].
$temp = $grants['example'];
// Now remove all module grants but our own.
$grants = array('example' => $temp);
}
}
/**
* Implements hook_node_grants_alter().
*/
function hook_node_grants_alter(&$grants, $account, $op) {
// Our sample module never allows certain roles to edit or delete
// content. Since some other node access modules might allow this
// permission, we expressly remove it by returning an empty $grants
// array for roles specified in our variable setting.
// Get our list of banned roles.
$restricted = variable_get('example_restricted_roles', array());
if ($op != 'view' && !empty($restricted)) {
// Now check the roles for this account against the restrictions.
foreach ($restricted as $role_id) {
if (isset($account->roles[$role_id])) {
$grants = array();
}
}
}
}