You are here

public function ApiTokenBase::validateToken in API Tokens 8

Same name and namespace in other branches
  1. 8.2 src/ApiTokenBase.php \Drupal\api_tokens\ApiTokenBase::validateToken()

Performs one-time context-independent validation of the API token.

Return value

bool

Overrides ApiTokenPluginInterface::validateToken

1 call to ApiTokenBase::validateToken()
ApiTokenBase::placeholder in src/ApiTokenBase.php
Returns a #lazy_builder placeholder for the API tokens filter.

File

src/ApiTokenBase.php, line 191

Class

ApiTokenBase
Provides a base class for the API token plugins.

Namespace

Drupal\api_tokens

Code

public function validateToken() {
  if (!$this->reflector) {
    $this->logger
      ->warning($this
      ->t('ApiToken plugin "@label" has no "build" method.', [
      '@label' => $this
        ->label(),
    ]));
    return FALSE;
  }
  if (!is_array($this->params)) {
    $this->logger
      ->warning($this
      ->t('API token "@token" has invalid parameters format.', [
      '@token' => $this->token,
    ]));
    return FALSE;
  }
  if ($this->reflector
    ->getNumberOfRequiredParameters() > count($this->params)) {
    $this->logger
      ->warning($this
      ->t('API token "@token" has not enough parameters.', [
      '@token' => $this->token,
    ]));
    return FALSE;
  }
  $params = [];
  foreach ($this->reflector
    ->getParameters() as $index => $param) {
    $use_default = $param
      ->isOptional() && !isset($this->params[$index]);
    $params[$param
      ->getName()] = $use_default ? $param
      ->getDefaultValue() : $this->params[$index];
  }
  $provided_count = count($this->params);
  $defined_count = count($params);
  if ($provided_count > $defined_count) {
    for ($index = $defined_count; $index < $provided_count; ++$index) {
      $params[$index] = $this->params[$index];
    }
  }
  if (!$this
    ->validate($params)) {
    $this->logger
      ->warning($this
      ->t('API token "@token" has invalid parameters.', [
      '@token' => $this->token,
    ]));
    return FALSE;
  }
  return TRUE;
}