Using drupal 7 I have implemented hook_node_access
but am having real trouble figuring out how to go a step further and achieve the following using hook_node_access_records
and hook_node_grants
.
For a given content type ('submission') I want to limit users with a particular role ('curator') and an additionally set field in their user profile ('location') to see only nodes that have a specific matching field value set ('location').
So in this case curators from London can only see submissions to the gallery in London rather than see all 'submission' nodes.
Update
I have made some progress here in my understanding. See the code below. My question now is can i do the following to check set a realm and grant id that i can use to lock specific users out?
Below I am trying to set a lock such that any user who has the grant id cannot access the node (this will be a subset of people within a given role) and for everyone else I just want the sites normal configured access permissions to apply.
function hook_node_access_records($node) {
$grants = array();
if ($node->type == 'submission') {
$grants[] = array(
// Say this node is in our realm.
// By convention we prefix the name of the realm
// with the module name.
'realm' => 'submission-permissions',
// Define the grant id - users in the realm must
// have this grant id to access this node.
'gid' => $node->field_location[LANGUAGE_NONE][0]['target_id'],
// Users who match cannot view the node.
'grant_view' => 0,
// Users who match cannot update the node.
'grant_update' => 0,
// Users who match cannot delete the node.
'grant_delete' => 0,
// In the case another module provides exactly
// this access record, the one with the highest
// priority wins. Usually leave this as 0.
'priority' => 100,
);
}
}
/*
* Implements hook_node_grants().
*/
function hook_node_grants($account, $op) {
$grants = array();
$role = is_string($account) ? $account : $account->roles[4];
if (($role == 'curator') && ($op == 'view')){
// Get the currently looged in users accounts details
$user_fields = user_load($account->uid);
$location = is_string($user_fields) ? $user_fields : $user_fields->field_curator_location[LANGUAGE_NONE][0]['target_id'];
if (!empty($location)){
$grants['submission-permissions'][] = $user_fields->field_curator_location[LANGUAGE_NONE][0]['target_id'];
}
}
return $grants;
}