$acl->addRole(new Zend_Acl_Role('user-john'));检查这个acl的话代码如下
$acl->addRole(new Zend_Acl_Role('group-accounting'));
//We'll assume $user is some kind of a User object
$name = $user->getName();
$groups = $user->getGroups();
$roles = array('user-' . $name);
foreach($groups as $g)
$roles[] = 'group-' . $g->getName();
foreach($roles as $r) {
if($acl->isAllowed($r, 'someresource', 'someprivilege') {
//allowed, do something.
}
}
class User implements Zend_Acl_Role_Interface {
//contents of the class here
//this method is for the role interface:
public function getRoleId() {
return 'user-' . $this->_name;
}
}
//We simply put the user and the groups in an array and be done with it
$roles = array($user) + $user->getGroups();
foreach($roles as $r) {
if($acl->isAllowed($r, 'someresource', 'someprivilege') {
//allowed, do something.
}
}
3.不同的resource类型
和role一样resource也是string,同样也有一个接口去转化。Zend_Acl_Resource_Interface
在第一份笔记中的那个小例子
class My_Plugin_Acl extends Zend_Controller_Plugin_Abstract {
private $_acl = null;
public function __construct(Zend_Acl $acl) {
$this->_acl = $acl;
}
public function preDispatch(Zend_Controller_Request_Abstract $request) {
//As in the earlier example, authed users will have the role user
$role = (Zend_Auth::getInstance()->hasIdentity())
? 'user'
: 'guest';
//For this example, we will use the controller as the resource:
$resource = $request->getControllerName();
if(!$this->_acl->isAllowed($role, $resource, 'view')) {
//If the user has no access we send him elsewhere by changing the request
$request->setModuleName('auth')
->setControllerName('auth')
->setActionName('login');
}
}
}
建立我们的resource
首先我们需要为我们的controller建立一个resource type。
class Resource_Controller implements Zend_Acl_Resource_Interface {
public function __construct($id) {
$this->_id = $id;
}
public function getResourceId() {
return 'controller-' . $this->_id;
}
我们为controller建立了resource类。$_id 是这个controller resource 的名字
现在假设你使用的是一个file model类。
class File implements Zend_Acl_Resource_Interface {
/* we can have some methods related to the file model here */
public function getResourceId() {
return 'file-' . $this->_name;
}
}
这个File model的名字是$_name 用于resource id
修改我们的plugin
现在我们首先plugin去使用controller resource
public function preDispatch(Zend_Controller_Request_Abstract $request) {注意:当建立ACL时必须使用Resource_Controller和User类
//Let's assume that the identity is a User object this time
$user = Zend_Auth::getInstance()->getIdentity();
//We need to create a new controller resource using the name:
$resource = new Resource_Controller($request->getControllerName());
if(!$this->_acl->isAllowed($user, $resource, 'view')) {
//If the user has no access we send him elsewhere by changing the request
$request->setModuleName('auth')
->setControllerName('auth')
->setActionName('login');
}
}
//assume $someUser is an user object
$this->addRole($someUser);
//assume $someController is a Resource_Controller object
$this->add($someController);
$this->allow($someUser, $someController, 'view');
建立一个显得plugin
现在我们能够增加代码去检查files的访问,这里如果我们建立一个新的plugin,这样做能提高我们的代码重用性。
现在我们假设我们有一段代码用于user访问(使用)files。当一个user想去下载一个文件,这个请求会包含一些参数(比如说the file's name).
It is very simple plugin to the first
class My_Plugin_Acl_File extends Zend_Controller_Plugin_Abstract {
private $_acl = null;
public function __construct(Zend_Acl $acl) {
$this->_acl = $acl;
}
public function preDispatch(Zend_Controller_Request_Abstract $request) {
//This too will use the user object from zend_auth
$user = Zend_Auth::getInstance()->getIdentity();
//Instead of using the controllers name, we'll use the parameter
$filename = $request->getParam('file');
//If the request is not for a file we can just exit here without doing anything
if(empty($filename))
return;
$resource = new File();
$resource->setName($filename);
if(!$this->_acl->isAllowed($role, $resource, 'view')) {
$request->setModuleName('auth')
->setControllerName('auth')
->setActionName('login');
}
}
}
这段代码里大部分与以前的相同,但是我们把检查controller 换成了检查a file model
4.更多的关于allow 和deny访问
通过以上的学习,我们能够通过allow方法验证对ACLresource的访问,现在有一些扩展
当你allow一个访问到 null resource,但是赋予了一些privilege。这意味着role拥有了
访问所有resource的权限。
//myrole will get dosomething privilege on *all* resources
$acl->allow('myrole', null, 'dosomething');
与这个等价:
//myrole will get all privileges on someresource (privilege defaults to null)
$acl->allow('myrole', 'someresource');
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8225983/viewspace-662742/ ,如需转载,请注明出处,否则将追究法律责任。