Zend framework2 ZF2-动态角色方法中的ACL资源

Zend framework2 ZF2-动态角色方法中的ACL资源,zend-framework2,Zend Framework2,给定一个具有静态权限的系统,如posts.create、category.edit等,以及可以在运行时创建的角色,两者都存储在数据库中,但权限不能(技术上不应该)修改;对于用户到角色以及角色到权限的关系为N:M: 查看ACL包,乍一看,我必须通过在每个请求上查询我的数据库角色并将它们添加到ACL实例和允许的权限来构建ACL图,如: // Some class like AclService.php that should be called in Module.php // ... $role

给定一个具有静态权限的系统,如
posts.create
category.edit
等,以及可以在运行时创建的角色,两者都存储在数据库中,但权限不能(技术上不应该)修改;对于
用户
角色
以及
角色
权限
的关系为
N:M

查看ACL包,乍一看,我必须通过在每个请求上查询我的数据库角色并将它们添加到ACL实例和允许的权限来构建ACL图,如:

// Some class like AclService.php that should be called in Module.php
// ...

$roles = // query db for all roles and their permissions

foreach ($roles as $role) {
    $acl->addRole($role->getName());

    foreach ($role->getPermissions() as $permission) {
        $acl->allow($role->getName(), null, $permission->getName());
    }
}
到目前为止,在我的控制器操作(或中间件,如果它们存在的话)中,我会检查是否允许用户执行操作:

// CategoryController
public function createAction() {
    $user = // retrieve user from identity

    if (! $acl->isAllowed($user->getRoles()->first(), null, 'categories.create')) {
        // throw a 403 exception
    }
}
我还不太明白的是,在这个模式中,
资源在哪里?我是不是遗漏了什么


或者,当权限不像
类别那样精细时,资源是否适合使用。创建
而只是
创建

您正在使用
$acl->addRole($role->getName())但根据文档说明,它应该在参考资料中定义

use Zend\Permissions\Acl\Acl;
use Zend\Permissions\Acl\Role\GenericRole as Role;
use Zend\Permissions\Acl\Resource\GenericResource as Resource;

$acl = new Acl();

$acl->addRole(new Role('guest'))
    ->addRole(new Role('member'))
    ->addRole(new Role('admin'));

$parents = array('guest', 'member', 'admin');
$acl->addRole(new Role('someUser'), $parents);

$acl->addResource(new Resource('someResource'));

$acl->deny('guest', 'someResource');
$acl->allow('member', 'someResource');

echo $acl->isAllowed('someUser', 'someResource') ? 'allowed' : 'denied';

如果这没有帮助,请告诉我我会尽力帮助。

categories.create是资源。。这就像说用户是否有权访问category controller上的create操作。它几乎可以将任何东西表示为字符串。