1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Can I do a permission check in PreDispatch?

Discussion in 'XenForo Development Discussions' started by Jaxel, Mar 10, 2011.

  1. Jaxel

    Jaxel Well-Known Member

    I have a public controller... each function of it, has their own little permissions. But there is also a global permission which must be true in order to do any of the functions. Do I need to check this global permission in every single function? Or can I do it in a preDispatch? I am trying using the following code below, but it doesn't seem to fire. If I move this permission check into each function, it works, but I would rather keep the redundancy down.
    PHP:
    public function _preDispatch($action)
    {
        
    parent::_preDispatch($action);

        
    $this->perms $this->getModelFromCache('EWRmedio_Model_Perms')->getPerm();

        if (!
    $this->perms['view']) { return $this->responseNoPermission(); } // ???
    }
     
  2. Mike

    Mike XenForo Developer Staff Member

    You can do that, but you can't return from that function - you need to throw an exception:

    throw $this->responseException($this->responseNoPermission());

    For example, though there's actually a method that combines those 2 calls... :)
     
  3. ragtek

    ragtek Guest

    yep
    throw $this->getNoPermissionResponseException()
     
  4. Jaxel

    Jaxel Well-Known Member

    Thanks, this was exactly what I was looking for.
     

Share This Page