Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP FEATURE: Show authentication screen if needed #132

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Classes/GraphQL/Resolver/Type/MutationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Neos\Flow\Persistence\Exception\InvalidQueryException;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Flow\Security\Exception\AccessDeniedException;
use Neos\Flow\Security\Exception\AuthenticationRequiredException;
use Neos\Http\Factories\FlowUploadedFile;
use Neos\Media\Domain\Model\Asset;
use Neos\Media\Domain\Model\AssetSource\AssetProxy\AssetProxyInterface;
Expand Down Expand Up @@ -654,6 +656,8 @@ public function createTag($_, array $variables): Tag
'assetCollectionId' => $assetCollectionId
] = $variables + ['assetCollectionId' => null];

throw new AuthenticationRequiredException('Please login', 1651741371);

$tag = $this->tagRepository->findOneByLabel($label);
if ($tag === null) {
$tag = new Tag($label);
Expand Down
94 changes: 94 additions & 0 deletions Classes/GraphQL/Transform/FlowErrorTransform.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace Flowpack\Media\Ui\GraphQL\Transform;

use GraphQL\Error\Error;
use GraphQL\Executor\ExecutionResult;
use GraphQLTools\Transforms\Transform;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Log\ThrowableStorageInterface;
use Neos\Flow\Exception;
use Neos\Flow\Security\Authentication\EntryPoint\WebRedirect;
use Neos\Flow\Security\Context;

class FlowErrorTransform implements Transform
{
/**
* @Flow\Inject
*
* @var ThrowableStorageInterface
*/
protected $throwableStorage;

/**
* @Flow\InjectConfiguration("includeExceptionMessageInOutput")
*
* @var bool
*/
protected $includeExceptionMessageInOutput;

/**
* @Flow\Inject
* @var Context
*/
protected $securityContext;

public function transformResult(ExecutionResult $result): ExecutionResult
{
$result->errors = array_map(function (Error $error) {
$previousError = $error->getPrevious();

if (! $previousError instanceof Error) {
$message = $this->throwableStorage->logThrowable($previousError);
$extensions = [
'category' => 'internal',
'code' => 'INTERNAL_SERVER_ERROR',
];

if (! $this->includeExceptionMessageInOutput) {
$message = preg_replace('/.* - See also: (.+)\.txt$/s', 'Internal error ($1)', $message);
}

// Map status codes to Apollo Graph Error codes (https://www.apollographql.com/docs/apollo-server/data/errors/#error-codes)
if ($previousError instanceof Exception) {
$statusCode = $previousError->getStatusCode();
$extensions['statusCode'] = $statusCode;
$extensions['message'] = $previousError->getMessage();
$extensions['internalCode'] = $previousError->getReferenceCode();

switch ($statusCode) {
case 401:
$extensions['code'] = 'UNAUTHENTICATED';
foreach ($this->securityContext->getAuthenticationTokens() as $token) {
$entryPoint = $token->getAuthenticationEntryPoint();
if ($entryPoint instanceof WebRedirect) {
// TODO: Get proper url from WebRedirect entrypoint
$extensions['authenticationUrl'] = 'some login url';
}
}
break;
case 403:
$extensions['code'] = 'FORBIDDEN';
break;
}
}

return new Error(
$message,
$error->getNodes(),
$error->getSource(),
$error->getPositions(),
$error->getPath(),
$previousError,
$extensions,
);
}

return $error;
}, $result->errors);

return $result;
}
}
1 change: 1 addition & 0 deletions Configuration/Settings.GraphQL.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ t3n:
'media-assets':
logRequests: false
context: 'Flowpack\Media\Ui\GraphQL\Context\AssetSourceContext'
errorTransform: 'Flowpack\Media\Ui\GraphQL\Transform\FlowErrorTransform'
schemas:
root:
typeDefs: 'resource://Flowpack.Media.Ui/Private/GraphQL/schema.root.graphql'
Expand Down
2 changes: 1 addition & 1 deletion Resources/Public/AssetEditor/Plugin.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Resources/Public/AssetEditor/Plugin.js.map

Large diffs are not rendered by default.

Loading