You are here

public function CurrentUserInfo::process in JSON:API Resources 8

Process the resource request.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request.

\Drupal\jsonapi\ResourceType\ResourceType[] $resource_types: The route resource types.

Return value

\Drupal\jsonapi\ResourceResponse The response.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

tests/modules/jsonapi_resources_test/src/Resource/CurrentUserInfo.php, line 91

Class

CurrentUserInfo
Processes a request for the authenticated user's information.

Namespace

Drupal\jsonapi_resources_test\Resource

Code

public function process(Request $request, array $resource_types) {
  $user_storage = $this->entityTypeManager
    ->getStorage('user');
  $current_user = $user_storage
    ->load($this->currentUser
    ->id());
  assert($current_user instanceof UserInterface);
  $resource_type = reset($resource_types);
  $links = new LinkCollection([]);
  $primary_data = new ResourceObject($current_user, $resource_type, $current_user
    ->uuid(), NULL, [
    'displayName' => $current_user
      ->getDisplayName(),
    'roles' => $current_user
      ->getRoles(TRUE),
    'token' => $this->tokenGenerator
      ->get(CsrfRequestHeaderAccessCheck::TOKEN_KEY),
  ], $links);
  $top_level_data = new ResourceObjectData([
    $primary_data,
  ], 1);
  $response = $this
    ->createJsonapiResponse($top_level_data, $request);
  $response
    ->addCacheableDependency((new CacheableMetadata())
    ->addCacheContexts([
    'user',
  ]));
  return $response;
}