skibish / simple-rest-acl
最简单的REST ACL,至今。
Requires
- php: >=5.5.9
- nikic/fast-route: ^1.0
- psr/log: ^1.0
- symfony/yaml: ^3.1
Requires (Dev)
- phpunit/phpunit: 4.6.*
- satooshi/php-coveralls: dev-master
This package is not auto-updated.
Last update: 2024-09-28 17:02:11 UTC
README
最简单的ACL构建,史上。
如何安装
运行 composer require skibish/simple-rest-acl
想法和动机
尽可能简单通过路由和方法配置ACL。
如何使用它
首先,您需要创建 acl.yml
文件。它可以看起来像这样
/users: roles: ['role1' ,'role2' ,'role3'] GET: ['role1' ,'role2'] POST: all PUT: none
在您的代码中,您可以这样开始ACL
$availableListOfRolesToUser = ['role1' ,'role2' ,'role3']; $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = $_SERVER['REQUEST_URI']; $acl = new \Skibish\SimpleRestAcl\ACL(__DIR__ . '/config/acl.yml', new \Skibish\SimpleRestAcl\Validator($availableListOfRolesToUser)); $acl->got($httpMethod, $uri)->verify();
现在您就可以开始了!
acl.yml 文件配置
文件有以下可能性
/users: # route as for resource or regex (see explanation below). roles: # array of roles available for current resource or 'public' string (mandatory). type: # 'resource' (default, see explanation below) or 'strict' string. If strict is set - only if route is matched it will check methods. GET: ['role1' ,'role2'] # method and array of roles that can access it POST: all # or string, that all roles defined in 'roles' will apply for current method PUT: none # or this route is not accessible with any role by current method DELETE: ['role3']
最好的理解方式是使用示例。
示例
示例 #1
假设,我们有以下配置
/photos: roles: ['role1' ,'role2' ,'role3'] GET: ['role1' ,'role2'] POST: all PUT: none
并且我们有请求 GET /photos/12
和可用的角色是 role1
。它将匹配,因为默认 type
是 resource
。如果 type
是 resource
它将匹配以下路由
- /photos
- /photos/new
- /photos/{id}
- /photos/{id}/edit
- /photos/{id}
并且在 GET
中我们指定了两个角色的数组 role1
和 role2
。可用的角色是 role1
并且它在数组中。所以,在这种情况下,方法 verify()
将返回 true
。
示例 #2
假设,我们有以下配置
/strict/{route:\d+}: type: strict roles: [1, 2, 3] GET: [1]
在幕后,ACL 使用 nikic/FastRoute 来匹配路由。因此,您可以在路由定义中使用正则表达式。但在这个情况下 不要忘记 将 type
设置为 strict
。
在这种情况下,只有具有 /strict
部分后跟数字的路由将匹配。如果我们传递 GET /strict/foo
,方法 verify()
将返回 false
。如果我们传递 GET /strict/42
- 它将是 true
。
选项
ACL
构造函数的第三个参数是选项数组。目前有两个选项
cacheFile
- 缓存文件的路径。例如:__DIR__.'/cache/acl-cache.php'
。此配置将缓存您的配置。如果需要更新缓存,只需删除缓存文件。resourceRegex
-type
resource
的正则表达式。默认正则表达式是[/{id:\d+|new}[/edit]]
。如果您想让它匹配 RESTful Resource Controllers,例如,用[/{id:\d+|create}[/edit]]
覆盖此选项。
代码片段
$availableListOfRolesToUser = ['role1' ,'role2' ,'role3']; $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = $_SERVER['REQUEST_URI']; $acl = new \Skibish\SimpleRestAcl\ACL(__DIR__ . '/config/acl.yml', new \Skibish\SimpleRestAcl\Validator($availableListOfRolesToUser, [ 'cacheFile' => __DIR__ . '/cache/acl-cache.php', 'resourceRegex' => '[/{id:\d+|create}[/edit]]', ])); $acl->got($httpMethod, $uri)->verify();
日志记录
如果您需要从这个库中记录某些内容,您可以使用 PSR-3
兼容的日志记录器。
$availableListOfRolesToUser = ['role1' ,'role2' ,'role3']; $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = $_SERVER['REQUEST_URI']; $acl = new \Skibish\SimpleRestAcl\ACL(__DIR__ . '/config/acl.yml', new \Skibish\SimpleRestAcl\Validator($availableListOfRolesToUser)); $acl->setLogger(new Logger()); $acl->got($httpMethod, $uri)->verify();
缺少的角色
如果您需要知道缺少的角色是什么,请使用 $acl->getMissingRoles()
。它将返回缺少角色的数组。
贡献
如果您看到,有什么可以改进的,请随时提交拉取请求。