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 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!
Well, the first thing that looks out of place to me is:
This looks very wrong and might be causing your problems. I would suggest changing this to something like:
or
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 iffalse
is equal tonull
, 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 valuenull
, you're wanting to check if the data type of$this->Auth->user()
is equal to the typenull
. 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!==
.Your
if
conditions look weird. They are: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?