Skip to content

Allow callable for requireAuthorizationCheck config option#322

Open
dereuromark wants to merge 1 commit into3.xfrom
feature/callable-require-authorization-check
Open

Allow callable for requireAuthorizationCheck config option#322
dereuromark wants to merge 1 commit into3.xfrom
feature/callable-require-authorization-check

Conversation

@dereuromark
Copy link
Member

Summary

This PR adds support for a callable in the requireAuthorizationCheck middleware configuration option. Currently, this option only accepts a boolean value, but applications often need to conditionally skip authorization checks based on the request path or other request attributes.

Use Case

When integrating third-party plugins that provide their own admin panels (e.g., queue management dashboards), these plugins may manage authorization independently. The host application needs a way to skip the middleware's authorization check for these specific routes without disabling it globally.

Before this PR, applications had to wrap the middleware in a custom closure:

$middlewareQueue->add(function ($request, $handler) use ($app) {
    $path = $request->getUri()->getPath();
    $skipAuthCheck = str_contains($path, '/admin/queue');

    $middleware = new AuthorizationMiddleware($app, [
        'requireAuthorizationCheck' => !$skipAuthCheck,
    ]);

    return $middleware->process($request, $handler);
});

After this PR, the same can be achieved cleanly:

$middlewareQueue->add(new AuthorizationMiddleware($this, [
    'requireAuthorizationCheck' => function ($request) {
        $path = $request->getUri()->getPath();
        if (str_contains($path, '/admin/queue')) {
            return false;
        }
        return true;
    }
]));

Changes

  • Modified AuthorizationMiddleware::process() to check if requireAuthorizationCheck is callable and invoke it with the request
  • Updated docblock to document the callable signature
  • Added 3 test cases covering:
    • Callable returning true (requires authorization check)
    • Callable returning false (skips authorization check)
    • Route-based logic example
  • Updated English documentation with example usage

Backwards Compatibility

This change is fully backwards compatible. The existing boolean behavior is preserved - the callable is only invoked if the config value is callable.

This allows applications to conditionally skip the authorization check
based on the request. This is useful when integrating plugins that have
their own admin panels and manage authorization independently (e.g.,
queue management dashboards).

The callable receives the ServerRequestInterface and should return a
boolean - true to require authorization check (default), false to skip.

Example use case:
```php
$middlewareQueue->add(new AuthorizationMiddleware($this, [
    'requireAuthorizationCheck' => function ($request) {
        $path = $request->getUri()->getPath();
        if (str_contains($path, '/admin/queue')) {
            return false;
        }
        return true;
    }
]));
```
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