Search code examples
zend-frameworkzend-authzend-acl

Problem defining what to do with 'guest' user in Zend_Acl


I'm getting the following error on every view available for the 'guest' user:

Notice: Trying to get property of non-object in /home/fiodorovich/public_html/gisele/library/Federico/Plugin/Acl.php on line 35

the line it's referring to is '$role = $this->_auth->getStorage()->read()->role;' in:

public function preDispatch (Zend_Controller_Request_Abstract $request)
{
    $role = $this->_auth->getStorage()->read()->role;

    if($role === null) {
        $role = self::DEFAULT_ROLE;
    }
    $action = $request->getActionName();
    $controller = $request->getControllerName();
    if($this->_acl->has($controller)) {
        if(!$this->_acl->isAllowed($role, $controller, $action)) {
            $request->setActionName('error');
            $request->setControllerName('error');
        }
    }
}

I know it's just a notice, and that it won't show in production since errors will be disabled... however it's kind of bugging me. So how could I solve this?


Solution

  • Use $this->_auth->hasIdentity() before request data from storage.

    if ($this->_auth->hasIdentity()) {
        // user is logged in and we can get role
        $role = $this->_auth->getStorage()->read()->role;  
    } else {
        // guest
        $role = self::DEFAULT_ROLE;
    }