Question

I have a bugs, actually it is error this is notice but still, I would like to fix it;

When I am try to access pages with null role, it is showing this messages:

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

How can get it fixed here is my script:

 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');
        }
    }

Line: 18 is this line :$role = $identity->role;

Was it helpful?

Solution

  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');
            }
        }

Here 'guest' is acting as default role . You can name it anything you like but make sure to add this role in your acl and give permission to this role accordingly .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top