Skip to content

refactor #52071

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

Closed
wants to merge 2 commits into from
Closed

refactor #52071

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
29 changes: 21 additions & 8 deletions src/Illuminate/Foundation/Auth/Access/AuthorizesRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,37 @@ protected function normalizeGuessedAbilityName($ability)
* @param \Illuminate\Http\Request|null $request
* @return void
*/

public function authorizeResource($model, $parameter = null, array $options = [], $request = null)
{
$model = is_array($model) ? implode(',', $model) : $model;

$parameter = is_array($parameter) ? implode(',', $parameter) : $parameter;

$parameter = $parameter ?: Str::snake(class_basename($model));

$middleware = [];
$request = $request ?: request();

// Getting the current action method from the request
$currentMethod = $request->route()->getActionMethod();

foreach ($this->resourceAbilityMap() as $method => $ability) {
$modelName = in_array($method, $this->resourceMethodsWithoutModels()) ? $model : $parameter;
// Determine if the current method should be processed based on 'only' or 'except' options
if (isset($options['only']) && !in_array($currentMethod, (array) $options['only'])) {
return; // Skip authorization if not in 'only' list
}

$middleware["can:{$ability},{$modelName}"][] = $method;
if (isset($options['except']) && in_array($currentMethod, (array) $options['except'])) {
return; // Skip authorization if in 'except' list
}

foreach ($middleware as $middlewareName => $methods) {
$this->middleware($middlewareName, $options)->only($methods);
// Get the ability corresponding to the current method
$ability = $this->resourceAbilityMap()[$currentMethod] ?? null;

// Check if there is an ability defined for the current method
if ($ability) {
// Decide the model or parameter to authorize against
$modelName = in_array($currentMethod, $this->resourceMethodsWithoutModels()) ? $model : array_values(array_intersect_key($request->route()->parameters, array_flip((array) $parameter)));

// Perform the authorization check
app(Gate::class)->authorize($ability, $modelName);
}
}

Expand Down
Loading