I have an if statement in a cakePHP app and I cant work out why its not doing what I would expect.
public function isAuthorized($user) {
if ($user['role'] === 'admin'){
return true;
}
if ((in_array($this->action, array('view', 'index')))&&($user['role'] === 'senior' || 'junior')) {
return true;
}
return false;
}
I would expect that if there is a user with the role “agent” it would deny all actions.
If I use this instead everthing is rosey, just not sure why its not checking both arguments before setting the boolean to True?
public function isAuthorized($user) {
if ($user['role'] === 'admin'){
return true;
}
if ($user['role'] == 'agent'){
return false;
}
if (in_array($this->action, array('edit', 'add', 'delete'))) {
if ($user['role'] == 'senior' || 'junior') {
return false;
}
}
return true;
}
Any Ideas?
Thanks
One of your test is wrong and always evaluates to true.
because you are evaluating
'junior'as a boolean, which is true in PHP.Your condition should be:
Note that you could also write it like this: