You are here

public function KeyAuth::getUserByKey in Key auth 8

Load the user associated with a given key.

Parameters

string $key: The API key to match to a user.

Return value

\Drupal\user\Entity\User|null The matching user entity, or NULL if there was no match.

Overrides KeyAuthInterface::getUserByKey

File

src/KeyAuth.php, line 88

Class

KeyAuth
Class KeyAuth.

Namespace

Drupal\key_auth

Code

public function getUserByKey($key) {

  // Load user storage.
  $storage = $this->entityTypeManager
    ->getStorage('user');

  // Query to find a user with this key.
  $user = $storage
    ->getQuery()
    ->condition('api_key', $key)
    ->execute();

  // Check if a user was found.
  if ($user) {

    // Load and return the user.
    return $storage
      ->load(reset($user));
  }
  return NULL;
}