You are here

public function AccessToken__1_0::getOrCreateToken in RESTful 7.2

Create a token for a user, and return its value.

File

modules/restful_token_auth/src/Plugin/resource/AccessToken__1_0.php, line 58
Contains Drupal\restful_token_auth\Plugin\resource\AccessToken__1_0.

Class

AccessToken__1_0
Class AccessToken__1_0 @package Drupal\restful_token_auth\Plugin\resource

Namespace

Drupal\restful_token_auth\Plugin\resource

Code

public function getOrCreateToken() {
  $entity_type = $this
    ->getEntityType();
  $account = $this
    ->getAccount();

  // Check if there is a token that did not expire yet.

  /* @var DataProviderEntityInterface $data_provider */
  $data_provider = $this
    ->getDataProvider();
  $query = $data_provider
    ->EFQObject();
  $result = $query
    ->entityCondition('entity_type', $entity_type)
    ->entityCondition('bundle', 'access_token')
    ->propertyCondition('uid', $account->uid)
    ->range(0, 1)
    ->execute();
  $token_exists = FALSE;
  if (!empty($result[$entity_type])) {
    $id = key($result[$entity_type]);
    $access_token = entity_load_single($entity_type, $id);
    $token_exists = TRUE;
    if (!empty($access_token->expire) && $access_token->expire < REQUEST_TIME) {
      if (variable_get('restful_token_auth_delete_expired_tokens', TRUE)) {

        // Token has expired, so we can delete this token.
        $access_token
          ->delete();
      }
      $token_exists = FALSE;
    }
  }
  if (!$token_exists) {

    /* @var \Drupal\restful_token_auth\Entity\RestfulTokenAuthController $controller */
    $controller = entity_get_controller($this
      ->getEntityType());
    $access_token = $controller
      ->generateAccessToken($account->uid);
    $id = $access_token->id;
  }
  $output = $this
    ->view($id);
  return $output;
}