You are here

function hook_openid_connect_userinfo_save in OpenID Connect / OAuth client 2.x

Same name and namespace in other branches
  1. 8 openid_connect.api.php \hook_openid_connect_userinfo_save()

Save userinfo hook.

This hook runs after the claim mappings have been applied by the OpenID Connect module, but before the account will be saved.

A popular use case for this hook is mapping additional information like user roles or other complex claims provided by the identity provider, that the OpenID Connect module has no mapping mechanisms for.

Parameters

\Drupal\user\UserInterface $account: A user account object.

array $context: An associative array with context information:

  • tokens: Array of original tokens.
  • user_data: Array of user and session data from the ID token.
  • userinfo: Array of user information from the userinfo endpoint.
  • plugin_id: The plugin identifier.
  • sub: The remote user identifier.
  • is_new: Whether the account was created during authorization.
1 invocation of hook_openid_connect_userinfo_save()
OpenIDConnect::saveUserinfo in src/OpenIDConnect.php
Save user profile information into a user account.

File

./openid_connect.api.php, line 250
Documentation for OpenID Connect module APIs.

Code

function hook_openid_connect_userinfo_save(UserInterface $account, array $context) {

  // Update only when the required information is available.
  if ($context['plugin_id'] != 'generic' || empty($context['userinfo']['my_info'])) {
    return;
  }

  // Note: For brevity, this example does not validate field
  // types, nor does it implement error handling.
  $my_info = $context['userinfo']['my_info'];
  foreach ($my_info as $key => $value) {
    $account
      ->set('field_' . $key, $value);
  }
}