php zend_acl权限管理

1. 误区

许多人认为ACL的“resource” ,“privilege” 和controller,action相似。这是不正确的。

在Zend中resource可以是任何事物:一个controller,一个file和一个mondel……

 Privilige也一样,当resource是一个controller时privilege就是一个action,当resource是个model或file时privilige就是“write和read”。

在今后的学习中会接触到这些用法。


2. 创建一个简单的ACL

刚刚我们提到Zend_acl由resource,privilige和新加的role构成。

resource可以被我们认为是controller,files或其他。privilige定义了访问这些resource的等级。role决定了谁可以用privilige去访问

resource,role可以是一个用户或者一个组或者是任何你想定义的东西。

这里介绍最简单的使用方法。这种方法适用于一个simple cases

<span style="font-size:18px;color:#666666;">class My_Acl extends Zend_Acl {
  public function __construct() {
    //Add a new role called "guest"
    $this->addRole(new Zend_Acl_Role('guest'));
 
    //Add a role called user, which inherits from guest
    $this->addRole(new Zend_Acl_Role('user'), 'guest');
 
    //Add a resource called page
    $this->add(new Zend_Acl_Resource('page'));
 
    //Add a resource called news, which inherits page
    $this->add(new Zend_Acl_Resource('news'), 'page');
 
    //Finally, we want to allow guests to view pages
    $this->allow('guest', 'page', 'view');
 
    //and users can comment news
    $this->allow('user', 'news', 'comment');
  }
}</span>



现在我们就能通过实例化这个类使用acl了。 

3.使用简单的ACL

使用ACL非常简单,仅仅需要使用inAllowed方法加一个role和resource和一个权限。唯一麻烦的是定义role,resource和privilege。我们怎样定义role呢?其实非常简单:使用if-clause,如果是登陆的用户那么role就是用户,其余的是游客那么resource呢?通过这个例子,我们定义了resource是文件,在这里我们可以建立一个page.php文件和一个news.php文件page检查page resource,news 可以检查news resource。权限,当load页面是你能够使用view权限,代码能够查看其他的privilege,比如是否有comment权限使用comment box。

<span style="font-size:18px;color:#666666;">$role = 'guest';
if(isset($_SESSION['auth']))
  $role = 'user';
 
$acl = new My_Acl();
 
if($acl->isAllowed($role, 'news', 'comment')) {
  //Some code here to display a news box
}</span>

这个例子用于普通的php页面,下面部分是介绍在ZF中使用acl

4. 在ZF中使用

一般情况下我们需要建立一个小的插件用来检查acl
在library/My/Controller/Plugin/文件夹下建立Acl.php
<span style="font-size:18px;color:#666666;">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');
    }
  }
}</span>

我们建立了一个静态的ACL, 现在我们把它加入到bootstrap里
<span style="font-size:18px;color:#666666;">$acl = new My_Acl();
 
//assuming $fc is the front controller
$fc->registerPlugin(new My_Plugin_Acl($acl));</span>

OK 现在每个request都要通过acl认证,如果过没有view权限将不能访问。
当我们想为某个特别的页面加入权限时,应当在action里加入
<span style="font-size:18px;color:#666666;">public function someAction() {
  $role = (Zend_Auth::getInstance()->hasIdentity())
        ? 'user'
        : 'guest';
 
  //assuming $this->_acl contains the acl
  $this->view->canComment = $this->_acl->isAllowed($role, 'news', 'comment');
}</span>

想一下当你把这段代码加入到auth认证里会很方便。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值