handle($request); * if ($this->context->getAspect('backend.user')->isLoggedIn()) { * return $this->applyHeadersToResponse($response); * } * ``` * * @internal this class might get merged again with the subclasses */ abstract class BackendUserAuthenticator implements MiddlewareInterface { public function __construct(protected Context $context) {} abstract public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface; /** * Adding headers to the response to avoid caching on the client side. * These headers will override any previous headers of these names sent. * Get the http headers to be sent if an authenticated user is available, * in order to disallow browsers to store the response on the client side. * * @return ResponseInterface the modified response object. */ protected function applyHeadersToResponse(ResponseInterface $response): ResponseInterface { if ($response->getHeaderLine('Cache-Control')) { return $response; } $headers = [ 'Expires' => 0, 'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT', 'Cache-Control' => 'no-cache, no-store', // HTTP 1.0 compatibility, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Pragma 'Pragma' => 'no-cache', ]; foreach ($headers as $headerName => $headerValue) { $response = $response->withHeader($headerName, (string)$headerValue); } return $response; } /** * Register the backend user as aspect */ protected function setBackendUserAspect(?BackendUserAuthentication $user, ?int $alternativeWorkspaceId = null): void { $this->context->setAspect('backend.user', new UserAspect($user)); $this->context->setAspect('workspace', new WorkspaceAspect($alternativeWorkspaceId ?? $user->workspace ?? 0)); } }