午夜 / 权限
简单的权限系统
2.0.0
2021-06-13 12:15 UTC
Requires
- php: ^7.4 || ^8.0
- psr/container: ^1.1 || ^2.0
Requires (Dev)
- eventjet/coding-standard: ^3.2
- infection/infection: ^0.22.1
- maglnet/composer-require-checker: ^3.2
- phpstan/extension-installer: ^1.0
- phpstan/phpstan: ^0.12.32
- phpstan/phpstan-phpunit: ^0.12.16
- phpstan/phpstan-strict-rules: ^0.12.2
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2024-09-14 20:40:46 UTC
README
午夜/权限
ACL 和 RBAC 并不是我想要的权限模型。所以我写了自己的。
安装
使用Composer安装 midnight/permissions
。
用法
你需要一个容器,可以为你的 PermissionService
提供权限。在这个例子中,我们将使用 league/container
,但任何实现 Psr\Container\ContainerInterface
的容器都可以工作。
为了简洁,用户和资源在这个例子中是数组。在现实世界的项目中,它们很可能是对象。
class CanDoSomething implements Midnight\Permissions\PermissionInterface { public function isAllowed($user = null, $resource = null): bool { return $user === $resource['owner']; } } $container = new League\Container\Container(); $container->add('can_do_something', CanDoSomething::class); $permissionService = new Midnight\Permissions\PermissionService($container); $permissionService->isAllowed('Rudolph', 'can_do_something', ['owner' => 'Rudolph']); // true $permissionService->isAllowed('Rudolph', 'can_do_something', ['owner' => 'Christoph']); // false
Laminas 模块
还有一个 Laminas 模块,允许你通过 Service Manager 访问 PermissionService
,通过配置添加权限,并为你提供视图助手和控制器插件。它被称为 midnight/permissions-module。