From 3b5c192795d5913baa815700207f5011b4380c38 Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 5 Jun 2025 20:25:14 +0200 Subject: [PATCH 01/10] Fix gate --- src/Query/Traits/PerformMutation.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Query/Traits/PerformMutation.php b/src/Query/Traits/PerformMutation.php index 575321a..a26bd12 100644 --- a/src/Query/Traits/PerformMutation.php +++ b/src/Query/Traits/PerformMutation.php @@ -64,7 +64,9 @@ public function applyMutation(array $mutation = [], $attributes = []) } elseif ($mutation['operation'] === 'update') { $this->resource->authorizeTo('update', $model); } else { - $this->resource->authorizeTo('view', $model); + if (!$this->resource->authorizeTo('attach' . $model, $model)) { + $this->resource->authorizeTo('view', $model); + } } return $this->mutateModel( From a9d2dc4e94a3fedd3da260217229e51fe1e6ddf9 Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 5 Jun 2025 20:35:57 +0200 Subject: [PATCH 02/10] Fix styleUi --- src/Query/Traits/PerformMutation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Query/Traits/PerformMutation.php b/src/Query/Traits/PerformMutation.php index a26bd12..f7de249 100644 --- a/src/Query/Traits/PerformMutation.php +++ b/src/Query/Traits/PerformMutation.php @@ -64,7 +64,7 @@ public function applyMutation(array $mutation = [], $attributes = []) } elseif ($mutation['operation'] === 'update') { $this->resource->authorizeTo('update', $model); } else { - if (!$this->resource->authorizeTo('attach' . $model, $model)) { + if (!$this->resource->authorizeTo('attach'.$model, $model)) { $this->resource->authorizeTo('view', $model); } } From 090e201277d86b84a99795d4f0081aacc60abb6a Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 6 Jun 2025 08:11:37 +0200 Subject: [PATCH 03/10] Fix Mutator Authorize fnct --- src/Query/Traits/PerformMutation.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Query/Traits/PerformMutation.php b/src/Query/Traits/PerformMutation.php index f7de249..1bae7b9 100644 --- a/src/Query/Traits/PerformMutation.php +++ b/src/Query/Traits/PerformMutation.php @@ -33,9 +33,7 @@ public function mutate(array $parameters = []) ]; foreach ($parameters['mutate'] as $parameter) { - $operations[ - $this->mutateOperationsVerbose[$parameter['operation']] - ][] = $this->applyMutation($parameter)->getKey(); + $operations[$this->mutateOperationsVerbose[$parameter['operation']]][] = $this->applyMutation($parameter)->getKey(); } return $operations; @@ -44,7 +42,7 @@ public function mutate(array $parameters = []) /** * Apply a mutation to the model based on the provided mutation parameters. * - * @param array $mutation An array of mutation parameters. + * @param array $mutation An array of mutation parameters. * @param array $attributes Additional attributes to apply to the model. * * @return Model The mutated model. @@ -64,8 +62,10 @@ public function applyMutation(array $mutation = [], $attributes = []) } elseif ($mutation['operation'] === 'update') { $this->resource->authorizeTo('update', $model); } else { - if (!$this->resource->authorizeTo('attach'.$model, $model)) { + if (!$this->resource->authorizedTo('attach' . $model, $model)) { $this->resource->authorizeTo('view', $model); + } else { + $this->resource->authorizeTo('attach' . $model, $model); } } @@ -79,9 +79,9 @@ public function applyMutation(array $mutation = [], $attributes = []) /** * Mutate the model by applying attributes and relations. * - * @param Model $model The Eloquent model to mutate. + * @param Model $model The Eloquent model to mutate. * @param array $attributes The attributes to mutate. - * @param array $mutation The mutation array. + * @param array $mutation The mutation array. * * @return Model The mutated model. */ From fae83ffc8772475ae4041a562f2cb23027c06ae3 Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 6 Jun 2025 08:13:44 +0200 Subject: [PATCH 04/10] Fix StyleCI --- src/Query/Traits/PerformMutation.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Query/Traits/PerformMutation.php b/src/Query/Traits/PerformMutation.php index 1bae7b9..9bad7cc 100644 --- a/src/Query/Traits/PerformMutation.php +++ b/src/Query/Traits/PerformMutation.php @@ -62,10 +62,10 @@ public function applyMutation(array $mutation = [], $attributes = []) } elseif ($mutation['operation'] === 'update') { $this->resource->authorizeTo('update', $model); } else { - if (!$this->resource->authorizedTo('attach' . $model, $model)) { + if (!$this->resource->authorizedTo('attach'.$model, $model)) { $this->resource->authorizeTo('view', $model); } else { - $this->resource->authorizeTo('attach' . $model, $model); + $this->resource->authorizeTo('attach'.$model, $model); } } @@ -79,9 +79,9 @@ public function applyMutation(array $mutation = [], $attributes = []) /** * Mutate the model by applying attributes and relations. * - * @param Model $model The Eloquent model to mutate. + * @param Model $model The Eloquent model to mutate. * @param array $attributes The attributes to mutate. - * @param array $mutation The mutation array. + * @param array $mutation The mutation array. * * @return Model The mutated model. */ From 70f68d5bac4ee4e8b84428341f3faac82bea99dc Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 6 Jun 2025 08:14:37 +0200 Subject: [PATCH 05/10] Fix StyleCI --- src/Query/Traits/PerformMutation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Query/Traits/PerformMutation.php b/src/Query/Traits/PerformMutation.php index 9bad7cc..f58b2f5 100644 --- a/src/Query/Traits/PerformMutation.php +++ b/src/Query/Traits/PerformMutation.php @@ -42,7 +42,7 @@ public function mutate(array $parameters = []) /** * Apply a mutation to the model based on the provided mutation parameters. * - * @param array $mutation An array of mutation parameters. + * @param array $mutation An array of mutation parameters. * @param array $attributes Additional attributes to apply to the model. * * @return Model The mutated model. From 41dda586c50fc8221c5e4f631f75c8d3e28931e3 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 9 Jun 2025 11:05:02 +0200 Subject: [PATCH 06/10] Add tests and fix PeformMutation --- src/Query/Traits/PerformMutation.php | 6 +- tests/Support/Policies/GreenPolicy.php | 115 ++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 3 deletions(-) diff --git a/src/Query/Traits/PerformMutation.php b/src/Query/Traits/PerformMutation.php index f58b2f5..2fa31fd 100644 --- a/src/Query/Traits/PerformMutation.php +++ b/src/Query/Traits/PerformMutation.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Model; use Lomkit\Rest\Http\Requests\MutateRequest; use Lomkit\Rest\Http\Requests\RestRequest; +use ReflectionClass; trait PerformMutation { @@ -62,10 +63,11 @@ public function applyMutation(array $mutation = [], $attributes = []) } elseif ($mutation['operation'] === 'update') { $this->resource->authorizeTo('update', $model); } else { - if (!$this->resource->authorizedTo('attach'.$model, $model)) { + $attachModel = (new ReflectionClass($model))->getShortName(); + if (!$this->resource->authorizedTo('attach'.$attachModel, $model)) { $this->resource->authorizeTo('view', $model); } else { - $this->resource->authorizeTo('attach'.$model, $model); + $this->resource->authorizeTo('attach'.$attachModel, $model); } } diff --git a/tests/Support/Policies/GreenPolicy.php b/tests/Support/Policies/GreenPolicy.php index cf3ad5c..95c8043 100644 --- a/tests/Support/Policies/GreenPolicy.php +++ b/tests/Support/Policies/GreenPolicy.php @@ -31,7 +31,7 @@ public function viewAny($user) */ public function view($user, Model $model) { - return true; + return false; } /** @@ -97,4 +97,117 @@ public function forceDelete($user, Model $model) { return true; } + + public function attachBelongsToRelation($user, Model $model) + { + return true; + } + + public function attachHasOneRelation($user, Model $model) + { + return true; + } + + public function attachHasOneOfManyRelation($user, Model $model) + { + return true; + } + + public function attachBelongsToManyRelation($user, Model $model) + { + return true; + } + + public function attachHasManyRelation($user, Model $model) + { + return true; + } + + public function detachBelongsToRelation($user, Model $model) + { + return true; + } + + public function detachHasOneRelation($user, Model $model) + { + return true; + } + + public function detachHasOneOfManyRelation($user, Model $model) + { + return true; + } + + public function detachBelongsToManyRelation($user, Model $model) + { + return true; + } + + public function detachHasManyRelation($user, Model $model) + { + return true; + } + + /** + * MORPHS. + */ + public function attachMorphToRelation($user, Model $model) + { + return true; + } + + public function attachMorphOneRelation($user, Model $model) + { + return true; + } + + public function attachMorphOneOfManyRelation($user, Model $model) + { + return true; + } + + public function attachMorphToManyRelation($user, Model $model) + { + return true; + } + + public function attachMorphManyRelation($user, Model $model) + { + return true; + } + + public function attachMorphedByManyRelation($user, Model $model) + { + return true; + } + + public function detachMorphToRelation($user, Model $model) + { + return true; + } + + public function detachMorphOneRelation($user, Model $model) + { + return true; + } + + public function detachMorphOneOfManyRelation($user, Model $model) + { + return true; + } + + public function detachMorphToManyRelation($user, Model $model) + { + return true; + } + + public function detachMorphManyRelation($user, Model $model) + { + return true; + } + + public function detachMorphedByManyRelation($user, Model $model) + { + return true; + } } From 49f82ac02df62a2ff8877610840400c1b23daf61 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 9 Jun 2025 11:12:04 +0200 Subject: [PATCH 07/10] Fix tests --- tests/Feature/Controllers/AutomaticGatingTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Feature/Controllers/AutomaticGatingTest.php b/tests/Feature/Controllers/AutomaticGatingTest.php index 994598a..cdb947b 100644 --- a/tests/Feature/Controllers/AutomaticGatingTest.php +++ b/tests/Feature/Controllers/AutomaticGatingTest.php @@ -48,7 +48,7 @@ public function test_searching_automatic_gated_resource(): void [ [ 'gates' => [ - 'authorized_to_view' => true, + 'authorized_to_view' => false, 'authorized_to_update' => true, 'authorized_to_delete' => true, 'authorized_to_restore' => true, @@ -331,11 +331,11 @@ public function test_searching_automatic_gated_resource_with_belongs_to_many_rel [ [ 'gates' => [ - 'authorized_to_view' => true, + 'authorized_to_view' => false, 'authorized_to_update' => true, 'authorized_to_delete' => true, 'authorized_to_restore' => true, - 'authorized_to_force_delete' => true, + 'authorized_to_force_delete' => true ], 'belongs_to_many_relation' => $matchingModel->belongsToManyRelation() ->orderBy('id', 'desc') @@ -357,7 +357,7 @@ public function test_searching_automatic_gated_resource_with_belongs_to_many_rel ], [ 'gates' => [ - 'authorized_to_view' => true, + 'authorized_to_view' => false, 'authorized_to_update' => true, 'authorized_to_delete' => true, 'authorized_to_restore' => true, From 352bbc0a3e1ca279424dc3ca51d6928f4554c642 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 9 Jun 2025 11:13:06 +0200 Subject: [PATCH 08/10] Fix styleCI --- tests/Feature/Controllers/AutomaticGatingTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/Controllers/AutomaticGatingTest.php b/tests/Feature/Controllers/AutomaticGatingTest.php index cdb947b..561723c 100644 --- a/tests/Feature/Controllers/AutomaticGatingTest.php +++ b/tests/Feature/Controllers/AutomaticGatingTest.php @@ -335,7 +335,7 @@ public function test_searching_automatic_gated_resource_with_belongs_to_many_rel 'authorized_to_update' => true, 'authorized_to_delete' => true, 'authorized_to_restore' => true, - 'authorized_to_force_delete' => true + 'authorized_to_force_delete' => true, ], 'belongs_to_many_relation' => $matchingModel->belongsToManyRelation() ->orderBy('id', 'desc') From 20c7bbe8dc77542d936062853d5c323341963ead Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 9 Jun 2025 11:18:01 +0200 Subject: [PATCH 09/10] Fix styleCI --- tests/Feature/Controllers/AutomaticGatingTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/Controllers/AutomaticGatingTest.php b/tests/Feature/Controllers/AutomaticGatingTest.php index 561723c..cdb947b 100644 --- a/tests/Feature/Controllers/AutomaticGatingTest.php +++ b/tests/Feature/Controllers/AutomaticGatingTest.php @@ -335,7 +335,7 @@ public function test_searching_automatic_gated_resource_with_belongs_to_many_rel 'authorized_to_update' => true, 'authorized_to_delete' => true, 'authorized_to_restore' => true, - 'authorized_to_force_delete' => true, + 'authorized_to_force_delete' => true ], 'belongs_to_many_relation' => $matchingModel->belongsToManyRelation() ->orderBy('id', 'desc') From 66ff0c684a931c95d8b4910d4f5c0600cd5bf45c Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 9 Jun 2025 11:18:11 +0200 Subject: [PATCH 10/10] Fix styleCI --- tests/Feature/Controllers/AutomaticGatingTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/Controllers/AutomaticGatingTest.php b/tests/Feature/Controllers/AutomaticGatingTest.php index cdb947b..561723c 100644 --- a/tests/Feature/Controllers/AutomaticGatingTest.php +++ b/tests/Feature/Controllers/AutomaticGatingTest.php @@ -335,7 +335,7 @@ public function test_searching_automatic_gated_resource_with_belongs_to_many_rel 'authorized_to_update' => true, 'authorized_to_delete' => true, 'authorized_to_restore' => true, - 'authorized_to_force_delete' => true + 'authorized_to_force_delete' => true, ], 'belongs_to_many_relation' => $matchingModel->belongsToManyRelation() ->orderBy('id', 'desc')