Reputation: 151
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
Upvotes: 0
Views: 46
Reputation: 5001
One of your test is wrong and always evaluates to true.
if($user['role'] === 'senior' || 'junior'){
//will always be true
}
because you are evaluating 'junior'
as a boolean, which is true in PHP.
Your condition should be:
if($user['role'] == 'senior' || $user['role'] == 'junior'){
...
}
Note that you could also write it like this:
if(in_array($user['role'], array('senior', 'junior'))){
}
Upvotes: 2