/**
  * Create a token for a user, and return its value.
  *
  * @param string $token
  *   The refresh token.
  *
  * @throws BadRequestException
  *
  * @return RestfulTokenAuth
  *   The new access token.
  */
 public function refreshToken($token)
 {
     // Check if there is a token that did not expire yet.
     /* @var \Drupal\restful\Plugin\resource\DataProvider\DataProviderEntityInterface $data_provider */
     $data_provider = $this->getDataProvider();
     $query = $data_provider->EFQObject();
     $results = $query->entityCondition('entity_type', $this->entityType)->entityCondition('bundle', 'refresh_token')->propertyCondition('token', $token)->range(0, 1)->execute();
     if (empty($results['restful_token_auth'])) {
         throw new BadRequestException('Invalid refresh token.');
     }
     // Remove the refresh token once used.
     $refresh_token = entity_load_single('restful_token_auth', key($results['restful_token_auth']));
     $uid = $refresh_token->uid;
     // Get the access token linked to this refresh token then do some cleanup.
     $access_token_query = new EntityFieldQuery();
     $access_token_reference = $access_token_query->entityCondition('entity_type', 'restful_token_auth')->entityCondition('bundle', 'access_token')->fieldCondition('refresh_token_reference', 'target_id', $refresh_token->id)->range(0, 1)->execute();
     if (!empty($access_token_reference['restful_token_auth'])) {
         $access_token = key($access_token_reference['restful_token_auth']);
         entity_delete('restful_token_auth', $access_token);
     }
     $refresh_token->delete();
     // Create the new access token and return it.
     /* @var \Drupal\restful_token_auth\Entity\RestfulTokenAuthController $controller */
     $controller = entity_get_controller($this->getEntityType());
     $token = $controller->generateAccessToken($uid);
     return $this->view($token->id);
 }
 /**
  * Adds a condition to an already built SelectQuery (internal function).
  *
  * This is a helper for hook_entity_query() and hook_field_storage_query().
  *
  * @param SelectQuery $select_query
  *   A SelectQuery object.
  * @param string $sql_field
  *   The name of the field.
  * @param array $condition
  *   A condition as described in EntityFieldQuery::fieldCondition() and
  *   EntityFieldQuery::entityCondition().
  * @param bool $having
  *   HAVING or WHERE. This is necessary because SQL can't handle WHERE
  *   conditions on aliased columns.
  */
 public function addCondition(SelectQuery $select_query, $sql_field, $condition, $having = FALSE)
 {
     $needs_or = !empty($condition['or']) || in_array($condition['operator'], static::$leftJoinOperators);
     if (in_array($condition['operator'], array('CONTAINS', 'STARTS_WITH')) || !$needs_or) {
         parent::addCondition($select_query, $sql_field, $condition, $having);
         return;
     }
     $method = $having ? 'havingCondition' : 'condition';
     $db_or = db_or()->condition($sql_field, $condition['value'], $condition['operator']);
     if (strtoupper($condition['operator']) != 'IS NULL' && strtoupper($condition['operator']) != 'IS NOT NULL') {
         $db_or->condition($sql_field, NULL, 'IS NULL');
     }
     $select_query->{$method}($db_or);
 }