Skip to content

Commit 1404246

Browse files
authored
feat(auth): add class-level permission support (#1405)
1 parent 978bba2 commit 1404246

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

packages/auth/src/Allow.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Attribute;
88
use UnitEnum;
99

10-
#[Attribute(Attribute::TARGET_METHOD)]
10+
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
1111
final readonly class Allow
1212
{
1313
public function __construct(

packages/auth/src/AuthorizerMiddleware.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ public function __construct(
2424

2525
public function __invoke(Request $request, HttpMiddlewareCallable $next): Response
2626
{
27-
$attribute = $this->matchedRoute
27+
$handler = $this->matchedRoute
2828
->route
29-
->handler
30-
->getAttribute(Allow::class);
29+
->handler;
30+
31+
$attribute = $handler->getAttribute(Allow::class) ?? $handler->getDeclaringClass()->getAttribute(Allow::class);
3132

3233
if ($attribute === null) {
3334
return $next($request);

tests/Fixtures/Controllers/AdminController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
use Tests\Tempest\Integration\Auth\Fixtures\CustomAuthorizer;
1212
use Tests\Tempest\Integration\Auth\Fixtures\UserPermissionUnitEnum;
1313

14+
#[Allow(UserPermissionUnitEnum::ADMIN)]
1415
final readonly class AdminController
1516
{
16-
#[Allow(UserPermissionUnitEnum::ADMIN)]
1717
#[Get('/admin')]
1818
public function admin(): Response
1919
{

0 commit comments

Comments
 (0)