Skip to content

Commit

Permalink
Apply php-cs-fixer changes
Browse files Browse the repository at this point in the history
  • Loading branch information
k0ka authored and github-actions[bot] committed Dec 10, 2023
1 parent 0eb4f13 commit 383f798
Show file tree
Hide file tree
Showing 18 changed files with 57 additions and 183 deletions.
37 changes: 8 additions & 29 deletions src/Auth/BaseCanDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,14 @@

namespace Nuwave\Lighthouse\Auth;

use GraphQL\Error\Error;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Arr;
use Nuwave\Lighthouse\Exceptions\AuthorizationException;
use Nuwave\Lighthouse\Exceptions\DefinitionException;
use Nuwave\Lighthouse\Execution\Resolved;
use Nuwave\Lighthouse\Execution\ResolveInfo;
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Schema\RootType;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\SoftDeletes\ForceDeleteDirective;
use Nuwave\Lighthouse\SoftDeletes\RestoreDirective;
use Nuwave\Lighthouse\SoftDeletes\TrashedDirective;
use Nuwave\Lighthouse\Support\Contracts\FieldManipulator;
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use Nuwave\Lighthouse\Support\Utils;
use Throwable;

abstract class BaseCanDirective extends BaseDirective implements FieldMiddleware
{
Expand Down Expand Up @@ -103,38 +85,35 @@ public function handleField(FieldValue $fieldValue): void
$fieldValue->wrapResolver(fn (callable $resolver): \Closure => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver, $ability) {
$gate = $this->gate->forUser($context->user());
$checkArguments = $this->buildCheckArguments($args);
$authorizeModel = fn(string|object|array|null $model) => $this->authorizeModel($gate, $ability, $model, $checkArguments);
$authorizeModel = fn (string|object|array|null $model) => $this->authorizeModel($gate, $ability, $model, $checkArguments);

try {
return $this->authorizeRequest($root, $args, $context, $resolveInfo, $resolver, $authorizeModel);
} catch (Throwable $e) {
} catch (\Throwable $e) {
$action = $this->directiveArgValue('action');
if ($action === 'EXCEPTION_NOT_AUTHORIZED'){
if ($action === 'EXCEPTION_NOT_AUTHORIZED') {
throw new AuthorizationException();
}

if ($action === 'RETURN_VALUE') {
return $this->directiveArgValue('return_value');
}


throw $e;
}
});
}

/**
* Authorizes request and resolves the field
* Authorizes request and resolves the field.
*
* @param array<string, mixed> $args
* @throws \Nuwave\Lighthouse\Exceptions\AuthorizationException
* @param array<string, mixed> $args
*/
protected abstract function authorizeRequest(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo, callable $resolver, callable $authorize): mixed;
abstract protected function authorizeRequest(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo, callable $resolver, callable $authorize): mixed;

/**
* @param string|array<string> $ability
* @param array<int, mixed> $arguments
* @throws \Nuwave\Lighthouse\Exceptions\AuthorizationException
* @param string|array<string> $ability
* @param array<int, mixed> $arguments
*/
protected function authorizeModel(Gate $gate, string|array $ability, mixed $model, array $arguments): void
{
Expand Down
18 changes: 3 additions & 15 deletions src/Auth/CanFindDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,14 @@
namespace Nuwave\Lighthouse\Auth;

use GraphQL\Error\Error;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Arr;
use Nuwave\Lighthouse\Exceptions\AuthorizationException;
use Nuwave\Lighthouse\Exceptions\DefinitionException;
use Nuwave\Lighthouse\Execution\Resolved;
use Nuwave\Lighthouse\Execution\ResolveInfo;
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Schema\RootType;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\SoftDeletes\ForceDeleteDirective;
use Nuwave\Lighthouse\SoftDeletes\RestoreDirective;
use Nuwave\Lighthouse\SoftDeletes\TrashedDirective;
use Nuwave\Lighthouse\Support\Contracts\FieldManipulator;
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use Nuwave\Lighthouse\Support\Utils;

Expand All @@ -33,14 +19,15 @@ class CanFindDirective extends BaseCanDirective
public static function definition(): string
{
$commonArguments = BaseCanDirective::commonArguments();

return /** @lang GraphQL */ <<<GRAPHQL
"""
Check a Laravel Policy to ensure the current user is authorized to access a field.
Query for specific model instances to check the policy against, using primary key(s) from specified argument.
"""
directive @canFind(
$commonArguments
{$commonArguments}
"""
Specify the name of the field argument that contains its primary key(s).
Expand All @@ -67,6 +54,7 @@ protected function authorizeRequest(mixed $root, array $args, GraphQLContext $co
foreach ($this->modelsToCheck($root, $args, $context, $resolveInfo) as $model) {
$authorize($model);
}

return $resolver($root, $args, $context, $resolveInfo);
}

Expand Down
27 changes: 3 additions & 24 deletions src/Auth/CanModelDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,23 @@

namespace Nuwave\Lighthouse\Auth;

use GraphQL\Error\Error;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Arr;
use Nuwave\Lighthouse\Exceptions\AuthorizationException;
use Nuwave\Lighthouse\Exceptions\DefinitionException;
use Nuwave\Lighthouse\Execution\Resolved;
use Nuwave\Lighthouse\Execution\ResolveInfo;
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Schema\RootType;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\SoftDeletes\ForceDeleteDirective;
use Nuwave\Lighthouse\SoftDeletes\RestoreDirective;
use Nuwave\Lighthouse\SoftDeletes\TrashedDirective;
use Nuwave\Lighthouse\Support\Contracts\FieldManipulator;
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use Nuwave\Lighthouse\Support\Utils;

class CanModelDirective extends BaseCanDirective
{
public static function definition(): string
{
$commonArguments = BaseCanDirective::commonArguments();

return /** @lang GraphQL */ <<<GRAPHQL
"""
Check a Laravel Policy to ensure the current user is authorized to access a field.
Check the policy against the root model.
"""
directive @canRoot(
$commonArguments
{$commonArguments}
"""
The model name to check against.
Expand All @@ -54,6 +32,7 @@ public static function definition(): string
protected function authorizeRequest(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo, callable $resolver, callable $authorize): mixed
{
$authorize($this->getModelClass());

return $resolver($root, $args, $context, $resolveInfo);
}
}
29 changes: 4 additions & 25 deletions src/Auth/CanQueryDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,15 @@

namespace Nuwave\Lighthouse\Auth;

use GraphQL\Error\Error;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Arr;
use Nuwave\Lighthouse\Exceptions\AuthorizationException;
use Nuwave\Lighthouse\Exceptions\DefinitionException;
use Nuwave\Lighthouse\Execution\Resolved;
use Nuwave\Lighthouse\Execution\ResolveInfo;
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Schema\RootType;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\SoftDeletes\ForceDeleteDirective;
use Nuwave\Lighthouse\SoftDeletes\RestoreDirective;
use Nuwave\Lighthouse\SoftDeletes\TrashedDirective;
use Nuwave\Lighthouse\Support\Contracts\FieldManipulator;
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use Nuwave\Lighthouse\Support\Utils;

class CanQueryDirective extends BaseCanDirective
{
public static function definition(): string
{
$commonArguments = BaseCanDirective::commonArguments();

return /** @lang GraphQL */ <<<GRAPHQL
"""
Check a Laravel Policy to ensure the current user is authorized to access a field.
Expand All @@ -41,7 +19,7 @@ public static function definition(): string
with directives that add constraints to the query builder, such as `@eq`.
"""
directive @canQuery(
$commonArguments
{$commonArguments}
"""
Apply scopes to the underlying query.
Expand All @@ -63,9 +41,10 @@ protected function authorizeRequest(mixed $root, array $args, GraphQLContext $co
$resolveInfo,
)
->get();
foreach ($models as $model){
foreach ($models as $model) {
$authorize($model);
}

return $resolver($root, $args, $context, $resolveInfo);
}
}
18 changes: 3 additions & 15 deletions src/Auth/CanResolvedDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,17 @@

namespace Nuwave\Lighthouse\Auth;

use GraphQL\Error\Error;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Arr;
use Nuwave\Lighthouse\Exceptions\AuthorizationException;
use Nuwave\Lighthouse\Exceptions\DefinitionException;
use Nuwave\Lighthouse\Execution\Resolved;
use Nuwave\Lighthouse\Execution\ResolveInfo;
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Schema\RootType;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\SoftDeletes\ForceDeleteDirective;
use Nuwave\Lighthouse\SoftDeletes\RestoreDirective;
use Nuwave\Lighthouse\SoftDeletes\TrashedDirective;
use Nuwave\Lighthouse\Support\Contracts\FieldManipulator;
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use Nuwave\Lighthouse\Support\Utils;

Expand All @@ -33,6 +21,7 @@ class CanResolvedDirective extends BaseCanDirective implements FieldManipulator
public static function definition(): string
{
$commonArguments = BaseCanDirective::commonArguments();

return /** @lang GraphQL */ <<<GRAPHQL
"""
Check a Laravel Policy to ensure the current user is authorized to access a field.
Expand All @@ -41,7 +30,7 @@ public static function definition(): string
Only use this if the field does not mutate data, it is run before checking.
"""
directive @canResolved(
$commonArguments
{$commonArguments}
) repeatable on FIELD_DEFINITION
GRAPHQL;
}
Expand All @@ -67,8 +56,7 @@ function ($modelLike) use ($authorize) {
public function manipulateFieldDefinition(DocumentAST &$documentAST, FieldDefinitionNode &$fieldDefinition, ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode &$parentType): void
{
if ($parentType->name->value === RootType::MUTATION) {
throw new DefinitionException("Do not use @can with `resolved` on mutation $fieldDefinition->name->value, it is unsafe as the resolver will run before checking permissions. Use `query` or `find`.");
throw new DefinitionException("Do not use @can with `resolved` on mutation {$fieldDefinition->name}->value, it is unsafe as the resolver will run before checking permissions. Use `query` or `find`.");
}
}

}
27 changes: 3 additions & 24 deletions src/Auth/CanRootDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,31 @@

namespace Nuwave\Lighthouse\Auth;

use GraphQL\Error\Error;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Arr;
use Nuwave\Lighthouse\Exceptions\AuthorizationException;
use Nuwave\Lighthouse\Exceptions\DefinitionException;
use Nuwave\Lighthouse\Execution\Resolved;
use Nuwave\Lighthouse\Execution\ResolveInfo;
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Schema\RootType;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\SoftDeletes\ForceDeleteDirective;
use Nuwave\Lighthouse\SoftDeletes\RestoreDirective;
use Nuwave\Lighthouse\SoftDeletes\TrashedDirective;
use Nuwave\Lighthouse\Support\Contracts\FieldManipulator;
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use Nuwave\Lighthouse\Support\Utils;

class CanRootDirective extends BaseCanDirective
{
public static function definition(): string
{
$commonArguments = BaseCanDirective::commonArguments();

return /** @lang GraphQL */ <<<GRAPHQL
"""
Check a Laravel Policy to ensure the current user is authorized to access a field.
Check the policy against the root object.
"""
directive @canRoot(
$commonArguments
{$commonArguments}
) repeatable on FIELD_DEFINITION
GRAPHQL;
}

protected function authorizeRequest(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo, callable $resolver, callable $authorize): mixed
{
$authorize($root);

return $resolver($root, $args, $context, $resolveInfo);
}
}
2 changes: 1 addition & 1 deletion src/Tracing/FederatedTracing/Proto/FieldStat.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Tracing/FederatedTracing/Proto/QueryLatencyStats.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 383f798

Please sign in to comment.