What is the neat soluton to check user group level to do certain tasks
Groups: 2 Admin, 3 Moderators, 4 Process Orders
Let assume data['user']['group_id'] is 3
I came up with this Solution.
Solution 1:
$allowGroup = array(2, 3, 4);
if (in_array($this->data['user']['group_id'], $allowGroup)) {
//Show a list of records
if ($this->data['user']['group_id'] == 2) {
//Show buttons to delete records (only admin can do this)
}
}
Solution 2:
if (($this->data['user']['group_id'] == 3) || ($this->data['user']['group_id'] == 4)) {
//Member can do this action..
}
A slightly more efficient, but less readable method would be to specify your “allowed” levels as the array keys. Then it’s a simple array lookup, without having to call in_array(), which would do a (more) expensive loop: