feat(graphql): authorization messages (#1023)

This commit is contained in:
Kyrch
2025-12-17 20:09:55 -03:00
committed by GitHub
parent 144994676e
commit 245aadf381
9 changed files with 23 additions and 7 deletions
@@ -11,6 +11,7 @@ use App\GraphQL\Schema\Unions\BaseUnion;
use App\GraphQL\Support\Argument\Argument;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Illuminate\Auth\Access\Response;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Mutation;
@@ -18,6 +19,8 @@ abstract class BaseMutation extends Mutation
{
use ResolvesArguments;
protected Response $response;
public function __construct(
protected string $name,
) {
@@ -29,6 +32,11 @@ abstract class BaseMutation extends Mutation
);
}
public function getAuthorizationMessage(): string
{
return $this->response->message() ?? 'Unauthorized';
}
/**
* @return array<string, mixed>
*/
@@ -35,7 +35,7 @@ abstract class CreateMutation extends BaseMutation
->values()
->all();
return Gate::allows('create', [$this->model, ...$args]);
return ($this->response = Gate::inspect('create', [$this->model, ...$args]))->allowed();
}
/**
@@ -35,7 +35,7 @@ abstract class DeleteMutation extends BaseMutation
->values()
->all();
return Gate::allows('delete', $args);
return ($this->response = Gate::inspect('delete', $args))->allowed();
}
/**
@@ -37,7 +37,7 @@ abstract class UpdateMutation extends BaseMutation
->values()
->all();
return Gate::allows('update', $args);
return ($this->response = Gate::inspect('update', $args))->allowed();
}
/**
+8
View File
@@ -16,6 +16,7 @@ use App\GraphQL\Support\Argument\PageArgument;
use App\GraphQL\Support\Argument\SortArgument;
use App\GraphQL\Support\Filter\Filter;
use GraphQL\Type\Definition\Type;
use Illuminate\Auth\Access\Response;
use Illuminate\Support\Arr;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Query;
@@ -26,12 +27,19 @@ abstract class BaseQuery extends Query
use FiltersModels;
use ResolvesArguments;
protected Response $response;
public function __construct(
protected string $name,
protected bool $nullable = true,
protected bool $isList = false,
) {}
public function getAuthorizationMessage(): string
{
return $this->response->message() ?? 'Unauthorized';
}
/**
* @return array<string, mixed>
*/
@@ -42,7 +42,7 @@ abstract class EloquentPaginationQuery extends EloquentQuery
->values()
->all();
return Gate::allows('viewAny', [$this->model(), ...$args]);
return ($this->response = Gate::inspect('viewAny', [$this->model(), ...$args]))->allowed();
}
/**
@@ -29,7 +29,7 @@ abstract class EloquentSingularQuery extends EloquentQuery
->values()
->all();
return Gate::allows('view', $args);
return ($this->response = Gate::inspect('view', $args))->allowed();
}
/**
@@ -41,7 +41,7 @@ class AnimeYearsQuery extends BaseQuery
->values()
->all();
return Gate::allows('viewAny', [Anime::class, $model, ...$args]);
return ($this->response = Gate::inspect('viewAny', [Anime::class, $model, ...$args]))->allowed();
}
/**
@@ -38,7 +38,7 @@ class FindAnimeByExternalSiteQuery extends BaseQuery
public function authorize($root, array $args, $ctx, ?ResolveInfo $resolveInfo = null, ?Closure $getSelectFields = null): bool
{
return Gate::allows('viewAny', Anime::class);
return ($this->response = Gate::inspect('viewAny', Anime::class))->allowed();
}
/**