2012-10-30 43 views
2

考慮下面的代碼:控制器級權限

$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 

我想在控制器級別授予權限,我的意思是當寫: $ ACL的>允許(「部件」,「用戶」); 它授予用戶控制器的動作成員,我不想手動添加用戶控制器的所有動作。我怎樣才能實現這個想法?

回答

5

在示例應用程序中實現了一個很好的示例,名爲INVOSecurity插件實現了一個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, '*'); 
    } 
} 
+0

謝謝,在文檔中沒有關於'*'參數的信息,也似乎是需要每個動作都由'addResources'函數添加。它似乎增加添加'*'是多餘的,如果省略了第三個參數,它隱式意味着所有操作都被允許/拒絕。 ZF採用了這種方法:http://framework.zend.com/manual/2.0/en/modules/zend.permissions.acl.intro.html – PHPst

+0

如何授予特定角色的所有資源? '*'不適用於資源。 – PHPst