zend_framework-----zend_acl实现用户权限的验证

zend_acl这个组件主要是通过用户定义的规则来限制各种角色对资源的访问。

首先说下role,resource是啥意思

role就是角色比如什么游客阿管理员阿之类的当让推荐使用英文的阿

resource就是资源了一般指的是控制器的名字,比如你建立了一个adminController则他对应的resource就是admin。

其他不用多说了,看下代码把!

"account","action"=>"index"); protected $_auth; public $acl; public function preDispatch(Zend_Controller_Request_Abstract $request){ $this->init(); //设置角色 $role=$this->_auth->hasIdentity()?$this->_auth->getStorage()->read()->role:$this->_defaultRole; if(!$this->acl->hasRole($role))$role=$this->_defaultRole; if(!$this->acl->isAllowed($role,$request->getControllerName())){ //echo $this->_request->getModuleName(); //echo $this->_request->getModuleKey(); $request->setModuleName($request->getModuleName()); $request->setControllerName($this->_authController['controller']); $request->setActionName($this->_authController['action']); } } public function init(){ $this->_auth=Zend_Auth::getInstance(); $this->_auth->setStorage(new Zend_Auth_Storage_Session("authNameSpace")); $this->acl=new Zend_Acl(); //添加角色 $this->acl->addRole(new Zend_Acl_Role("guest")); $this->acl->addRole(new Zend_Acl_Role("member"),"guest"); $this->acl->addRole(new Zend_Acl_Role("author"),"member"); $this->acl->addRole(new Zend_Acl_Role("admin"),"author"); //添加资源 $this->acl->addResource(new Zend_Acl_Resource("index")); $this->acl->addResource(new Zend_Acl_Resource("account")); $this->acl->addResource(new Zend_Acl_Resource("menu")); $this->acl->addResource(new Zend_Acl_Resource("main")); $this->acl->addResource(new Zend_Acl_Resource("header")); $this->acl->addResource(new Zend_Acl_Resource("sorts")); //设置规则 $this->acl->deny("guest"); $this->acl->deny("member"); $this->acl->allow("author"); $this->acl->allow("admin"); } }

这个是建立了一个插件来检测用户的权限

他是在dispatch以前调用的,当然要想调用这个插件还要去入口文件中把这个插件注册一下( $front->registerPlugin(new Plugin_AclManager()))就ok了!

你可能感兴趣的:(Zend,Framework)