Skip to content
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
13 changes: 6 additions & 7 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ public function resource(Resource $resource)

protected function buildGatesForModel(Model $model, Resource $resource, array $gates)
{
return array_merge(
in_array('view', $gates) ? [config('rest.gates.names.authorized_to_view') => $resource->authorizedTo('view', $model)] : [],
in_array('update', $gates) ? [config('rest.gates.names.authorized_to_update') => $resource->authorizedTo('update', $model)] : [],
in_array('delete', $gates) ? [config('rest.gates.names.authorized_to_delete') => $resource->authorizedTo('delete', $model)] : [],
in_array('restore', $gates) ? [config('rest.gates.names.authorized_to_restore') => $resource->authorizedTo('restore', $model)] : [],
in_array('forceDelete', $gates) ? [config('rest.gates.names.authorized_to_force_delete') => $resource->authorizedTo('forceDelete', $model)] : [],
$filteredGates = array_filter($gates, fn ($gate) => $gate !== 'create');

return array_combine(
array_map(fn ($gate) => config('rest.gates.names.authorized_to_'.Str::snake($gate), 'authorized_to_'.Str::snake($gate)), $filteredGates),
array_map(fn ($gate) => $resource->authorizedTo($gate, $model), $filteredGates)
);
}

Expand All @@ -53,7 +52,7 @@ protected function buildGatesForModel(Model $model, Resource $resource, array $g
*
* @return array The structured array representation of the model, including attributes and recursively processed relations.
*/
public function modelToResponse(Model $model, Resource $resource, array $requestArray, Relation $relation = null)
public function modelToResponse(Model $model, Resource $resource, array $requestArray, ?Relation $relation = null)
{
$currentRequestArray = $relation === null ? $requestArray : collect($requestArray['includes'] ?? [])
->first(function ($include) use ($relation) {
Expand Down
7 changes: 6 additions & 1 deletion src/Rules/SearchRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;
use Lomkit\Rest\Http\Requests\RestRequest;
Expand Down Expand Up @@ -68,6 +69,10 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
$attribute .= '.';
}

$policy = Gate::getPolicyFor($this->resource::$model);
$validGates = $policy ? get_class_methods($policy) : [];
$validGates = array_filter($validGates, fn ($method) => !in_array($method, ['before', 'after', '__call', '__construct']));

$this
->validator
->setRules(
Expand All @@ -89,7 +94,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
[
$attribute.'limit' => ['sometimes', 'integer', Rule::in($this->resource->getLimits($this->request))],
$attribute.'page' => ['sometimes', 'integer'],
$attribute.'gates' => ['sometimes', 'array', Rule::in(['viewAny', 'view', 'create', 'update', 'delete', 'restore', 'forceDelete'])],
$attribute.'gates' => ['sometimes', 'array', Rule::in($validGates)],
],
$this->isRootSearchRules ? [$attribute.'includes' => ['sometimes', 'array']] : [],
$this->isRootSearchRules ? $this->includesRules($this->resource, $attribute.'includes') : [],
Expand Down