System architecture:
Main entities:
- Company
- Employee (and other user types)
- Document
References:
- Every document has reference to company and employee.
- Every employee has reference to company also (at least one company)
I've implemented custom access control for documents, which suppose to working fine (my unit tests gives me all green also).
Now I'm creating View - documents list for an employee. Basic version of View is working correct. I want to add exposed filter for a company. By default this is a list of all companies. But I want to restrict this list only for companies from employee documents, related to current logged in employee.
Code:
/**
* Implements hook_node_access_records().
*/
function mymod_company_node_access_records($node) {
if ($node->type <> myCompany::CONTENT_TYPE_NAME) {
return array();
}
return array(
array(
'realm' => MYMOD_COMPANY_LOCK_NAME,
'gid' => $node->nid,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 0,
)
);
}
/**
* Implements hook_node_grants().
*/
function mymoo_company_node_grants($account, $op) {
if ($op <> 'view') {
return array();
}
if (mymod_user_is_an_employee($account)) {
$myUser = myUser::createFromUser($account);
$companies = $sdUser->getCompanies();
if (empty($companies)) {
return array();
}
$grants = array();
foreach(array_keys($companies) as $nid) {
$grants[MYMOD_COMPANY_LOCK_NAME][] = $nid;
}
return $grants;
} else {
return array();
}
}
Indeed filter list is restricted to employee related companies, but in my View, I'm seeing ALL documents related to these companies. Why?
My View main content type is Document. Company is related node via entity reference field. I don't understand why company access control has some type of precedence here.
hook_node_access_records
grants access to that node?hook_node_access_records
creates aview all
record in thenode_access
table.