You are here

public function OpenIDConnect::saveUserinfo in OpenID Connect / OAuth client 8

Same name and namespace in other branches
  1. 2.x src/OpenIDConnect.php \Drupal\openid_connect\OpenIDConnect::saveUserinfo()

Save user profile information into a user account.

Parameters

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

array $context: An associative array with context information:

  • tokens: An array of tokens.
  • user_data: An array of user and session data.
  • userinfo: An array of user information.
  • plugin_id: The plugin identifier.
  • sub: The remote user identifier.
2 calls to OpenIDConnect::saveUserinfo()
OpenIDConnect::completeAuthorization in src/OpenIDConnect.php
Complete the authorization after tokens have been retrieved.
OpenIDConnect::connectCurrentUser in src/OpenIDConnect.php
Connect the current user's account to an external provider.

File

src/OpenIDConnect.php, line 584

Class

OpenIDConnect
Main service of the OpenID Connect module.

Namespace

Drupal\openid_connect

Code

public function saveUserinfo(UserInterface $account, array $context) {
  $userinfo = $context['userinfo'];
  $properties = $this->entityFieldManager
    ->getFieldDefinitions('user', 'user');
  $properties_skip = $this
    ->userPropertiesIgnore($context);
  foreach ($properties as $property_name => $property) {
    if (isset($properties_skip[$property_name])) {
      continue;
    }
    $userinfo_mappings = $this->configFactory
      ->get('openid_connect.settings')
      ->get('userinfo_mappings');
    if (isset($userinfo_mappings[$property_name])) {
      $claim = $userinfo_mappings[$property_name];
      if ($claim && isset($userinfo[$claim])) {
        $claim_value = $userinfo[$claim];
        $property_type = $property
          ->getType();
        $claim_context = $context + [
          'claim' => $claim,
          'property_name' => $property_name,
          'property_type' => $property_type,
          'userinfo_mappings' => $userinfo_mappings,
        ];
        $this->moduleHandler
          ->alter('openid_connect_userinfo_claim', $claim_value, $claim_context);

        // Set the user property, while ignoring exceptions from invalid
        // values.
        try {
          switch ($property_type) {
            case 'string':
            case 'string_long':
            case 'list_string':
            case 'datetime':
              $account
                ->set($property_name, $claim_value);
              break;
            case 'boolean':
              $account
                ->set($property_name, !empty($claim_value));
              break;
            case 'image':

              // Create file object from remote URL.
              $basename = explode('?', $this->fileSystem
                ->basename($claim_value))[0];
              $data = file_get_contents($claim_value);
              $file = file_save_data($data, 'public://user-picture-' . $account
                ->id() . '-' . $basename, FileSystemInterface::EXISTS_RENAME);

              // Cleanup the old file.
              if ($file) {
                $old_file = $account->{$property_name}->entity;
                if ($old_file) {
                  $old_file
                    ->delete();
                }
              }
              $account
                ->set($property_name, [
                'target_id' => $file
                  ->id(),
              ]);
              break;
            default:
              $this->logger
                ->error('Could not save user info, property type not implemented: %property_type', [
                '%property_type' => $property_type,
              ]);
              break;
          }
        } catch (\InvalidArgumentException $e) {
          $this->logger
            ->error($e
            ->getMessage());
        }
      }
    }
  }

  // Save the display name additionally in the user account 'data', for
  // use in openid_connect_username_alter().
  if (isset($userinfo['name'])) {
    $this->userData
      ->set('openid_connect', $account
      ->id(), 'oidc_name', $userinfo['name']);
  }

  // Allow other modules to add additional user information.
  $this->moduleHandler
    ->invokeAllDeprecated('openid_connect_save_userinfo() is deprecated and will be removed in 8.x-2.0.', 'openid_connect_save_userinfo', [
    $account,
    $context,
  ]);
  $this->moduleHandler
    ->invokeAll('openid_connect_userinfo_save', [
    $account,
    $context,
  ]);
  $account
    ->save();
}