tkhamez / slim-role-auth
Role-based authorization for the Slim framework
Installs: 9 873
Dependents: 0
Suggesters: 0
Security: 0
Stars: 14
Watchers: 2
Forks: 2
Open Issues: 0
pkg:composer/tkhamez/slim-role-auth
Requires
- php: ^8.1
- psr/http-factory: ^1.0
- psr/http-message: ^1.0 || ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- slim/slim: ^4.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^9.5
- slim/psr7: ^1.4
Suggests
- ext-xdebug: Allows code coverage analysis with PHPUnit
README
Role-based authorization
Middleware for the Slim 4 framework.
For Slim 3 use the 1.0.0 release.
Installation
With Composer:
composer require tkhamez/slim-role-auth
Usage
Example:
use Tkhamez\Slim\RoleAuth\RoleMiddleware; use Tkhamez\Slim\RoleAuth\SecureRouteMiddleware; $app = Slim\Factory\AppFactory::create(); // Deny access if a required role is missing. $app->add(new SecureRouteMiddleware( new Slim\Psr7\Factory\ResponseFactory(), // Any implementation of Psr\Http\Message\ResponseFactoryInterface. [ // Route pattern => Roles, the first "starts-with" match is used. '/secured/public' => ['any'], '/secured' => ['user'], ], ['redirect_url' => null] // Adds the "Location" header instead of a 403 status code if set. )); // Add roles to request attribute. $app->add(new RoleMiddleware( new App\RoleProvider(), // Any implementation of Tkhamez\Slim\RoleAuth\RoleProviderInterface. ['route_pattern' => ['/secured']] // Optionally limit to these routes. )); // Add routing middleware last, so the Slim router is available from the request. $app->addRoutingMiddleware();
- The
SecureRouteMiddlewaredenies access to a route if the required role is missing in the request object. - The
RoleMiddlewareclass adds roles provided by theRoleProviderobject to the request object. - You can add multiple role providers for different paths.
For more information, see the inline documentation of the classes.
Dev Env
docker build --tag slim-role-auth . docker run -it --mount type=bind,source="$(pwd)",target=/app --workdir /app slim-role-auth /bin/sh