You are here

function access_user_roles in Access Control Kit 7

Returns a list of a user's realm-level role memberships.

Parameters

object $account: (optional) The account to check. Defaults to the currently logged in user.

Return value

array A nested array indexed first by role ID, then by scheme machine name, where each value is an array containing the user's assigned realms for that role in that scheme as realm_value => realm_label. Note that the list of roles is limited to those found in $scheme->roles for each scheme; non-ACK roles are excluded.

1 call to access_user_roles()
access_user_permission_realms in ./access.module
Returns a list of realms in which the user has a given permission.

File

./access.module, line 834
The access control kit module.

Code

function access_user_roles($account = NULL) {
  global $user;
  if (!isset($account)) {
    $account = $user;
  }

  // Cache the user's role list using the advanced drupal_static() pattern for
  // best performance on repeated access checks.
  static $drupal_static_fast;
  if (!isset($drupal_static_fast)) {
    $drupal_static_fast['user_roles'] =& drupal_static(__FUNCTION__);
  }
  $user_roles =& $drupal_static_fast['user_roles'];

  // On cache miss, load all grants for the user and sort by role and scheme.
  if (!isset($user_roles[$account->uid])) {
    $grants = access_grant_load_by_condition(array(
      'uid' => $account->uid,
    ));
    $roles = array();
    foreach ($grants as $grant) {
      $scheme = access_scheme_machine_name_load($grant->scheme);

      // Only include roles that are in the current list of ACK-enabled roles
      // (in case the scheme settings have changed), and that are currently
      // associated with the user (in case another module revoked a role without
      // ACK being notified to clean up grants).
      if (isset($scheme->roles[$grant->rid]) && isset($account->roles[$grant->rid])) {
        $roles[$grant->rid][$grant->scheme] = $grant->realms;
      }
    }
    $user_roles[$account->uid] = $roles;
  }
  return $user_roles[$account->uid];
}