ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 学习笔记二 不同的roles,resource 和 access

学习笔记二 不同的roles,resource 和 access

原创 Linux操作系统 作者:寻找迭戈 时间:2010-05-14 21:18:44 0 删除 编辑
ACL学习笔记
网站在大多数情况下使用不同的resource: 网站可能有主页,留言板,和管理员面板resource。 也可能有一些files甚至有一台咖啡机这些都可以称为resource。
上一章中我们定义了一个静态的role但是在以上这种情况下,显然这种方法已经不适用了。这一章将要详细介绍这些知识。
1. 处理单独的用户和用户组
现在我们想要限定一个用户或者一个组访问特别的resource,怎样实现?
既然每一个Zend_acl的role被定义为一个string,那么我这就涉及到了role-scheme。这里对于用户role-id是用户名,对于组来说role-id是组名。
$acl->addRole(new Zend_Acl_Role('user-john'));
$acl->addRole(new Zend_Acl_Role('group-accounting'));

检查这个acl的话代码如下
//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.
}
}
2.使用Zend_Acl_Role_Interface
在上个例子中我们遇到了一个小问题String。
我们使用user->something, group->something的关系。但是我们想要在其他地方也使用这些呢?有些人可能选择复制粘贴代码,这就增加了代码的复杂度,而且对今后的升级和维护增加了困难。
幸运的是Zend为我们提供了一个函数Zend_Acl_Role_Interface解决这个问题。
同样是上个例子。我们能够让他继承这个接口,使用这个方法能够非常方便的把实例转化为ACL,而不用先把他们转化为String。
以下示例:
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;
}
}
现在我们有了一个getRoleId的方法, 这个方法返回了一个实例的识别符,同样我们在我们的Croupclass中也加入代码,不过返回值是groupId。上面的检查ACl的代码现在变成这个样子:
//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) {
//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');
}
}
注意:当建立ACL时必须使用Resource_Controller和User类

//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/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-05-14

  • 博文量
    4
  • 访问量
    5095