You are here

og_access.module in Organic groups 6

Same filename and directory in other branches
  1. 6.2 modules/og_access/og_access.module

File

modules/og_access/og_access.module
View source
<?php

// visibility states for private groups. site admin chooses in og_access_settings()
define('OG_PRIVATE_GROUPS_NEVER', 0);
define('OG_PRIVATE_GROUPS_ALWAYS', 1);
define('OG_PRIVATE_GROUPS_CHOOSE_TRUE', 2);
define('OG_PRIVATE_GROUPS_CHOOSE_FALSE', 3);

// visibility states for nodes within groups. site admin chooses in og_settings()
define('OG_VISIBLE_GROUPONLY', 0);
define('OG_VISIBLE_BOTH', 1);
define('OG_VISIBLE_CHOOSE_PUBLIC', 2);
define('OG_VISIBLE_CHOOSE_PRIVATE', 3);
function og_access_menu() {
  $items['admin/og/og_access'] = array(
    'title' => 'Organic groups access configuration',
    'description' => 'Choose whether new groups should be private or public.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'og_access_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  return $items;
}
function og_access_settings() {
  drupal_add_js(drupal_get_path('module', 'og_access') . '/og_access.js');

  // load the form javascript to handle private groups / node visibility conflicts in the og access settings form.
  $options = array(
    t('Visible only within the targeted groups.'),
    t('Visible within the targeted groups and on other pages.'),
    t('Visibility chosen by author/editor using a checkbox on the posting form. Checkbox defaults to <em>public</em>.'),
    t('Visibility chosen by author/editor using a checkbox on the posting form. Checkbox defaults to <em>private</em>.'),
  );
  $form['og_visibility'] = array(
    '#type' => 'radios',
    '#title' => t('Visibility of posts'),
    '#default_value' => variable_get('og_visibility', 0),
    '#description' => t('Determine how broadly available a given post should be when it is affiliated with a group. OG admins always see the checkbox for making a post public. Note that changing this setting has no effect on existing posts. Re-save those posts to acquire this new setting. If the setting you want is disabled here, check the settings under <em>Private Groups</em> setting below. You cannot set node visibility to always be public if private groups are set to always on and vice versa.'),
    '#options' => $options,
  );

  // private groups control
  $options = array(
    t('New group home pages and default audience are always public.'),
    t('New group home pages and default audience are always private.'),
    t('Group administrator chooses whether her group homepage and audience are private or not. Defaults to <em>private</em>.'),
    t('Group administrator chooses whether her group homepage and audience are private or not. Defaults to <em>public</em>.'),
  );
  $form['og_private_groups'] = array(
    '#type' => 'radios',
    '#title' => t('Private groups'),
    '#options' => $options,
    '#default_value' => variable_get('og_private_groups', OG_PRIVATE_GROUPS_CHOOSE_FALSE),
    '#description' => '<p>' . t("A private group's group home page cannot be seen by non-members, and new posts created in the group will default to being private. This setting controls what private groups options can be used when creating a new group or editing an existing group. If you select one of the <em>group administrator chooses</em> options then it will be up to group admins whether their new groups are private or not, with the default you specify here.") . '</p><p>' . t('Note that the privacy of all <em>content</em> in the group is determined as each node is created or edited, according to the <em>Visibility of Posts</em> setting on this page. Note also that changing this setting only affects the default for new groups being created, not the privacy of any existing groups! To change those you must edit the groups and their individual content nodes directly. If the setting you want is disabled here, check <em>Visibility of Posts</em> above. You cannot choose to only have private groups if node visibility is set to be always public, and vice versa.') . '</p>',
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_content_extra_fields.
 */
function og_access_content_extra_fields($type_name) {
  $extra = array();
  if (og_is_group_type($type_name)) {
    $extra['og_private'] = array(
      'label' => t('Private group'),
      'description' => t('Checkbox for visibility of group home page to non-members.'),
      'weight' => 0,
    );
  }
  return $extra;
}
function og_access_alter_group_form(&$form, $node) {

  // private groups
  $visibility = variable_get('og_private_groups', OG_PRIVATE_GROUPS_CHOOSE_FALSE);

  // override setting for admins - get right default
  if (user_access('administer nodes')) {
    $not = array(
      OG_PRIVATE_GROUPS_NEVER,
      OG_PRIVATE_GROUPS_CHOOSE_FALSE,
    );
    $visibility = in_array($visibility, $not) ? OG_PRIVATE_GROUPS_CHOOSE_FALSE : OG_PRIVATE_GROUPS_CHOOSE_TRUE;
  }
  $default = FALSE;
  switch ($visibility) {
    case OG_PRIVATE_GROUPS_NEVER:
      $form['og_private'] = array(
        '#type' => 'value',
        '#value' => 0,
      );
      break;
    case OG_PRIVATE_GROUPS_ALWAYS:
      $form['og_private'] = array(
        '#type' => 'value',
        '#value' => 1,
      );
      break;
    case OG_PRIVATE_GROUPS_CHOOSE_TRUE:
      $default = TRUE;

    // fall through
    case OG_PRIVATE_GROUPS_CHOOSE_FALSE:
      $form['og_private'] = array(
        '#type' => 'checkbox',
        '#title' => t('Private group'),
        '#default_value' => isset($node->nid) ? $node->og_private : $default,
        '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'og_private') : 0,
        '#description' => t('Should this group be visible only to its members? Disabled if the group is set to <em>List in Directory</em> or <em>Membership requests: open</em>.'),
      );
      break;
  }
}

// Add public checkbox to node edit form.
function og_access_alter_nongroup_form(&$form, $form_state, $node) {
  global $user;

  // If user has no subscriptions, don't bother with Public checkbox - it's meaningless.
  if (og_is_group_post_type($node->type) && !empty($form['og_nodeapi']['visible'])) {

    // get the visibility for normal users
    $vis = variable_get('og_visibility', 0);

    // override visibility for og admins
    if (user_access('administer organic groups')) {
      if ($vis < 2) {
        $vis = $vis == OG_VISIBLE_GROUPONLY ? OG_VISIBLE_CHOOSE_PRIVATE : OG_VISIBLE_CHOOSE_PUBLIC;
      }
    }
    elseif (!og_get_subscriptions($user->uid)) {

      // don't show checkbox if no memberships. must be public.
      $vis = OG_VISIBLE_BOTH;
    }

    // We are using this form element to communicate $groups from og to og_access.
    $groups = $form['og_initial_groups']['#value'];

    // If the post is to a private group, visibility must default to one of the private options.
    $selected_groups = isset($form_state['values']['og_groups']) ? array_filter($form_state['values']['og_groups']) : $groups;
    if (!empty($selected_groups)) {
      foreach ($selected_groups as $gid) {
        $group_node = new stdClass();
        $group_node->nid = $gid;
        og_load_group($group_node);
        if (!empty($group_node->og_private)) {

          // Try not to show checkbox if admin likes to reduce decisions for node authors.
          $vis = variable_get('og_visibility', 0) == OG_VISIBLE_BOTH ? OG_VISIBLE_GROUPONLY : OG_VISIBLE_CHOOSE_PRIVATE;
          break;
        }
      }
    }
    else {

      // TODOL: No groups. Public must be checked if it is visible.
    }
    switch ($vis) {
      case OG_VISIBLE_BOTH:
        $form['og_nodeapi']['og_public'] = array(
          '#type' => 'value',
          '#value' => 1,
        );
        break;
      case OG_VISIBLE_GROUPONLY:
        $form['og_nodeapi']['og_public'] = array(
          '#type' => 'value',
          '#value' => 0,
        );
        break;

      //user decides how public the post is.
      case OG_VISIBLE_CHOOSE_PUBLIC:
        $form['og_nodeapi']['visible']['og_public'] = array(
          '#type' => 'checkbox',
          '#title' => t('Public'),
          '#default_value' => isset($node->og_public) ? $node->og_public : 1,
          '#description' => t('Show this post to everyone, or only to members of the groups checked above. Posts without any groups are always <em>public</em>.'),
          '#weight' => 2,
        );
        break;
      case OG_VISIBLE_CHOOSE_PRIVATE:
        $form['og_nodeapi']['visible']['og_public'] = array(
          '#type' => 'checkbox',
          '#title' => t('Public'),
          '#default_value' => isset($node->og_public) ? $node->og_public : 0,
          '#description' => t('Show this post to everyone, or only to members of the groups checked above. Posts without any groups are always <em>public</em>.'),
          '#weight' => 2,
        );
        break;
    }
    if (count($form['og_nodeapi']['visible']) > 1) {
      $form['og_nodeapi']['#type'] = 'fieldset';
      $form['og_nodeapi']['#title'] = t('Groups');
      $form['og_nodeapi']['#collapsible'] = TRUE;
      $form['og_nodeapi']['#collapsed'] = $selected_groups ? TRUE : FALSE;
    }
  }
}

/**
 * Implementation of hook_form_alter().
 */
function og_access_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['#node']) && $form_id == $form['#node']->type . '_node_form') {
    drupal_add_js(drupal_get_path('module', 'og_access') . '/og_access.js');
    $node = $form['#node'];
    if (og_is_group_type($node->type)) {
      og_access_alter_group_form($form, $node);
    }
    else {
      og_access_alter_nongroup_form($form, $form_state, $node);
    }
  }
}

/**
 * Implementation of hook_node_grants().
 */
function og_access_node_grants($account, $op) {
  if ($op == 'view') {
    $grants['og_public'][] = 0;

    // everyone can see a public node
  }

  // Subscribers get an admin or non-admin grant for each subscription
  if ($subscriptions = og_get_subscriptions($account->uid)) {
    foreach ($subscriptions as $key => $val) {

      // Admins don't need to receive the subscriber grant since they can perform all operations.
      if ($val['is_admin']) {
        $grants['og_admin'][] = $key;
      }
      else {
        $grants['og_subscriber'][] = $key;
      }
    }
  }
  return isset($grants) ? $grants : array();
}

/**
 * Implementation of hook_node_access_records.
 */
function og_access_node_access_records($node) {
  if (og_is_group_type($node->type)) {

    // This grant allows group admins to manage their group.
    $grants[] = array(
      'realm' => 'og_admin',
      'gid' => $node->nid,
      'grant_view' => 1,
      'grant_update' => 1,
      'grant_delete' => 0,
      'priority' => 0,
    );
    if (!$node->og_private) {

      // If the group is not private, let everyone view the group homepage.
      $grants[] = array(
        'realm' => 'og_public',
        'gid' => 0,
        'grant_view' => 1,
        'grant_update' => 0,
        'grant_delete' => 0,
        'priority' => 0,
      );
    }
    else {

      // If the group private, let subscribers view the group homepage.
      $grants[] = array(
        'realm' => 'og_subscriber',
        'gid' => $node->nid,
        'grant_view' => 1,
        'grant_update' => 0,
        'grant_delete' => 0,
        'priority' => 0,
      );
    }
  }
  elseif (!empty($node->og_groups)) {

    // Applies to non group nodes.
    if ($node->og_public) {
      $grants[] = array(
        'realm' => 'og_public',
        'gid' => 0,
        'grant_view' => 1,
        'grant_update' => 0,
        'grant_delete' => 0,
        'priority' => 0,
      );
    }
    foreach ($node->og_groups as $gid) {

      // Group administrators get all operations.
      $grants[] = array(
        'realm' => 'og_admin',
        'gid' => $gid,
        'grant_view' => 1,
        'grant_update' => 1,
        'grant_delete' => 1,
        'priority' => 0,
      );

      // Normal subscribers just get update operation if node type is a wiki type.
      $is_wiki = og_is_wiki_type($node->type);
      $grants[] = array(
        'realm' => 'og_subscriber',
        'gid' => $gid,
        'grant_view' => 1,
        'grant_update' => $is_wiki,
        'grant_delete' => 0,
        'priority' => 0,
      );
    }
  }
  if (!empty($grants)) {

    // Allow other modules to change the grants.
    drupal_alter('og_access_grants', $grants, $node);
    return $grants;
  }
  return NULL;
}

/**
 * Implementation of hook_node_access_explain.
 */
function og_access_node_access_explain($row) {
  if ($row->realm == 'og_public') {
    return t('All users may view this node.');
  }
  elseif ($row->realm == 'og_subscriber') {
    $node = node_load((int) $row->gid);
    return t('Members of <a href="@group-node">@group-name</a> may view this node.', array(
      '@group-node' => url('node/' . $row->gid),
      '@group-name' => $node->title,
    ));
  }
  elseif ($row->realm == 'og_admin') {
    $node = node_load((int) $row->gid);
    return t('Group admins of <a href="@group-node">@group-name</a> may view/edit/delete this node.', array(
      '@group-node' => url('node/' . $row->gid),
      '@group-name' => $node->title,
    ));
  }
}