You are here

public function UserManager::loadUserByProperty in Social Auth 3.x

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

Loads existing Drupal user object by given property and value.

Note that first matching user is returned. Email address and account name are unique so there can be only zero or one matching user when loading users by these properties.

Parameters

string $field: User entity field to search from.

string $value: Value to search for.

Return value

\Drupal\user\Entity\User|false Drupal user account if found False otherwise

1 call to UserManager::loadUserByProperty()
UserManager::generateUniqueUsername in src/User/UserManager.php
Ensures that Drupal usernames will be unique.

File

src/User/UserManager.php, line 309

Class

UserManager
Manages database related tasks.

Namespace

Drupal\social_auth\User

Code

public function loadUserByProperty($field, $value) {
  try {
    $users = $this->entityTypeManager
      ->getStorage('user')
      ->loadByProperties([
      $field => $value,
    ]);
    if (!empty($users)) {
      return current($users);
    }
  } catch (\Exception $ex) {
    $this->loggerFactory
      ->get($this
      ->getPluginId())
      ->error('Failed to load user. Exception: @message', [
      '@message' => $ex
        ->getMessage(),
    ]);
  }

  // If user was not found, return FALSE.
  return FALSE;
}