diff --git a/src/Query/Traits/PerformMutation.php b/src/Query/Traits/PerformMutation.php index 575321a..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 { @@ -33,9 +34,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; @@ -64,7 +63,12 @@ public function applyMutation(array $mutation = [], $attributes = []) } elseif ($mutation['operation'] === 'update') { $this->resource->authorizeTo('update', $model); } else { - $this->resource->authorizeTo('view', $model); + $attachModel = (new ReflectionClass($model))->getShortName(); + if (!$this->resource->authorizedTo('attach'.$attachModel, $model)) { + $this->resource->authorizeTo('view', $model); + } else { + $this->resource->authorizeTo('attach'.$attachModel, $model); + } } return $this->mutateModel( diff --git a/tests/Feature/Controllers/AutomaticGatingTest.php b/tests/Feature/Controllers/AutomaticGatingTest.php index 994598a..561723c 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,7 +331,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, @@ -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, 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; + } }