Skip to content

Change gate for attach mutation #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Query/Traits/PerformMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/Controllers/AutomaticGatingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
115 changes: 114 additions & 1 deletion tests/Support/Policies/GreenPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function viewAny($user)
*/
public function view($user, Model $model)
{
return true;
return false;
}

/**
Expand Down Expand Up @@ -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;
}
}