You are here

function hosting_client_node_access_records in Hosting 7.3

Same name and namespace in other branches
  1. 5 client/hosting_client.access.inc \hosting_client_node_access_records()
  2. 6.2 client/hosting_client.access.inc \hosting_client_node_access_records()
  3. 7.4 client/hosting_client.access.inc \hosting_client_node_access_records()

Implements hook_node_access_records().

File

client/hosting_client.access.inc, line 250
Control client node access.

Code

function hosting_client_node_access_records($node) {
  if (hosting_client_disabling()) {
    return;
  }
  $grants = array();
  $base_grant = array(
    'realm' => 'hosting ' . $node->type,
    'grant_view' => 1,
    'grant_update' => 1,
    'grant_delete' => 0,
    'priority' => 1,
  );

  // Tasks inherit from their parent.
  if ($node->type == 'task') {
    $node = node_load($node->rid);
    $base_grant['grant_update'] = 0;
  }
  switch ($node->type) {
    case 'site':
      $base_grant['gid'] = $node->client;
      break;
    case 'client':
      $base_grant['gid'] = $node->nid;
      break;
    case 'package':
      $base_grant['grant_update'] = 0;
      break;
    case 'task':

      // The rest of the node types are configuration, so only admin should see them.
      $base_grant['gid'] = HOSTING_ADMIN_CLIENT;
      break;
    case 'server':

      // The rest of the node types are configuration, so only admin should see them.
      $base_grant['gid'] = HOSTING_ADMIN_CLIENT;

      // Lookup platforms on this server.
      $platforms = _hosting_get_platforms($node->nid);
      $gids = array();
      foreach ($platforms as $nid => $title) {
        $node = node_load($nid);
        if (!empty($node->clients)) {
          foreach ($node->clients as $cid => $client) {
            if ($cid != HOSTING_ADMIN_CLIENT) {
              $gids[] = $cid;
            }
          }
        }
        else {

          // When $node->clients isn't set on a platform, all clients have access.
          $results = db_query("select nid from {hosting_client} c");
          foreach ($results as $row) {
            if ($row->nid != HOSTING_ADMIN_CLIENT) {
              $gids[] = $row->nid;
            }
          }
        }
      }

      // Lookup databases hosted on this server.
      $databases = hosting_get_sites_on_db_server($node->nid);
      foreach ($databases as $nid => $dbname) {
        $node = node_load($nid);
        if (!empty($node->client)) {
          if ($node->client != HOSTING_ADMIN_CLIENT) {
            $gids[] = $node->client;
          }
        }
      }
      $gids = array_unique($gids);

      // Grant access to all clients who have access to a platform.
      foreach ($gids as $cid) {
        $grants[] = array_merge($base_grant, array(
          'gid' => $cid,
          'grant_update' => 0,
        ));
      }
      break;
    case 'platform':
      $base_grant['gid'] = HOSTING_ADMIN_CLIENT;
      if (isset($node->clients)) {
        foreach ($node->clients as $cid => $client) {
          if ($cid != HOSTING_ADMIN_CLIENT) {
            $grants[] = array_merge($base_grant, array(
              'gid' => $cid,
              'grant_update' => 0,
            ));
          }
        }
      }
      else {

        // When $node->clients isn't set, all clients have access.
        $results = db_query("select nid from {hosting_client} c");
        foreach ($results as $row) {
          if ($row->nid != HOSTING_ADMIN_CLIENT) {
            $grants[] = array_merge($base_grant, array(
              'gid' => $row->nid,
              'grant_update' => 0,
            ));
          }
        }
      }
      break;
    default:

      // Not hosting node, don't change access.
      return;
  }
  if (isset($base_grant['gid'])) {
    $grants[] = $base_grant;

    // This should _always_ happen.
    if ($base_grant['gid'] != HOSTING_ADMIN_CLIENT) {

      // Also give full access to the administrator user.
      $base_grant['gid'] = HOSTING_ADMIN_CLIENT;
      $grants[] = $base_grant;
    }
    return $grants;
  }
}