控制器级权限

PHP*_*Pst 2 acl phalcon

考虑以下代码:

$acl = new Phalcon\Acl\Adapter\Memory();

$acl->setDefaultAction(Phalcon\Acl::DENY);

//Register roles
$acl->addRole(new Phalcon\Acl\Role('guest'));
$acl->addRole(new Phalcon\Acl\Role('member', 'guest'));
$acl->addRole(new Phalcon\Acl\Role('admin', 'user'));

$acl->addResource(new Phalcon\Acl\Resource('user'));
$acl->addResource(new Phalcon\Acl\Resource('index'));

$acl->allow('member', 'user', array());
$acl->allow('guest', 'index', array());

$acl->isAllowed('guest','index','index'); //returns false
Run Code Online (Sandbox Code Playgroud)

我想在控制器级别获得授权许可,我的意思是写:$ acl-> allow('member','user'); 它授予用户控制器动作成员,我不想手动添加用户控制器的所有动作.我该如何实现这个想法?

twi*_*tra 6

在名为INVO的示例应用程序中实现Acl有一个很好的示例,Security插件实现了Acl列表,有关公共区域的资源使用'*'作为通配符为控制器中的每个操作添加规则:

<?php

$publicResources = array(
    'index' => array('index'),
    'about' => array('index'),
    'session' => array('index', 'register', 'start', 'end'),
    'contact' => array('index', 'send')
);
foreach($publicResources as $resource => $actions){
    $acl->addResource(new Phalcon\Acl\Resource($resource), $actions);
}

//Grant access to public areas to both users and guests
foreach($roles as $role){
    foreach($publicResources as $resource => $actions){
        $acl->allow($role->getName(), $resource, '*');
    }
}
Run Code Online (Sandbox Code Playgroud)