diff --git a/src/Http/Response.php b/src/Http/Response.php index cef1aea..40f6f2b 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -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) ); } @@ -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) { diff --git a/src/Rules/SearchRules.php b/src/Rules/SearchRules.php index d1033af..f1a777a 100644 --- a/src/Rules/SearchRules.php +++ b/src/Rules/SearchRules.php @@ -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; @@ -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( @@ -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') : [],