Zend学习杂谈–Zend_Acl角色访问控制
2007-8-28 07:59引导文件(在这里没有使用URL重写)
PHP代码
- <?php
- error_reporting(E_ALL);
- require ‘./Zend/Loader.php’;
- function __autoload($class)
- {
- Zend_Loader::loadClass($class);
- }
- //初始化访问控制连
- $acl = new Zend_Acl;
- $acl->add(new Zend_Acl_Resource(‘Default’));
- $acl->add(new Zend_Acl_Resource(‘News’));
- //上面对应我的个module,一个Default,一个News
- $acl->addRole(new Zend_Acl_Role(‘guest’));
- $acl->addRole(new Zend_Acl_Role(‘user’), ‘guest’);
- $acl->addRole(new Zend_Acl_Role(’staff’), ‘user’);
- $acl->addRole(new Zend_Acl_Role(‘admin’));
- $acl->allow(‘guest’, array(‘Default’, ‘News’), ‘view’);
- $acl->allow(‘user’, array(‘Default’, ‘News’), array(‘reply’, ‘download’));
- $acl->allow(’staff’, array(‘Default’, ‘News’), array(‘delete’, ‘update’));
- $acl->allow(‘admin’);
- //验证权限,如果没有登录则以游客身份登录
- $auth = Zend_Auth::getInstance();
- if(!$auth->hasIdentity())
- {
- $auth->getStorage()->write((object)array(‘username’ => ‘Guest’,
- ‘role’ => ‘guest’,
- ‘truename’ => ‘游客’));
- }
- $router = new Zend_Controller_Router_Rewrite();
- //$router->addRoute(’root’,new Zend_Controller_Router_Route(’/',array(’module’ =>’News’, ‘controller’ => ‘Index’, ‘Action’ => ‘index’))); 也是给出默认控制器的
- $front = Zend_Controller_Front::getInstance()->setControllerDirectory(array(
- ‘default’ => ‘./Default/Controllers’,
- ‘News’ => ‘./News/Controllers’
- ))
- ->setRouter($router)
- ->setParam(‘Zend_Acl’, $acl)
- ->setParam(‘Zend_Auth’, $auth)
- ->setBaseUrl(‘/zf/index.php’)
- ->setParam(‘noViewRenderer’, true)
- ->setParam(‘useDefaultControllerAlways’,true)
- ->throwExceptions(true)->returnResponse(false)
- ->dispatch();
即在引导文件中实现身份认证,在这里,如果没有登录则以游客身份登录!
让控制器接受授权:
PHP代码
- <?php
- class IndexController extends Zend_Controller_Action
- {
- public function indexAction()
- {
- $view = new Zend_View;
- $view->name = ‘张心灵’;
- $view->title = ‘测试’;
- $view->setScriptPath(‘./Default/Views/Index’);
- $view->addScriptPath(‘./Default/Views’);
- $this->getResponse()->appendBody($view->render(‘Index.phtml’));
- }
- public function downloadAction()
- {
- $acl = $this->getInvokeArg(‘Zend_Acl’);
- if(!$acl->isAllowed($this->getInvokeArg(‘Zend_Auth’)->getStorage()->read()->role, ‘Default’, ‘download’)) $this->getResponse()->appendBody(‘访问不合法’);
- else $this->getResponse()->appendBody(‘合法访问’);
- }
- }
很显然,我们是在 downloadAction 方法中实现的授权,这样灵活,但是不是被鼓励的方法,上例执行结果一切正常,如果你的角色不是 user、staff或者admin的话,执行的结果是 访问不合法!
更集中的权限控制
当然,上例和不使用MVC模式时授权是一摸一样的,这样貌似体现不了MVC模式的优势了!
集中的权限控制(在 preDispatch 中进行权限控制)
集中的权限控制(在 preDispatch 中进行权限控制)
PHP代码
- <?php
- class IndexController extends Zend_Controller_Action
- {
- public function preDispatch()
- {
- if(!$this->getInvokeArg(‘Zend_Acl’)->isAllowed($this->getInvokeArg(‘Zend_Auth’)->getStorage()->read()->role, ucfirst($this->getRequest()->getModuleName()), $this->getRequest()->getActionName()))
- $this->_forward(‘index’, ‘user’, ‘Default’);
- }
- public function indexAction()
- {
- $view = new Zend_View;
- $view->name = ‘张心灵’;
- $view->title = ‘测试’;
- $view->setScriptPath(‘./Default/Views/Index’);
- $view->addScriptPath(‘./Default/Views’);
- $this->getResponse()->appendBody($view->render(‘Index.phtml’));
- }
- public function downloadAction()
- {
- $this->getResponse()->appendBody(‘合法访问’);
- }
- }
这 样也可以实现用户的权限控制了,一切不合法的访问都会实际上被指派到 Default 模块的 ,UserController 控制器的 loginAction 方法,而同时在编写具体的实现时完全从身份认证,授权的逻辑中解脱出来!(可以看到 在downloadAction 中你只要专注于 download功能就可以了,再也不用考虑那些人可以访问这一方法了)
通过RSS 2.0来获取此文章的最新评论.