From 24c15ffb249e81154083dab79a67c40d2473bb1a Mon Sep 17 00:00:00 2001 From: Ark4ne Date: Tue, 7 Oct 2025 20:50:22 +0200 Subject: [PATCH 1/8] feat: add authorization capabilities with can method --- src/Descriptors/Relations/Relation.php | 51 ++++++++++++++++++-------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/src/Descriptors/Relations/Relation.php b/src/Descriptors/Relations/Relation.php index 46a3f83..a7bcdcb 100644 --- a/src/Descriptors/Relations/Relation.php +++ b/src/Descriptors/Relations/Relation.php @@ -6,6 +6,7 @@ use Ark4ne\JsonApi\Resources\Relationship; use Ark4ne\JsonApi\Traits\HasRelationLoad; use Closure; +use Illuminate\Contracts\Auth\Access\Gate; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; use Illuminate\Http\Resources\MissingValue; @@ -25,12 +26,13 @@ abstract class Relation extends Describer /** * @param class-string<\Ark4ne\JsonApi\Resources\JsonApiResource|\Ark4ne\JsonApi\Resources\JsonApiCollection> $related - * @param string|\Closure|null $relation + * @param string|\Closure|null $relation */ public function __construct( - protected string $related, + protected string $related, protected null|string|Closure $relation - ) { + ) + { } /** @@ -61,6 +63,24 @@ public function meta(Closure $meta): static return $this; } + /** + * @param iterable|string $abilities Abilities to check + * @param array $arguments Arguments to pass to the policy method, the model is always the first argument + * @param string $gateClass Gate class to use, defaults to the default Gate implementation + * @return static + */ + public function can(iterable|string $abilities, array $arguments = [], string $gateClass = Gate::class): static + { + return $this->when(fn( + Request $request, + Model $model, + string $attribute + ) => app($gateClass) + ->forUser($request->user()) + ->allows($abilities, [$model, ...$arguments]) + ); + } + /** * @param bool|null $whenIncluded * @return $this @@ -77,35 +97,34 @@ public function whenIncluded(null|bool $whenIncluded = null): static } /** - * @see \Illuminate\Http\Resources\ConditionallyLoadsAttributes::whenLoaded - * * @param string|null $relation * * @return static + * @see \Illuminate\Http\Resources\ConditionallyLoadsAttributes::whenLoaded */ - public function whenLoaded(null|string $relation = null): self + public function whenLoaded(null|string $relation = null): static { return $this->when(fn( Request $request, - Model $model, - string $attribute + Model $model, + string $attribute ): bool => $model->relationLoaded($relation ?? (is_string($this->relation) ? $this->relation : $attribute))); } /** - * @see \Illuminate\Http\Resources\ConditionallyLoadsAttributes::whenPivotLoadedAs - * - * @param string $table + * @param string $table * @param string|null $accessor * * @return static + * @see \Illuminate\Http\Resources\ConditionallyLoadsAttributes::whenPivotLoadedAs + * */ - public function whenPivotLoaded(string $table, null|string $accessor = null): self + public function whenPivotLoaded(string $table, null|string $accessor = null): static { return $this->when(fn( Request $request, - Model $model, - string $attribute + Model $model, + string $attribute ): bool => ($pivot = $model->{$accessor ?? (is_string($this->relation) ? $this->relation : $attribute)}) && ( $pivot instanceof $table || @@ -139,8 +158,8 @@ public function resolveFor(Request $request, mixed $model, string $field): Relat /** * @param \Illuminate\Http\Request $request - * @param T $model - * @param string $field + * @param T $model + * @param string $field * * @return mixed */ From db2d0df6ac5ef521e47249bec6aa7511294c68fc Mon Sep 17 00:00:00 2001 From: Ark4ne Date: Wed, 8 Oct 2025 20:48:11 +0200 Subject: [PATCH 2/8] refactor: rename 'field' to 'attribute' for clarity in relation methods --- src/Descriptors/Describer.php | 12 ++++----- src/Descriptors/Relations/Relation.php | 27 +++++++++++-------- src/Descriptors/Relations/RelationRaw.php | 2 +- .../Concerns/ConditionallyLoadsAttributes.php | 2 +- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/Descriptors/Describer.php b/src/Descriptors/Describer.php index 4d06905..4f2100f 100644 --- a/src/Descriptors/Describer.php +++ b/src/Descriptors/Describer.php @@ -84,17 +84,17 @@ public function whenFilled(): static /** * @param \Illuminate\Http\Request $request * @param T $model - * @param string $field + * @param string $attribute * * @return mixed */ - public function valueFor(Request $request, mixed $model, string $field): mixed + public function valueFor(Request $request, mixed $model, string $attribute): mixed { - if (!$this->check($request, $model, $field)) { + if (!$this->check($request, $model, $attribute)) { return new MissingValue(); } - return $this->resolveFor($request, $model, $field); + return $this->resolveFor($request, $model, $attribute); } /** @@ -134,11 +134,11 @@ private function retrieveValue(mixed $model, string $attribute): mixed /** * @param \Illuminate\Http\Request $request * @param T $model - * @param string $field + * @param string $attribute * * @return mixed */ - abstract protected function resolveFor(Request $request, mixed $model, string $field): mixed; + abstract protected function resolveFor(Request $request, mixed $model, string $attribute): mixed; /** * @return string|Closure|null diff --git a/src/Descriptors/Relations/Relation.php b/src/Descriptors/Relations/Relation.php index a7bcdcb..0fc442c 100644 --- a/src/Descriptors/Relations/Relation.php +++ b/src/Descriptors/Relations/Relation.php @@ -4,6 +4,7 @@ use Ark4ne\JsonApi\Descriptors\Describer; use Ark4ne\JsonApi\Resources\Relationship; +use Ark4ne\JsonApi\Support\Includes; use Ark4ne\JsonApi\Traits\HasRelationLoad; use Closure; use Illuminate\Contracts\Auth\Access\Gate; @@ -83,7 +84,7 @@ public function can(iterable|string $abilities, array $arguments = [], string $g /** * @param bool|null $whenIncluded - * @return $this + * @return static */ public function whenIncluded(null|bool $whenIncluded = null): static { @@ -93,7 +94,11 @@ public function whenIncluded(null|bool $whenIncluded = null): static $this->whenIncluded = $whenIncluded; } - return $this; + return $this->when(fn( + Request $request, + Model $model, + string $attribute + ): bool => !$this->whenIncluded || Includes::include($request, $attribute)); } /** @@ -133,21 +138,21 @@ public function whenPivotLoaded(string $table, null|string $accessor = null): st ); } - public function resolveFor(Request $request, mixed $model, string $field): Relationship + public function resolveFor(Request $request, mixed $model, string $attribute): Relationship { $retriever = $this->retriever(); if ($retriever instanceof Closure) { - $value = static fn() => $retriever($model, $field); + $value = static fn() => $retriever($model, $attribute); } else { $value = static fn() => match (true) { - $model instanceof Model => $model->getRelationValue($retriever ?? $field), - Arr::accessible($model) => $model[$retriever ?? $field], - default => $model->{$retriever ?? $field} + $model instanceof Model => $model->getRelationValue($retriever ?? $attribute), + Arr::accessible($model) => $model[$retriever ?? $attribute], + default => $model->{$retriever ?? $attribute} }; } - $relation = $this->value(fn() => $this->check($request, $model, $field) ? $value() : new MissingValue()); + $relation = $this->value(fn() => $this->check($request, $model, $attribute) ? $value() : new MissingValue()); if ($this->whenIncluded !== null) { $relation->whenIncluded($this->whenIncluded); @@ -159,13 +164,13 @@ public function resolveFor(Request $request, mixed $model, string $field): Relat /** * @param \Illuminate\Http\Request $request * @param T $model - * @param string $field + * @param string $attribute * * @return mixed */ - public function valueFor(Request $request, mixed $model, string $field): mixed + public function valueFor(Request $request, mixed $model, string $attribute): mixed { - return $this->resolveFor($request, $model, $field); + return $this->resolveFor($request, $model, $attribute); } abstract protected function value(Closure $value): Relationship; diff --git a/src/Descriptors/Relations/RelationRaw.php b/src/Descriptors/Relations/RelationRaw.php index a0666fa..3586f33 100644 --- a/src/Descriptors/Relations/RelationRaw.php +++ b/src/Descriptors/Relations/RelationRaw.php @@ -22,6 +22,6 @@ protected function value(Closure $value): Relationship public static function fromRelationship(Relationship $relationship): self { - return new self($relationship->getResource(), fn() => $relationship); + return new self($relationship->getResource(), static fn() => $relationship); } } diff --git a/src/Resources/Concerns/ConditionallyLoadsAttributes.php b/src/Resources/Concerns/ConditionallyLoadsAttributes.php index feb1d1a..78f1cbf 100644 --- a/src/Resources/Concerns/ConditionallyLoadsAttributes.php +++ b/src/Resources/Concerns/ConditionallyLoadsAttributes.php @@ -66,7 +66,7 @@ protected function applyWhen(bool|Closure $condition, iterable $data): MergeValu $value = new ValueMixed(is_callable($raw) ? $raw : static fn () => $raw); } - return $value->when(fn () => value($condition)); + return $value->when(static fn () => value($condition)); })); } From 2d1b88b295cfb25a4cb3d55c1dbd15e828ceca27 Mon Sep 17 00:00:00 2001 From: Ark4ne Date: Wed, 8 Oct 2025 20:48:35 +0200 Subject: [PATCH 3/8] fix: collection and schema tests for improved accuracy and stability --- composer.json | 2 +- tests/Feature/Comment/CollectionTest.php | 4 ++ tests/Feature/SchemaTest.php | 2 +- .../ConditionallyLoadsAttributesTest.php | 65 ++++++++++--------- 4 files changed, 40 insertions(+), 33 deletions(-) diff --git a/composer.json b/composer.json index 2677eb3..7192f23 100644 --- a/composer.json +++ b/composer.json @@ -44,5 +44,5 @@ ] } }, - "minimum-stability": "dev" + "minimum-stability": "stable" } diff --git a/tests/Feature/Comment/CollectionTest.php b/tests/Feature/Comment/CollectionTest.php index d486ce8..5568d3c 100644 --- a/tests/Feature/Comment/CollectionTest.php +++ b/tests/Feature/Comment/CollectionTest.php @@ -99,21 +99,25 @@ private function getJsonResult(Collection $comments, ?array $attributes = null, [ 'active' => false, 'label' => "« Previous", + 'page' => null, 'url' => null, ], [ 'active' => true, 'label' => '1', + 'page' => 1, 'url' => "http://localhost/comment?page=1", ], ...(array_map(static fn($value) => [ 'active' => false, 'label' => (string)$value, + 'page' => $value, 'url' => "http://localhost/comment?page=$value", ], range(2, 10))), [ 'active' => false, 'label' => "Next »", + 'page' => 2, 'url' => "http://localhost/comment?page=2", ], ], diff --git a/tests/Feature/SchemaTest.php b/tests/Feature/SchemaTest.php index 3a9a303..357fe4d 100644 --- a/tests/Feature/SchemaTest.php +++ b/tests/Feature/SchemaTest.php @@ -41,7 +41,7 @@ public function testSchema() $user->loads['main-post'] = 'post'; $user->loads['posts'] = 'posts'; $user->loads['comments'] = [ - 'comments' => fn(Builder $q) => $q->where('content', 'like', '%e%') + 'comments' => UserResource::schema()->loads['comments']['comments'] ]; $post->relationships['user'] = $user; diff --git a/tests/Unit/Resources/Concerns/ConditionallyLoadsAttributesTest.php b/tests/Unit/Resources/Concerns/ConditionallyLoadsAttributesTest.php index 617aeaa..6961f00 100644 --- a/tests/Unit/Resources/Concerns/ConditionallyLoadsAttributesTest.php +++ b/tests/Unit/Resources/Concerns/ConditionallyLoadsAttributesTest.php @@ -78,46 +78,49 @@ public function testApplyWhen() 'missing.1' => 'abc', 'missing.2' => 123, ]); - $this->assertEquals(new MergeValue([ - 'missing.1' => (new ValueMixed(fn() => 'abc'))->when(fn() => false), - 'missing.2' => (new ValueMixed(fn() => 123))->when(fn() => false), - ]), $actual); + $this->assertInstanceOf(MergeValue::class, $actual); + $this->assertInstanceOf(ValueMixed::class, $actual->data['missing.1']); + $this->assertInstanceOf(ValueMixed::class, $actual->data['missing.2']); + $this->assertEquals('abc', $actual->data['missing.1']->retriever()()); + $this->assertEquals(123, $actual->data['missing.2']->retriever()()); $actual = Reflect::invoke($stub, 'applyWhen', true, [ 'present.1' => 'abc', 'present.2' => 123, ]); - $this->assertEquals(new MergeValue([ - 'present.1' => (new ValueMixed(fn() => 'abc'))->when(fn() => true), - 'present.2' => (new ValueMixed(fn() => 123))->when(fn() => true), - ]), $actual); + $this->assertInstanceOf(ValueMixed::class, $actual->data['present.1']); + $this->assertInstanceOf(ValueMixed::class, $actual->data['present.2']); + $this->assertEquals('abc', $actual->data['present.1']->retriever()()); + $this->assertEquals(123, $actual->data['present.2']->retriever()()); + $actual = Reflect::invoke($stub, 'applyWhen', true, [ - 'present.1' => (new ValueMixed(fn() => 'abc')), - 'present.2' => (new ValueMixed(fn() => 123)), - 'present.3' => (new RelationOne('present', fn() => 'abc')), - 'present.4' => (new RelationOne('present', fn() => 123)), - 'present.5' => (new Relationship(UserResource::class, fn() => null)), + 'present.1' => $p1 = (new ValueMixed(fn() => 'abc')), + 'present.2' => $p2 = (new ValueMixed(fn() => 123)), + 'present.3' => $p3 = (new RelationOne('present', fn() => 'abc')), + 'present.4' => $p4 = (new RelationOne('present', fn() => 123)), + 'present.5' => $p5 = (new Relationship(UserResource::class, fn() => null)), ]); - $this->assertEquals(new MergeValue([ - 'present.1' => (new ValueMixed(fn() => 'abc'))->when(fn() => true), - 'present.2' => (new ValueMixed(fn() => 123))->when(fn() => true), - 'present.3' => (new RelationOne('present', fn() => 'abc'))->when(fn() => true), - 'present.4' => (new RelationOne('present', fn() => 123))->when(fn() => true), - 'present.5' => RelationRaw::fromRelationship(new Relationship(UserResource::class, fn() => null))->when(fn() => true), - ]), $actual); + $this->assertInstanceOf(MergeValue::class, $actual); + $this->assertEquals($p1, $actual->data['present.1']); + $this->assertEquals($p2, $actual->data['present.2']); + $this->assertEquals($p3, $actual->data['present.3']); + $this->assertEquals($p4, $actual->data['present.4']); + $this->assertInstanceOf(RelationRaw::class, $actual->data['present.5']); + $this->assertInstanceOf(Relationship::class, $actual->data['present.5']->retriever()()); + $actual = Reflect::invoke($stub, 'applyWhen', false, [ - 'missing.1' => (new ValueMixed(fn() => 'abc')), - 'missing.2' => (new ValueMixed(fn() => 123)), - 'missing.3' => (new RelationOne('present', fn() => 'abc')), - 'missing.4' => (new RelationOne('present', fn() => 123)), + 'missing.1' => $p1 = (new ValueMixed(fn() => 'abc')), + 'missing.2' => $p2 = (new ValueMixed(fn() => 123)), + 'missing.3' => $p3 = (new RelationOne('present', fn() => 'abc')), + 'missing.4' => $p4 = (new RelationOne('present', fn() => 123)), 'missing.5' => (new Relationship(UserResource::class, fn() => null)), ]); - $this->assertEquals(new MergeValue([ - 'missing.1' => (new ValueMixed(fn() => 'abc'))->when(fn() => false), - 'missing.2' => (new ValueMixed(fn() => 123))->when(fn() => false), - 'missing.3' => (new RelationOne('present', fn() => 'abc'))->when(fn() => false), - 'missing.4' => (new RelationOne('present', fn() => 123))->when(fn() => false), - 'missing.5' => RelationRaw::fromRelationship(new Relationship(UserResource::class, fn() => null))->when(fn() => false), - ]), $actual); + $this->assertInstanceOf(MergeValue::class, $actual); + $this->assertEquals($p1, $actual->data['missing.1']); + $this->assertEquals($p2, $actual->data['missing.2']); + $this->assertEquals($p3, $actual->data['missing.3']); + $this->assertEquals($p4, $actual->data['missing.4']); + $this->assertInstanceOf(RelationRaw::class, $actual->data['missing.5']); + $this->assertInstanceOf(Relationship::class, $actual->data['missing.5']->retriever()()); } public function testWhenHas() From b54b578968467924cf62b711d80c32a0a49a35da Mon Sep 17 00:00:00 2001 From: Ark4ne Date: Wed, 8 Oct 2025 22:16:22 +0200 Subject: [PATCH 4/8] feat: enhance pagination handling for Laravel 12 compatibility --- tests/Feature/Comment/CollectionTest.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/Feature/Comment/CollectionTest.php b/tests/Feature/Comment/CollectionTest.php index 5568d3c..7664dd3 100644 --- a/tests/Feature/Comment/CollectionTest.php +++ b/tests/Feature/Comment/CollectionTest.php @@ -3,6 +3,7 @@ namespace Test\Feature\Comment; use DateTimeInterface; +use Illuminate\Foundation\Application; use Illuminate\Http\Request; use Illuminate\Support\Collection; use Test\app\Http\Resources\PostResource; @@ -82,6 +83,8 @@ private function getJsonResult(Collection $comments, ?array $attributes = null, )) ->reduce(fn(Collection $all, Collection $value) => $all->merge($value), collect()); + $isLaravel12 = ((int)explode('.', Application::VERSION, 2)) >= 12; + return collect(array_filter([ 'data' => $data, 'included' => $include->uniqueStrict()->values()->all(), @@ -99,25 +102,26 @@ private function getJsonResult(Collection $comments, ?array $attributes = null, [ 'active' => false, 'label' => "« Previous", - 'page' => null, + ...($isLaravel12 ? ['page' => null] : []), 'url' => null, ], [ 'active' => true, 'label' => '1', - 'page' => 1, + ...($isLaravel12 ? ['page' => 1] : []), 'url' => "http://localhost/comment?page=1", ], ...(array_map(static fn($value) => [ 'active' => false, 'label' => (string)$value, - 'page' => $value, + ...($isLaravel12 ? ['page' => $value] : []), 'url' => "http://localhost/comment?page=$value", ], range(2, 10))), [ 'active' => false, 'label' => "Next »", 'page' => 2, + ...($isLaravel12 ? ['page' => 2] : []), 'url' => "http://localhost/comment?page=2", ], ], From cade32e968d8739c3957fe14c5389eb1d5e04a5c Mon Sep 17 00:00:00 2001 From: Ark4ne Date: Thu, 9 Oct 2025 09:39:21 +0200 Subject: [PATCH 5/8] feat: enhance pagination handling for Laravel 12 compatibility --- tests/Feature/Comment/CollectionTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Feature/Comment/CollectionTest.php b/tests/Feature/Comment/CollectionTest.php index 7664dd3..677fb05 100644 --- a/tests/Feature/Comment/CollectionTest.php +++ b/tests/Feature/Comment/CollectionTest.php @@ -120,7 +120,6 @@ private function getJsonResult(Collection $comments, ?array $attributes = null, [ 'active' => false, 'label' => "Next »", - 'page' => 2, ...($isLaravel12 ? ['page' => 2] : []), 'url' => "http://localhost/comment?page=2", ], From 07f344a251ac63aeb265371b6adddcf0b9e96a09 Mon Sep 17 00:00:00 2001 From: Ark4ne Date: Thu, 9 Oct 2025 10:32:32 +0200 Subject: [PATCH 6/8] fix: update test method signatures to include ignored parameter for consistency --- tests/Feature/Comment/CollectionTest.php | 2 +- tests/Unit/Descriptors/ValueTest.php | 6 +++--- tests/Unit/Support/ValuesTest.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Feature/Comment/CollectionTest.php b/tests/Feature/Comment/CollectionTest.php index 677fb05..8ec8a1e 100644 --- a/tests/Feature/Comment/CollectionTest.php +++ b/tests/Feature/Comment/CollectionTest.php @@ -83,7 +83,7 @@ private function getJsonResult(Collection $comments, ?array $attributes = null, )) ->reduce(fn(Collection $all, Collection $value) => $all->merge($value), collect()); - $isLaravel12 = ((int)explode('.', Application::VERSION, 2)) >= 12; + $isLaravel12 = ((int)explode('.', Application::VERSION, 2)[0]) >= 12; return collect(array_filter([ 'data' => $data, diff --git a/tests/Unit/Descriptors/ValueTest.php b/tests/Unit/Descriptors/ValueTest.php index 260843d..1ea4e02 100644 --- a/tests/Unit/Descriptors/ValueTest.php +++ b/tests/Unit/Descriptors/ValueTest.php @@ -91,7 +91,7 @@ public static function modelsValues() * @dataProvider values */ #[DataProvider('values')] - public function testConvertValue($class, $value, $excepted) + public function testConvertValue($class, $value, $excepted, $_ignored) { /** @var \Ark4ne\JsonApi\Descriptors\Values\Value $v */ $v = new $class(null); @@ -102,7 +102,7 @@ public function testConvertValue($class, $value, $excepted) * @dataProvider modelsValues */ #[DataProvider('modelsValues')] - public function testValueFor($model, $class, $value, $excepted) + public function testValueFor($model, $class, $value, $excepted, $_ignored) { data_set($model, 'attr', $value); @@ -118,7 +118,7 @@ public function testValueFor($model, $class, $value, $excepted) * @dataProvider modelsValues */ #[DataProvider('modelsValues')] - public function testValueForWithNull($model, $class, $value, $excepted) + public function testValueForWithNull($model, $class, $value, $excepted, $_ignored) { data_set($model, 'attr', null); diff --git a/tests/Unit/Support/ValuesTest.php b/tests/Unit/Support/ValuesTest.php index 8eb53e9..4963843 100644 --- a/tests/Unit/Support/ValuesTest.php +++ b/tests/Unit/Support/ValuesTest.php @@ -136,7 +136,7 @@ public static function dataAttribute() * @dataProvider dataAttribute */ #[DataProvider('dataAttribute')] - public function testHasAttribute($data, $attribute, $expected) + public function testHasAttribute($data, $attribute, $expected, $_ignored) { $this->assertEquals($expected, Values::hasAttribute($data, $attribute)); } From bd6308d1b63e423c35ad45839977136e0c428073 Mon Sep 17 00:00:00 2001 From: Ark4ne Date: Thu, 9 Oct 2025 11:50:06 +0200 Subject: [PATCH 7/8] docs: add Laravel policy on relationships to README --- readme.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 06e0b80..503b3b2 100644 --- a/readme.md +++ b/readme.md @@ -263,7 +263,7 @@ protected function toRelationships(Request $request): array `toRelationships` must returns an array, keyed by string, of `JsonApiResource` or `JsonApiCollection`. -#### Laravel conditional relationships +#### Laravel conditional relationships _**@see** [laravel: eloquent-conditional-relationships](https://laravel.com/docs/9.x/eloquent-resources#conditional-relationships)_ Support laravel conditional relationships. @@ -331,6 +331,21 @@ protected function toRelationships(Request $request): array } ``` +#### Laravel policy on relationships +_**@see** [laravel: authorizing-actions-via-gates](https://laravel.com/docs/12.x/authorization#authorizing-actions-via-gates)_ + +Apply laravel policy on relationships. + +```php +protected function toRelationships(Request $request): array +{ + return [ + 'posts' => $this->many(PostResource::class) + ->can('viewAny.post') + ]; +} +``` + ### toLinks _**@see** [{json:api}: resource-linkage](https://jsonapi.org/format/#document-resource-object-links)_ From 90a49e3252b6696d781d5ad5434827cc549b69a1 Mon Sep 17 00:00:00 2001 From: Ark4ne Date: Thu, 9 Oct 2025 16:28:05 +0200 Subject: [PATCH 8/8] refactor: remove unused can method and related documentation from Relation.php --- readme.md | 14 -------------- src/Descriptors/Relations/Relation.php | 19 ------------------- 2 files changed, 33 deletions(-) diff --git a/readme.md b/readme.md index 503b3b2..438ddb7 100644 --- a/readme.md +++ b/readme.md @@ -331,20 +331,6 @@ protected function toRelationships(Request $request): array } ``` -#### Laravel policy on relationships -_**@see** [laravel: authorizing-actions-via-gates](https://laravel.com/docs/12.x/authorization#authorizing-actions-via-gates)_ - -Apply laravel policy on relationships. - -```php -protected function toRelationships(Request $request): array -{ - return [ - 'posts' => $this->many(PostResource::class) - ->can('viewAny.post') - ]; -} -``` ### toLinks _**@see** [{json:api}: resource-linkage](https://jsonapi.org/format/#document-resource-object-links)_ diff --git a/src/Descriptors/Relations/Relation.php b/src/Descriptors/Relations/Relation.php index 0fc442c..dc2f5b5 100644 --- a/src/Descriptors/Relations/Relation.php +++ b/src/Descriptors/Relations/Relation.php @@ -7,7 +7,6 @@ use Ark4ne\JsonApi\Support\Includes; use Ark4ne\JsonApi\Traits\HasRelationLoad; use Closure; -use Illuminate\Contracts\Auth\Access\Gate; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; use Illuminate\Http\Resources\MissingValue; @@ -64,24 +63,6 @@ public function meta(Closure $meta): static return $this; } - /** - * @param iterable|string $abilities Abilities to check - * @param array $arguments Arguments to pass to the policy method, the model is always the first argument - * @param string $gateClass Gate class to use, defaults to the default Gate implementation - * @return static - */ - public function can(iterable|string $abilities, array $arguments = [], string $gateClass = Gate::class): static - { - return $this->when(fn( - Request $request, - Model $model, - string $attribute - ) => app($gateClass) - ->forUser($request->user()) - ->allows($abilities, [$model, ...$arguments]) - ); - } - /** * @param bool|null $whenIncluded * @return static