You are here

function _drupalgap_resource_user_permissions in DrupalGap 7

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

Returns the current user's permissions.

Return value

array Array of user roles and their corresponding permissions.

1 call to _drupalgap_resource_user_permissions()
drupalgap_system_connect_extras in ./drupalgap.module
1 string reference to '_drupalgap_resource_user_permissions'
drupalgap_services_resources in ./drupalgap.services.inc
Defines function signatures for resources available to services.

File

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

Code

function _drupalgap_resource_user_permissions() {
  global $user;
  $uid = $user->uid;
  if ($uid == 0) {

    // Grab permissions for 'anonymous user' role.
    $query = db_select('role_permission', 'rp');
    $query
      ->condition('rp.rid', '1')
      ->fields('rp', array(
      'permission',
      'module',
    ));
    $result = $query
      ->execute();
    return $result
      ->fetchAll();
  }
  else {

    // Grab roles for authenticated user.
    $query = db_select('users_roles', 'ur');
    $query
      ->condition('ur.uid', $uid)
      ->fields('ur', array(
      'rid',
    ));
    $result = $query
      ->execute();
    $results = $result
      ->fetchAll();
    if (empty($results)) {

      // The user only has the 'authenticated user' role.
      // Grab permissions for 'anonymous user' role.
      $query = db_select('role_permission', 'rp');
      $query
        ->condition('rp.rid', '2')
        ->fields('rp', array(
        'permission',
        'module',
      ));
      $result = $query
        ->execute();
      return $result
        ->fetchAll();
    }
    else {

      // The user has roles other than the 'authenticated user' role, let's
      // extract them then load their permissions.
      $rids = array(
        '2',
      );
      foreach ($results as $role) {
        $rids[] = $role->rid;
      }
      $query = db_select('role_permission', 'rp');
      $query
        ->leftJoin('users_roles', 'ur', 'rp.rid = ur.rid');
      $query
        ->fields('rp', array(
        'permission',
        'module',
      ))
        ->fields('ur', array(
        'rid',
      ))
        ->condition('rp.rid', $rids, 'IN');
      $result = $query
        ->execute();
      return $result
        ->fetchAll();
    }
  }
}