You are here

function og_subgroups_node_access_records_alter in Subgroups for Organic groups 7

Same name and namespace in other branches
  1. 7.2 og_subgroups.module \og_subgroups_node_access_records_alter()

Implements hook_node_access_records_alter().

This alter is fired on node save, we want to add view permission to a user's subgroups (private groups), to do so we add the parent groups id to all groups.

File

./og_subgroups.module, line 110
Enable defining hierarchy of groups for organic groups.

Code

function og_subgroups_node_access_records_alter(&$grants, $node) {

  // Relevant only for private groups.
  if (module_exists('og_access')) {

    // The group IDs, that in case access is granted, will be recorded.
    $gids = array();
    $private = FALSE;
    $groups = array();

    // Dealing with a node group that is private.
    if (!empty($node->{OG_ACCESS_FIELD}[LANGUAGE_NONE][0]['value'])) {
      $group = og_get_group('node', $node->nid);
      if ($group) {
        $groups[] = $group->gid;
        $private = TRUE;
      }
    }
    elseif (isset($node->{OG_CONTENT_ACCESS_FIELD}[LANGUAGE_NONE][0]['value'])) {

      // If no groups with og realm are defined, this means it's a public group
      //  then do nothing, otherwise treat as a private group.
      if ($node->{OG_CONTENT_ACCESS_FIELD}[LANGUAGE_NONE][0]['value'] == OG_CONTENT_ACCESS_PRIVATE || $node->{OG_CONTENT_ACCESS_FIELD}[LANGUAGE_NONE][0]['value'] == OG_CONTENT_ACCESS_DEFAULT && og_subgroups_grants_has_og_realm($grants)) {
        $groups = og_get_entity_groups('node', $node);
        $private = TRUE;
      }
    }

    // If group is private, then grant permissions for parent groups.
    if ($private) {
      og_subgroups_get_reverse_hierarchy_tree_perm($groups, '', NULL, $gids);

      // Check existing grant and remove from gids[], to avoid duplication.
      foreach ($grants as $granted) {
        if (isset($gids[$granted['gid']])) {
          unset($gids[$granted['gid']]);
        }
      }

      // Build the new access Grant array.
      foreach ($gids as $gid) {
        $grants[] = array(
          'realm' => OG_ACCESS_AUTHENTICATED_REALM,
          'gid' => $gid['gid'],
          'grant_view' => 1,
          'grant_update' => 0,
          'grant_delete' => 0,
          'priority' => 0,
        );
      }
    }
  }
}