tkhamez/slim-role-auth

Slim 框架的角色授权

5.0.0 2024-06-01 17:45 UTC

This package is auto-updated.

Last update: 2024-09-17 19:17:10 UTC


README

build Test Coverage Packagist Downloads

基于角色的授权

Slim 4 框架的中间件。

对于 Slim 3,请使用 1.0.0 版本。

安装

使用 Composer

composer require tkhamez/slim-role-auth

用法

示例

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, first "starts-with" match is used.
        '/secured/public' => ['any'],
        '/secured'        => ['user'],
    ],
    ['redirect_url' => null] // Adds "Location" header instead of 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();
  • SecureRouteMiddleware 中间件如果请求对象中缺少所需的角色,将拒绝访问路由。
  • RoleMiddleware 类将 RoleProvider 对象提供的角色添加到请求对象中。
  • 您可以为不同的路径添加多个角色提供者。

有关更多信息,请参阅类的内联文档。

开发环境

docker build --tag slim-role-auth .
docker run -it --mount type=bind,source="$(pwd)",target=/app --workdir /app slim-role-auth /bin/sh

变更日志

5.0.0 - 2024-06-01

  • 将最低所需 PHP 版本提升至 7.4。

4.0.0

  • 将最低所需 PHP 版本提升至 7.3。

3.0.1

  • 更新 PHP 要求以包含版本 8 (^7.2|^8.0)。

3.0.0

  • 将最低 PHP 版本提升至 7.2
  • 添加了一个类常量,用于存储角色的请求属性名称,并更改了其名称。

2.0.1

  • 与 Slim 4.4 的兼容性

2.0.0

  • 针对 Slim 4 的更新。

1.0.0

  • 第一个稳定版本。