手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆

Zend_Acl and MVC Integration Part I (Basic Use)

首页 > PHP Framework >

原文地址:http://devzone.zend.com/article/3509-Zend_Acl-and-MVC-Integration-Part-I-Basic-Use

原文内容:

By Aldemar Bernal

So, what is wrong with Zend_Acl and the current MVC implementation in the Zend Framework? there is nothing wrong, it is just that it gets not too obvious for developers how to achieve an optimal integration between these two important parts of the framework.

First at all, this article is based on the following Zend Framework Proporsal (link), by this moment this proposal is in Pending Recommendation state.

Well, how it works? There are two key components in this proposal:

  1. A Front Controller Plugin: This component resolves if the current user has access to the page which is being opened.
  2. An Action Helper: This component allows you to check whether the current user has access inside a controller.

Based on these two components, let's try them with an example. Let's talk about a website like DevZone, we would need a controller that work with the user management and another one which will deal with article management, as well we need 3 types of users (roles), one for guests, one for writers and another one which will approve the articles; resuming, we have:

Resources:

  1. user controller.
  2. article controller.

Roles:

  1. Guest.
  2. Writer.
  3. Admin.

 

Setting up the Zend_Acl component

After defined what we want to do, the next step will create a Zend_Acl instance which will reflect our model.

 

/** Creating the ACL object */
require_once 'Zend/Acl.php';
$myAcl = new Zend_Acl();

 

Creating the roles

Now we create the roles in our Zend_Acl instance.

 

/** Creating Roles */
require_once 'Zend/Acl/Role.php';
$myAcl->addRole(new Zend_Acl_Role('guest'))
->addRole(new Zend_Acl_Role('writer'), 'guest')
->addRole(new Zend_Acl_Role('admin'), 'writer');

 

Creating the resources

And then we create the resources needed (one per controller) and their relationship with the roles we created.

 

/** Creating resources */
require_once 'Zend/Acl/Resource.php';
$myAcl->add(new Zend_Acl_Resource('user'))
->add(new Zend_Acl_Resource('article'));

 

Creating the permissions

Now that we added the roles and resources to our Zend_Acl instance, it's time to explain what actions must be available to which roles.

  1. Guest won't have access to edit, add or approve an article.
  2. Writer won't have access to approve an article.
  3. Admin will have complete access.

 

/** Creating permissions */
$myAcl->allow('guest', 'user')
->deny('guest', 'article')
->allow('guest', 'article', 'view')
->allow('writer', 'article', array('add', 'edit'))
->allow('admin', 'article', 'approve');

 

Creating the access denied view file

We will need to create a view and an action which will address all those denied users, in order to do it, first we create a new action in our error controller:

 

class ErrorController extends Zend_Controller_Action
{
....

public function deniedAction()
{
}

....
}

 

And then we create our view file (/application/views/scripts/error/denied.phtml) with some warning message:

 

<h1>Error</h1>
<h2>Access denied</h2>
<p>You are trying to access an area which you have not allowed.</p>

 

Finishing the configuration

Okay, we have setup our Zend_Acl configuration, so far, it doesn't look like something new, but the next step is register the controller plugin, this important part takes the Zend_Acl instance we created and then validates it against the current page being accessed by an user.

 

/** Setting up the front controller */ 
require_once 'Zend/Controller/Front.php';
$front = Zend_Controller_Front::getInstance();
$front->setControllerDirectory('path/to/controllers');

/** Registering the Plugin object */
require_once 'Zend/Controller/Plugin/Acl.php';
$aclPlugin = new Zend_Controller_Plugin_Acl($myAcl);
$aclPlugin->setRoleName($currentUserRole);

$front->registerPlugin(new Zend_Controller_Plugin_Acl($acl, 'guest'));

/** Dispatching the front controller */
$front->dispatch();

 

After this configuration is done, once an user enters in our application, depending the role he/she has the page will be displayed or an access denied page will be displayed.

For more information about this you can go to:
Zend_Acl & MVC Integration
and here is a small implementation source code of this:
Source Code

————END————
由于本文并没有什么特别的地方,而且单词也没有什么,故不作翻译。




本站采用创作共享版权协议, 要求署名、非商业和保持一致. 本站欢迎任何非商业应用的转载, 但须注明出自"易栈网-膘叔", 保留原始链接, 此外还必须标注原文标题和链接.

Tags: framework, zend, mvc, zend_acl

« 上一篇 | 下一篇 »

只显示10条记录相关文章

QEE PHP 发布 (浏览: 34343, 评论: 5)
ZendFramewok介绍 (浏览: 33044, 评论: 0)
Fleaphp介绍(在迟来的QeePhp前的最后介绍) (浏览: 31922, 评论: 0)
【笔记】UBUNTU下安装zendoptimizer (浏览: 29829, 评论: 2)
使用ZendFramework的时候打印SQL (浏览: 29704, 评论: 0)
Yiiframework(Yii框架)开发笔记:续四 (浏览: 27671, 评论: 3)
來自風雪之隅的:深入理解PHP原理之foreach (浏览: 25890, 评论: 1)
zendframework 1.6终于将SOAP放进去了 (浏览: 25690, 评论: 0)
测试JS的速度 (浏览: 24770, 评论: 0)
9 个基于JavaScript 和 CSS 的 Web 图表框架 (浏览: 24330, 评论: 0)

发表评论

评论内容 (必填):