Skip to content

fix: support parameterless policy methods for guest access#727

Merged
binaryk merged 1 commit into
10.xfrom
fix/parameterless-policy-guest-access
Apr 9, 2026
Merged

fix: support parameterless policy methods for guest access#727
binaryk merged 1 commit into
10.xfrom
fix/parameterless-policy-guest-access

Conversation

@binaryk
Copy link
Copy Markdown
Collaborator

@binaryk binaryk commented Apr 9, 2026

Problem

Laravel's Gate denies guest access to a policy method unless that method declares a nullable $user as its first parameter. Gate::policyAllowsGuests() inspects the method signature and returns false when the parameter is absent, so even a policy like:

public function allowRestify(): bool
{
    return true;
}

would return a 403 for unauthenticated requests — despite explicitly returning true.

Fix

AuthorizableModels now has a checkPolicyMethod() helper that uses ReflectionMethod to detect when a policy method has zero parameters. In that case it calls the method directly, bypassing Gate's guest-check logic entirely. When the method does have parameters the existing Gate::check() path is used unchanged.

The helper is used in the four existing call sites: authorizedToUseRepository, authorizedToStore, authorizedToStoreBulk, and authorizedTo.

Tests

tests/Feature/ParameterlessPolicyTest.php covers three scenarios using a ParameterlessAllowPolicy whose allowRestify() has no parameters:

  • Unauthenticated request is allowed when the parameterless policy returns true (the key regression case — previously returned 403)
  • Unauthenticated request is denied when the parameterless policy returns false
  • Authenticated request is allowed when the parameterless policy returns true

Test plan

  • ./vendor/bin/phpunit tests/Feature/ParameterlessPolicyTest.php — 3 tests pass
  • ./vendor/bin/phpunit tests/Feature/AuthorizableModelsTest.php — 4 existing tests still pass
  • Verify that a policy with a parameterless allowRestify(): bool { return true; } no longer returns 403 for guest requests

Laravel's Gate denies guest access when a policy method has zero
parameters because policyAllowsGuests() requires a nullable \$user
first param to opt in.  Add checkPolicyMethod() to AuthorizableModels
which uses ReflectionMethod to detect a zero-parameter policy method
and calls it directly, bypassing Gate's guest-check logic.

Add ParameterlessPolicyTest covering the unauthenticated-allowed,
unauthenticated-denied, and authenticated-allowed scenarios.
@binaryk binaryk merged commit 46fc32b into 10.x Apr 9, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant