You are here

public function UserManager::addUserRecord in Social Auth 8.2

Same name and namespace in other branches
  1. 3.x src/User/UserManager.php \Drupal\social_auth\User\UserManager::addUserRecord()

Add user record in Social Auth Entity.

Parameters

int $user_id: Drupal User ID.

string $provider_user_id: Unique Social ID returned by social network.

string $token: For making API calls.

array|null $user_data: Additional user data collected.

Return value

true if user record is added in social_auth entity table Else false.

1 call to UserManager::addUserRecord()
UserManager::createNewUser in src/User/UserManager.php
Creates a new user.

File

src/User/UserManager.php, line 245

Class

UserManager
Manages database related tasks.

Namespace

Drupal\social_auth\User

Code

public function addUserRecord($user_id, $provider_user_id, $token, $user_data) {

  // Make sure we have everything we need.
  if (!$user_id || !$this->pluginId || !$provider_user_id) {
    $this->loggerFactory
      ->get($this
      ->getPluginId())
      ->error('Failed to add user record in Social Auth entity.
          User_id: @user_id, social_network_identifier: @social_network_identifier, provider_user_id : @provider_user_id ', [
      '@user_id' => $user_id,
      '@social_network_identifier' => $this->pluginId,
      '@provider_user_id ' => $provider_user_id,
    ]);
    $this->messenger
      ->addError($this
      ->t('You could not be authenticated, please contact the administrator.'));
    return FALSE;
  }
  else {

    // Add user record.
    $values = [
      'user_id' => $user_id,
      'plugin_id' => $this->pluginId,
      'provider_user_id' => $provider_user_id,
      'additional_data' => $user_data,
      'token' => $token,
    ];
    try {
      $user_info = SocialAuth::create($values);

      // Save the entity.
      $user_info
        ->save();
    } catch (\Exception $ex) {
      $this->loggerFactory
        ->get($this
        ->getPluginId())
        ->error('Failed to add user record in Social Auth entity.
            Exception: @message', [
        '@message' => $ex
          ->getMessage(),
      ]);
      $this->messenger
        ->addError($this
        ->t('You could not be authenticated, please contact the administrator.'));
      return FALSE;
    }
    return TRUE;
  }
}