You are here

function _drupalgap_resource_user_roles_and_permissions in DrupalGap 6

Same name and namespace in other branches
  1. 7.2 drupalgap.resource.inc \_drupalgap_resource_user_roles_and_permissions()
  2. 7 drupalgap.resource.inc \_drupalgap_resource_user_roles_and_permissions()

Returns a user's roles and permissions.

Return value

array Array of user roles and their corresponding permissions.

1 call to _drupalgap_resource_user_roles_and_permissions()
_drupalgap_resource_user_permissions in ./drupalgap.resource.inc
Returns the current user's permissions.
1 string reference to '_drupalgap_resource_user_roles_and_permissions'
drupalgap_services_resources in ./drupalgap.services.inc
Defines function signatures for resources available to services.

File

./drupalgap.resource.inc, line 206
This file implements the DrupalGap service resource call back functions.

Code

function _drupalgap_resource_user_roles_and_permissions() {
  global $user;
  $uid = $user->uid;
  if (!isset($uid) || !is_numeric($uid)) {
    services_error(t('Missing argument uid.'), 406);
  }

  // Prepare empty resuls array.
  $results = array();

  // Load user account.
  $account = user_load($uid);

  // Depending if user is authenticated or not, retrieve permissions based on
  // the user's role(s).
  if ($uid == 0) {

    // Grab permissions for 'anonymous user' role.
    $sql = " SELECT rid, perm FROM {permission} ";
    $sql .= " WHERE rid = 1 ";
    $sql .= " GROUP BY rid ";
    $perms = db_query($sql);
    while ($data = db_fetch_object($perms)) {
      drupalgap_convert_d6_perms_to_d7(&$results, $data, $account);
    }
  }
  else {

    // Grab role(s) for authenticated user.
    $roles = array();
    $sql = " SELECT ur.rid FROM {users_roles} ur WHERE ur.uid = %d ";
    $result = db_query($sql, $uid);
    while ($role = db_fetch_object($result)) {
      $roles[] = $role;
    }

    // We must manually append the 'authenticated user' role id (2) to the roles
    // array. The user roles table does not hold this data.
    $roles[] = 2;

    // Retrieve permissions for the user's role(s).
    $sql = " SELECT rid, perm FROM {permission} ";
    $sql .= " WHERE rid IN (" . db_placeholders($roles, 'int') . ") ";
    $sql .= " GROUP BY rid ";
    $perms = db_query($sql, $roles);
    if ($perms) {
      while ($data = db_fetch_object($perms)) {
        drupalgap_convert_d6_perms_to_d7(&$results, $data, $account);
      }
    }
  }
  return $results;
}