开发者

CakePHP - Site Offline - Admin Routing Not Working

I setup the following code in my app_controllers.php file to control access to the site when the site is set to OFFLINE (site_status = 0).

function beforeFilter(){
    // Site Offline = 0 , Site Online = 1
    if($this->Configuration->get_site_status() == 1){
          // Allow access to the site to all users and perform all required 
          // beforeFilter code
    }else{
        ...
        // If site is OFFLINE but User is logged in allow access. 
        // Later I will need to change开发者_StackOverflow社区 it to only allow admin access if logged in as I am still developing
        // Everyone else will be denied access even if they are able to authenticate          
        if(!$this->Auth->user() == null){
            $this->layout = 'default';
            $this->Auth->allow('*');
        }else{        
            $this->layout = 'offline';
            $this->Auth->deny('*');
        }
        ...
    }
}

Everything works great when the requested address looks like the following:

http://www.mydomain.com/articles

However, when I have the following it does not work properly

http://www.mydomain.com/admin/articles

It prevents access to the site correctly, but it fails to use the $this->layout = 'offline'. It defaults back to the default layout.

What do I need to do to fix this.

Thank you!


Your if conditions look weird. They are:

If site is offline and user logged in
    use default layout
otherwise
    use offline layout and require authentication on all pages

I.e. you're using offline layout when site is online OR user is not logged in. Are you sure that is what you want?


Well, the first thing that looks out of place to me is:

(!$this->Auth->user() == null)

This looks very wrong and might be causing your problems. I would suggest changing this to something like:

(!is_null($this->Auth->user())

or

($this->Auth->user() !== NULL)

Edits

First, check out the PHP logical operators. You were appending a NOT statement to the return value of $this->Auth->user(). So, with a user logged in you're essentially asking if false is equal to null, which of course it isn't and never will be.

Second, check out the PHP comparison operators. You aren't wanting to check if the value of $this->Auth->user() is equal to the value null, you're wanting to check if the data type of $this->Auth->user() is equal to the type null. In short, null is a data type, not a value. If you did just have to use "=" in your if statement then you would want to use the identical === check or the identical not check !==.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜