You are here

function og_get_user_roles in Organic groups 7.2

Same name and namespace in other branches
  1. 7 og.module \og_get_user_roles()

Get all roles of a user in a certain group.

Parameters

$group_type: The entity type of the group.

$gid: The group ID.

$uid: (optional) Integer specifying the user ID. By default an ID of current logged in user will be used.

$include: (optional) If TRUE also anonymous or authenticated role ID will be returned. Defaults to TRUE.

$check_active: (optional) If TRUE, and the user is pending, only anonymous role will be returned. If blocked, no role will be returned.

Return value

Array with the role IDs of the user as the key, and the role name as the value.

13 calls to og_get_user_roles()
OgDefaultAccessFieldTestCase::testOgDefaultAccessField in ./og.test
Test groups with default access field enabled or disabled.
OgGroupAndUngroup::testGroupManagerDefaultRoles in ./og.test
Test granting deault role to group manager.
OgMigrate7000TestCase::testUser in ./og.test
Test user upgrade.
OgPermissionsTestCase::testBlockedAndPendingRoles in ./og.test
Assert blocked and pending roles influence the allowed permissions.
OgRoleRevoke::testOgRoleRevoke in ./og.test

... See full list

1 string reference to 'og_get_user_roles'
og_invalidate_cache in ./og.module
Invalidate cache.

File

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

Code

function og_get_user_roles($group_type, $gid, $uid = NULL, $include = TRUE, $check_active = TRUE) {
  $roles =& drupal_static(__FUNCTION__, array());
  if (empty($uid)) {
    global $user;
    $uid = $user->uid;
  }
  $account = user_load($uid);
  $identifier = implode(':', array(
    $group_type,
    $gid,
    $uid,
    $include,
  ));
  if (isset($roles[$identifier])) {
    return $roles[$identifier];
  }
  $is_blocked = og_is_member($group_type, $gid, 'user', $account, array(
    OG_STATE_BLOCKED,
  ));
  if ($check_active && $is_blocked) {
    $roles[$identifier] = array();
    return $roles[$identifier];
  }
  $is_member = og_is_member($group_type, $gid, 'user', $account);
  $rids = array();
  $group = entity_load_single($group_type, $gid);

  // Get the bundle of the group.
  list(, , $bundle) = entity_extract_ids($group_type, $group);

  // Check if roles are overriden for the group.
  $query_gid = og_is_group_default_access($group_type, $gid) ? 0 : $gid;
  if (!$check_active || $is_member) {
    $query = db_select('og_users_roles', 'ogur');
    $query
      ->innerJoin('og_role', 'ogr', 'ogur.rid = ogr.rid');
    $rids = $query
      ->fields('ogur', array(
      'rid',
    ))
      ->fields('ogr', array(
      'name',
    ))
      ->condition('ogr.group_type', $group_type, '=')
      ->condition('ogr.group_bundle', $bundle, '=')
      ->condition('ogr.gid', $query_gid, '=')
      ->condition('ogur.uid', $uid, '=')
      ->condition('ogur.gid', $gid, '=')
      ->orderBy('rid')
      ->execute()
      ->fetchAllkeyed();
  }
  if ($include && !$is_blocked) {
    $role_name = $is_member ? OG_AUTHENTICATED_ROLE : OG_ANONYMOUS_ROLE;
    $rids = db_select('og_role', 'ogr')
      ->fields('ogr', array(
      'rid',
      'name',
    ))
      ->condition('group_type', $group_type, '=')
      ->condition('group_bundle', $bundle, '=')
      ->condition('gid', $query_gid, '=')
      ->condition('name', $role_name, '=')
      ->execute()
      ->fetchAllkeyed() + $rids;
  }
  $roles[$identifier] = $rids;
  return $rids;
}