0

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;

Akram
  • 423
  • 2
  • 5
  • 13
  • it looks like role is not valid. Are you sure the role was saved as part of the identity in Zend_Auth? Dump identity and see whats there. You may not be saving the role to the session correctly. To see why the property is invalid we'll need to see the code where the identity is saved to the storage. – RockyFord May 13 '12 at 08:12
  • No, I have not add anything, becuase based on the tutorial I found there is no such thing. can you show me how to do it – Akram May 13 '12 at 08:44
  • Have you actually called the role 'null'? If so change it. You're going to have all sorts of problems otherwise. – vascowhite May 13 '12 at 08:51
  • so, how can I define it in other way – Akram May 13 '12 at 11:34

1 Answers1

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

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 .

Mr Coder
  • 8,169
  • 5
  • 45
  • 74
  • But guest is supposed to have null value, if I declare guest, the system will ask me to login – Akram May 13 '12 at 11:28
  • How Can I define guest as null – Akram May 13 '12 at 11:29
  • System will only ask you to login when the given resource is not allowed for guest role . Which you can easily manage through acl . Here role guest is acting like null but giving you an opportunity to define the access level of role which is not defined . – Mr Coder May 13 '12 at 11:39
  • No, how about if the user is not loged in, in this case role will be equal to null. isn't it – Akram May 13 '12 at 11:47
  • here role = 'guest' is acting like fallback role or default role when no role is found . So even user is not loged in acl will resolve its access through guest role . – Mr Coder May 13 '12 at 12:07
  • Sorry, it was my bad, I did not notice `$role = 'guest'; //your default role`. Thank you very much – Akram May 13 '12 at 12:44