Fix role model guard filtering in hasRole checks

Merged
SE
2d ago
+22-8

Fixes #2977

Reproduction

When a user has an assigned Role model on the web guard, hasRole($role, 'admin') and hasAllRoles($role, 'admin') returned true. The equivalent name and key checks returned false.

Change

  • Apply the optional guard filter before matching a Role model in both methods.
  • Add a regression test for direct Role input and the hasRole([$role], $guard) path.

Changed files:

  • src/Traits/HasRoles.php
  • tests/Traits/HasRolesTest.php

Verification

  • The regression test failed before the fix (true received where false was expected).
  • HasRolesTest.php and TeamHasRolesTest.php: 133 passed, 430 assertions, in the isolated PHP 8.5.11 container. PHPUnit reported one deprecation for the --do-not-cache-result option.
  • git diff --check passed.
  • Project patch policy accepted the 2-file, 14-line text-only patch.