Zend学习杂谈–Zend_Acl角色访问控制

2007-8-28  07:59

引导文件(在这里没有使用URL重写)

PHP代码

     

  1. <?php
  2. error_reporting(E_ALL);
  3. require ‘./Zend/Loader.php’;
  4. function __autoload($class)
  5. {
  6. Zend_Loader::loadClass($class);
  7. }
  8. //初始化访问控制连
  9. $acl = new Zend_Acl;
  10. $acl->add(new Zend_Acl_Resource(‘Default’));
  11. $acl->add(new Zend_Acl_Resource(‘News’));
  12. //上面对应我的个module,一个Default,一个News
  13. $acl->addRole(new Zend_Acl_Role(‘guest’));
  14. $acl->addRole(new Zend_Acl_Role(‘user’), ‘guest’);
  15. $acl->addRole(new Zend_Acl_Role(’staff’), ‘user’);
  16. $acl->addRole(new Zend_Acl_Role(‘admin’));
  17. $acl->allow(‘guest’, array(‘Default’, ‘News’), ‘view’);
  18. $acl->allow(‘user’, array(‘Default’, ‘News’), array(‘reply’, ‘download’));
  19. $acl->allow(’staff’, array(‘Default’, ‘News’), array(‘delete’, ‘update’));
  20. $acl->allow(‘admin’);
  21. //验证权限,如果没有登录则以游客身份登录
  22. $auth = Zend_Auth::getInstance();
  23. if(!$auth->hasIdentity())
  24. {
  25. $auth->getStorage()->write((object)array(‘username’ => ‘Guest’,
  26. ‘role’ => ‘guest’,
  27. ‘truename’ => ‘游客’));
  28. }
  29. $router = new Zend_Controller_Router_Rewrite();
  30. //$router->addRoute(’root’,new Zend_Controller_Router_Route(’/',array(’module’ =>’News’, ‘controller’ => ‘Index’, ‘Action’ => ‘index’))); 也是给出默认控制器的
  31. $front = Zend_Controller_Front::getInstance()->setControllerDirectory(array(
  32. ‘default’ => ‘./Default/Controllers’,
  33. ‘News’ => ‘./News/Controllers’
  34. ))
  35. ->setRouter($router)
  36. ->setParam(‘Zend_Acl’, $acl)
  37. ->setParam(‘Zend_Auth’, $auth)
  38. ->setBaseUrl(‘/zf/index.php’)
  39. ->setParam(‘noViewRenderer’, true)
  40. ->setParam(‘useDefaultControllerAlways’,true)
  41. ->throwExceptions(true)->returnResponse(false)
  42. ->dispatch();

 


即在引导文件中实现身份认证,在这里,如果没有登录则以游客身份登录!

让控制器接受授权:

PHP代码

     

  1. <?php
  2. class IndexController extends Zend_Controller_Action
  3. {
  4. public function indexAction()
  5. {
  6. $view = new Zend_View;
  7. $view->name = ‘张心灵’;
  8. $view->title = ‘测试’;
  9. $view->setScriptPath(‘./Default/Views/Index’);
  10. $view->addScriptPath(‘./Default/Views’);
  11. $this->getResponse()->appendBody($view->render(‘Index.phtml’));
  12. }
  13. public function downloadAction()
  14. {
  15. $acl = $this->getInvokeArg(‘Zend_Acl’);
  16. if(!$acl->isAllowed($this->getInvokeArg(‘Zend_Auth’)->getStorage()->read()->role, ‘Default’, ‘download’)) $this->getResponse()->appendBody(‘访问不合法’);
  17. else $this->getResponse()->appendBody(‘合法访问’);
  18. }
  19. }

 


很显然,我们是在 downloadAction 方法中实现的授权,这样灵活,但是不是被鼓励的方法,上例执行结果一切正常,如果你的角色不是 user、staff或者admin的话,执行的结果是 访问不合法!

更集中的权限控制

当然,上例和不使用MVC模式时授权是一摸一样的,这样貌似体现不了MVC模式的优势了!
集中的权限控制(在 preDispatch 中进行权限控制)

 

PHP代码
  1. <?php
  2. class IndexController extends Zend_Controller_Action
  3. {
  4. public function preDispatch()
  5. {
  6. if(!$this->getInvokeArg(‘Zend_Acl’)->isAllowed($this->getInvokeArg(‘Zend_Auth’)->getStorage()->read()->role, ucfirst($this->getRequest()->getModuleName()), $this->getRequest()->getActionName()))
  7. $this->_forward(‘index’, ‘user’, ‘Default’);
  8. }
  9. public function indexAction()
  10. {
  11. $view = new Zend_View;
  12. $view->name = ‘张心灵’;
  13. $view->title = ‘测试’;
  14. $view->setScriptPath(‘./Default/Views/Index’);
  15. $view->addScriptPath(‘./Default/Views’);
  16. $this->getResponse()->appendBody($view->render(‘Index.phtml’));
  17. }
  18. public function downloadAction()
  19. {
  20. $this->getResponse()->appendBody(‘合法访问’);
  21. }
  22. }

这 样也可以实现用户的权限控制了,一切不合法的访问都会实际上被指派到 Default 模块的 ,UserController 控制器的 loginAction 方法,而同时在编写具体的实现时完全从身份认证,授权的逻辑中解脱出来!(可以看到 在downloadAction 中你只要专注于 download功能就可以了,再也不用考虑那些人可以访问这一方法了)

发表评论