You are here

function commons_groups_og_user_access_alter in Drupal Commons 7.3

Implements hook_og_user_access_alter().

Deny create permissions from non-members on "non-public" groups (i.e. groups that don't allow joining without approval).

File

modules/commons/commons_groups/commons_groups.module, line 490

Code

function commons_groups_og_user_access_alter(&$perm, $context) {
  $account = $context['account'];
  $group_type = $context['group_type'];
  $group = $context['group'];
  if ($group_type != 'node') {
    return;
  }

  // The purpose of this function is to grant permissions to create content
  // in a group to non-members of the group, when the group's privacy settings
  // (field_og_subscribe_settings) is set to "Anyone can contribute".
  if (og_is_member($group_type, $group->nid, 'user', $account, array(
    OG_STATE_ACTIVE,
    OG_STATE_PENDING,
    OG_STATE_BLOCKED,
  ))) {

    // The user is a group member, so comply to the OG permissions.
    return;
  }
  $wrapper = entity_metadata_wrapper($group_type, $group);
  $access_create = $account->uid && $wrapper->field_og_subscribe_settings
    ->value() == 'anyone';

  // Make sure user can view group (i.e. it's not private).
  $commons_groups_entity_types = commons_groups_get_group_content_entity_types();
  foreach (array_keys($commons_groups_entity_types['node']) as $type) {
    $perm["create {$type} content"] = $access_create;
  }
}