You are here

function domain_node_access_records in Domain Access 6.2

Same name and namespace in other branches
  1. 5 domain.module \domain_node_access_records()
  2. 7.3 domain.module \domain_node_access_records()
  3. 7.2 domain.module \domain_node_access_records()

Implement hook_node_access_records()

Set permissions for a node to be written to the database. By design if no options are selected, the node is assigned to the main site.

1 call to domain_node_access_records()
domain_content_update_nodes in domain_content/domain_content.module
Abstraction function that lets us update access rules.

File

./domain.module, line 1655
Core module functions for the Domain Access suite.

Code

function domain_node_access_records($node) {
  global $_domain;

  // Define the $grants array.
  $grants = array();

  // Check to see if the node domains were set properly.
  // If not, we are dealing with an automated node process, which
  // means we have to add the logic from hook_form_alter() here.
  if (!isset($node->domain_site)) {

    // We came from a separate source, so let's set the proper defaults.
    $node->domain_site = variable_get('domain_node_' . $node->type, variable_get('domain_behavior', DOMAIN_INSTALL_RULE));

    // And the currently active domain.
    $node->domains = array(
      $_domain['domain_id'] => $_domain['domain_id'],
    );
  }

  // If the form is hidden, we are passed the 'domains_raw' variable.
  // We need to append unique values from this variable to the existing
  // stored values.  See the logic for 'view domain publshing' in domain_form_alter().
  if (!empty($node->domains_raw)) {
    if (!isset($node->domains)) {
      $node->domains = array();
    }
    foreach ($node->domains_raw as $value) {

      // Only add this if it is not present already.
      if (!in_array($value, $node->domains)) {
        $node->domains[$value] = $value;
      }
    }
  }

  // If set, grant access to the core site, otherwise
  // The grant must be explicitly given to a domain.
  if (!empty($node->domain_site)) {
    $grants[] = array(
      'realm' => 'domain_site',
      'gid' => 0,
      'grant_view' => TRUE,
      'grant_update' => FALSE,
      'grant_delete' => FALSE,
      'priority' => 0,
    );
  }

  // Set the domain-specific grants.
  if (!empty($node->domains)) {
    foreach ($node->domains as $key => $value) {

      // We can't use a 0 value in an $options list, so convert -1 to 0.
      if (abs($value) > 0) {
        $key == -1 ? $key = 0 : ($key = $key);
        $grants[] = array(
          'realm' => 'domain_id',
          'gid' => $key,
          'grant_view' => TRUE,
          'grant_update' => TRUE,
          'grant_delete' => TRUE,
          'priority' => 0,
        );
      }
    }
  }
  else {
    $grants[] = array(
      'realm' => 'domain_id',
      'gid' => 0,
      'grant_view' => TRUE,
      'grant_update' => TRUE,
      'grant_delete' => TRUE,
      'priority' => 0,
    );
  }

  // Let Domain Access module extensions act to override the defaults.
  static $_modules;
  if (!isset($_modules)) {
    $_modules = module_implements('domainrecords');
  }
  if (!empty($_modules)) {
    foreach ($_modules as $module) {

      // Cannot use module_invoke_all() since these are passed by reference.
      $function = $module . '_domainrecords';
      $function($grants, $node);
    }
  }

  // Store our records in the {domain_access} table.
  _domain_store_grants($node->nid, $grants);
  return $grants;
}