nybbl / access-acl
提供了一个简单的zf3权限控制实现。
Requires
- php: >=7.0
- zendframework/zend-authentication: ^2.6
- zendframework/zend-mvc: ^3.1@dev
- zendframework/zend-permissions-acl: ^2.7
- zendframework/zend-servicemanager: ^3.3
- zendframework/zend-session: ^2.7.1
Suggests
- doctrine/orm: ^2.5.14, to use DoctrineRoleProvider
This package is auto-updated.
Last update: 2024-09-05 04:10:04 UTC
README
为ZF3提供了一个易于使用的权限控制实现。提供对Doctrine的原生支持。
安装
$ composer require nybbl/access-acl
使用
要使用此模块,将其添加到您的 modules.config.php 文件中
return [ ... 'Nybbl\AccessAcl', ];
可选配置
如果您想进一步配置该模块,请将此包的 config/module.config.php 文件内容复制到 config/autoload/nybbl.access.acl.config.php 或您的 config/autoload/global.php 文件中。
'access_manager' => [ 'redirect_route_name' => 'application.home', 'default_access_all_role' => 'Guest', ],
配置键描述
-
redirect_route_name: 应用程序应重定向到的路由名称。例如,您可能希望未授权用户重定向到 "user.login"。
-
default_access_all_role: 默认角色。如果在 AuthenticationService 中没有身份,则默认角色为 "Guest"。
映射资源
ACL的核心是一个资源。要映射您的资源(即控制器),您可以在模块配置中指定一个数组键。
Application/config/module.config.php
'controllers' => [ 'factories' => [ Controller\ApplicationController::class => InvokableFactory::class, ], ], ## This is where you can specify your resources 'access_manager' => [ 'resources' => [ Controller\ApplicationController::class => [ [ 'allow' => 'Guest', 'actions' => ['index'], ], [ 'allow' => 'Admin', 'actions' => ['home', 'users', 'posts'], ], ], ], ],
自定义未授权视图
默认情况下,"未授权"视图仅渲染一些文本。您很可能想自定义它。您可以在任何模块中创建一个视图目录,路径为
access-acl/not-authorised/index.twig
更改角色提供者
要更改角色提供者,您需要一个实现了 RoleProviderInterface 的类。然后,在模块配置中添加到接口的别名
'service_manager' => [ 'aliases' => [ Nybbl\AccessAcl\Contract\RoleProviderInterface::class => MyCustomRoleProvider::class, ], ],
创建角色
要创建一个角色,您需要一个实现了 Nybbl\AccessAcl\Contract\RoleInterface 的类
如下所示
class AdminRole implements RoleInterface { // Implement body methods. }
然后,您的自定义角色提供者可以消费这些角色。
创建动态断言
use Nybbl\AccessAcl\Contract\DynamicAssertionInterface; class ExampleAssertion implements DynamicAssertionInterface { /** * @param string $resource * @param null $privilege * @param array $options * @return bool|mixed */ public function assert(string $resource, $privilege = null, array $options = []) { // Implement yor logic based on the result... if ($options['can.edit']) { if ($options['identity']->id() === $options['blogPost']->ownerId()) { return self:ACCESS_GRANTED; } } } }
在您的控制器中
public function editAction() { $this->assert(ExampleAssertion::class, 'index', 'can.edit', [ 'identity' => $this->identity(), 'blogPost' => $blogPostEntity, ]); return new ViewModel(); }
默认情况下,动态断言将返回 false。