You are here

function acb_node_access_records_alter in Access Control Bridge 7

Same name and namespace in other branches
  1. 8 acb.module \acb_node_access_records_alter()

Implements hook_node_access_records_alter().

Creates and inserts our cascaded access records for nodes.

File

./acb.module, line 50
Drupal hooks and functions for the acb module.

Code

function acb_node_access_records_alter(&$grants, $node) {

  // Split all grants into their respective controlling modules.
  $split_grants = array();
  foreach (acb_get_modules() as $module) {
    $grant_storage = array();
    foreach ($grants as $key => $grant) {
      switch ($grant['realm']) {

        // 'all' and ACL require special care.
        case 'acl':
        case 'all':

          // Check 'module' or '#module' key. If not present, then assume it to be Content Access.
          if (isset($grant['module'])) {
            $realm = $grant['module'];
          }
          elseif (isset($grant['#module'])) {
            $realm = $grant['#module'];
          }
          else {
            $realm = 'content_access';
          }
          break;
        default:
          $realm = $grant['realm'];
          break;
      }
      if (acb_is_module_realm($realm, $module) === TRUE) {
        $grant_storage[$key] = $grant;
      }
    }
    $split_grants[$module] = $grant_storage;
  }

  // Filter out any modules that didn't set access records.
  $split_grants = array_filter($split_grants);

  // Only proceed and alter the grants if multiple modules control this node.
  // If this is not the case, the legacy user grants will control the node as usual.
  if (count($split_grants) <= 1) {
    return;
  }

  // Convert 'all' realm grants to anonymous and authenticated role grants.
  $roles = array(
    DRUPAL_ANONYMOUS_RID,
    DRUPAL_AUTHENTICATED_RID,
  );
  foreach ($split_grants as $module => $split_grant_module) {
    foreach ($split_grant_module as $key => $split_grant) {
      if ($split_grant['realm'] == 'all') {
        foreach ($roles as $rid) {
          $split_grants[$module][$rid] = $split_grant;
          $split_grants[$module][$rid]['realm'] = $module . '_rid';
          $split_grants[$module][$rid]['gid'] = $rid;
        }
        unset($split_grants[$module][$key]);
      }
    }
  }

  // We keep the existing grants and merge ours because we overrule them
  // by setting a high priority on our own grants in _acb_cascade_grants().
  $grants = array_merge($grants, _acb_cascade_grants($split_grants, TRUE));
}