You are here

public function W3CTokenManager::createAccessToken in W3C Validator 8

Access token generator helper.

Creates and stores a token to allow access as per specified user. If not specified, then the current user is used.

File

src/W3CTokenManager.php, line 47

Class

W3CTokenManager
Token manager: allow to be logged as a user from URL.

Namespace

Drupal\w3c_validator

Code

public function createAccessToken($user = NULL) {

  // Get current user if no custom value.
  if (!isset($user)) {
    $user = $this->currentUser;
  }

  // Build unique token.
  $time = time() + 20;
  $rand = mt_rand();
  $token = md5('w3c_validator' . $time . $rand . $user
    ->id());

  // Store unique token.
  $this->database
    ->insert('w3c_access_token')
    ->fields([
    'token' => $token,
    'expiration' => $time,
    'rand' => $rand,
    'uid' => $user
      ->id(),
  ])
    ->execute();
  return $token;
}