zend框架中的访问控制

时间:2012-05-13 05:17:36

标签: zend-framework zend-acl

我有一个错误,实际上这是错误,这是通知但仍然,我想解决它;

当我尝试访问null role的网页时,会显示以下消息:

Notice: Trying to get property of non-object in C:\Zend\Apache2\htdocs\hotelrwanda\application\plugin\AccessCheck.php on line 18

如何解决这个问题是我的脚本:

 public function preDispatch(Zend_Controller_Request_Abstract $request) {

        $resource = $request->getControllerName();
        $action = $request->getActionName();

        $identity = $this->_auth->getStorage()->read();
        $role = $identity->role;


        if(!$this->_acl->isAllowed($role, $resource, $action)){
            $request->setControllerName('users')
                    ->setActionName('login');
        }
    }

行:18是这一行:$role = $identity->role;

1 个答案:

答案 0 :(得分:3)

  public function preDispatch(Zend_Controller_Request_Abstract $request) {

            $resource = $request->getControllerName();
            $action = $request->getActionName();

           $role = 'guest';  //your default role 

        if(Zend_Auth::getInstance()->hasIdentity())
        {
            $role = Zend_Auth::getInstance()->getIdentity()->role;
        }


            if(!$this->_acl->isAllowed($role, $resource, $action)){
                $request->setControllerName('users')
                        ->setActionName('login');
            }
        }

这里'来宾'扮演默认角色。您可以将其命名为任何名称,但请确保在您的acl中添加此角色并相应地授予此角色权限。