You are here

public function Storage::getUserClaims in OAuth2 Server 7

File

lib/Drupal/oauth2_server/Storage.php, line 330

Class

Storage
Provides Drupal storage (through the underlying Entity API) for the library.

Namespace

Drupal\oauth2_server

Code

public function getUserClaims($uid, $scope) {
  $account = user_load($uid);
  if (!$account) {
    throw new \InvalidArgumentException("The supplied user couldn't be loaded.");
  }
  $requested_scopes = explode(' ', trim($scope));

  // The OpenID Connect 'sub' (Subject Identifier) property is usually the
  // user's UID, but this is configurable for backwards compatibility reasons.
  // See: https://www.drupal.org/node/2274357#comment-9779467
  $sub_property = variable_get('oauth2_server_user_sub_property', 'uid');

  // Prepare the default claims.
  $claims = array(
    'sub' => $account->{$sub_property},
  );
  if (in_array('email', $requested_scopes)) {
    $claims['email'] = $account->mail;
    $claims['email_verified'] = variable_get('user_email_verification', TRUE);
  }
  if (in_array('profile', $requested_scopes)) {
    if (!empty($account->name)) {
      $claims['name'] = format_username($account);
      $claims['preferred_username'] = $account->name;
    }
    if (!empty($account->timezone)) {
      $claims['zoneinfo'] = $account->timezone;
    }
    if (user_access('access user profiles', drupal_anonymous_user())) {
      $claims['profile'] = url('user/' . $account->uid, array(
        'absolute' => TRUE,
      ));
    }
    if ($picture = $this
      ->getUserPicture($account)) {
      $claims['picture'] = $picture;
    }
  }

  // Allow modules to supply additional claims.
  $claims += module_invoke_all('oauth2_server_user_claims', $account, $requested_scopes);

  // Finally, allow modules to alter claims.
  drupal_alter('oauth2_server_user_claims', $claims, $account, $requested_scopes);
  return $claims;
}