You are here

function og_user_access_entity in Organic groups 7

Same name and namespace in other branches
  1. 7.2 og.module \og_user_access_entity()

Check if a user has access to a permission on a certain entity context.

Parameters

$perm: The organic groups permission.

$entity_type: The entity type.

$entity: The entity object.

$account: (optional) The user object. If empty the current user will be used.

Return value

Returns TRUE if the user has access to the permission, otherwise FALSE, or if the entity is not in OG context, function will return NULL. This allows a distinction between FALSE - no access, and NULL - no access but no OG context found.

3 calls to og_user_access_entity()
OgGroupApi::testOgAccessEntity in ./og.test
Verify og_user_access_entity() returns correct value.
og_field_access_field_access in og_field_access/og_field_access.module
Implements hook_field_access().
og_node_access in ./og.module
Implement hook_node_access()

File

./og.module, line 1879
Enable users to create and manage groups with roles and permissions.

Code

function og_user_access_entity($perm, $entity_type, $entity, $account = NULL) {
  if (empty($account)) {
    global $user;
    $account = clone $user;
  }

  // Quick check for user ID 1.
  if ($account->uid == 1) {
    return TRUE;
  }

  // Set the default for the case there is not a group or a group content.
  $result = NULL;
  if (empty($entity)) {

    // $entity might be NULL, so return early.
    // @see field_access().
    return $result;
  }
  list($id, $vid, $bundle_name) = entity_extract_ids($entity_type, $entity);
  if (empty($id)) {

    // Entity isn't saved yet.
    return $result;
  }
  $is_group = og_is_group_type($entity_type, $bundle_name);
  $is_group_content = og_is_group_content_type($entity_type, $bundle_name);

  // Check if entity is an active group.
  $group = og_get_group($entity_type, $id);
  if ($is_group && $group) {
    if (og_user_access($group->gid, $perm, $account)) {
      return TRUE;
    }
    else {

      // An entity can be a group and group content in the same time. The group
      // didn't return TRUE, but the user still might have access to the
      // permission in group content context.
      $result = FALSE;
    }
  }
  if ($is_group_content && ($gids = og_get_entity_groups($entity_type, $entity))) {
    foreach ($gids as $gid) {
      if (og_user_access($gid, $perm, $account)) {
        return TRUE;
      }
    }
    return FALSE;
  }

  // Either the user didn't have permission, or the entity might be a
  // disabled group or an orphaned group content.
  return $result;
}