You are here

function og_access_node_access_records in Organic groups 7

Same name and namespace in other branches
  1. 5.8 og_access.module \og_access_node_access_records()
  2. 5 og_access.module \og_access_node_access_records()
  3. 5.3 og_access.module \og_access_node_access_records()
  4. 5.7 og_access.module \og_access_node_access_records()
  5. 6.2 modules/og_access/og_access.module \og_access_node_access_records()
  6. 6 modules/og_access/og_access.module \og_access_node_access_records()
  7. 7.2 og_access/og_access.module \og_access_node_access_records()

Implements hook_node_access_records().

File

og_access/og_access.module, line 60
Enable access control for private and public groups and group content.

Code

function og_access_node_access_records($node) {
  if (empty($node->status)) {

    // Node is unpublished, so we don't allow every group member to see
    // it.
    return array();
  }
  $grants = array();

  // The group IDs, that in case access is granted, will be recorded.
  $gids = array();
  $access = FALSE;
  if (!empty($node->{OG_ACCESS_FIELD}[LANGUAGE_NONE][0]['value']) && ($group = og_get_group('node', $node->nid))) {

    // Group or group content that is explicitly set to be unpublic.
    $access = TRUE;
    $gids[] = $group->gid;
  }
  elseif (isset($node->{OG_CONTENT_ACCESS_FIELD}[LANGUAGE_NONE][0]['value'])) {
    switch ($node->{OG_CONTENT_ACCESS_FIELD}[LANGUAGE_NONE][0]['value']) {
      case OG_CONTENT_ACCESS_DEFAULT:
        if ($field = field_info_field(OG_ACCESS_FIELD)) {

          // Access should be determined by its groups. If group content belongs
          // to several groups, and one of them is private, then the group
          // content will private as-well.
          $gids = og_get_entity_groups('node', $node);
          $groups = og_load_multiple($gids);

          // Get all groups under their entity.
          $list = array();
          foreach ($groups as $group) {
            $list[$group->entity_type][$group->etid] = $group->etid;
          }

          // If group content belongs to several groups, and one of them is
          // private, then this variable decides what should happen -- if the
          // group content will be private as-well or become public.
          // By default, if one group is private then the group content will be
          // private.
          $strict_private = variable_get('group_access_strict_private', 1);
          $total_count = 0;
          foreach ($list as $entity_type => $entity_gids) {
            $query = new EntityFieldQuery();
            $count = $query
              ->entityCondition('entity_type', $entity_type)
              ->entityCondition('entity_id', $entity_gids, 'IN')
              ->fieldCondition(OG_ACCESS_FIELD, 'value', 1, '=')
              ->count()
              ->execute();
            if ($strict_private) {

              // There is at least one private group and 'strict private' is
              // TRUE so this group content should be private.
              if ($count) {
                $access = TRUE;
                break;
              }
            }
            else {

              // 'strict private' is FALSE so count all the groups, and only if
              // all of them are private then this group content should be
              // private.
              $total_count += $count;
            }
          }
          if ($total_count == count($gids)) {

            // All groups are private.
            $access = TRUE;
          }
        }
        break;
      case OG_CONTENT_ACCESS_PUBLIC:

        // Do nothing.
        break;
      case OG_CONTENT_ACCESS_PRIVATE:
        $access = TRUE;
        $gids = og_get_entity_groups('node', $node);
        break;
    }
  }

  //TODO: Add a case where the group has access field but group content doesn't.
  if ($access && $gids) {
    foreach ($gids as $gid) {
      $grants[] = array(
        'realm' => OG_ACCESS_AUTHENTICATED_REALM,
        'gid' => $gid,
        'grant_view' => 1,
        'grant_update' => 0,
        'grant_delete' => 0,
        'priority' => 0,
      );
    }
  }
  return $grants;
}