I have set up a users group permission role system where user can have or have not access to controllers and or modify them.
Problem: if I leave all my permission modify array unchecked for a particular user group. And if that user group tried to modify the controller It throws two errors. When modify permissions are unchecked.
How can I make it so that it will work even if permissions are unchecked. And if user that has that user group try's to modify it. It will not throw these two errors.
Error One
A PHP Error was encountered
Severity: Notice
Message: Undefined index: modify
Filename: libraries/User.php
Line Number: 206
Error Two
A PHP Error was encountered
Severity: Warning
Message: in_array() expects parameter 2 to be array, null given
Filename: libraries/User.php
Line Number: 206
User Library Function
public function hasPermissionModify() {
$user_id = $this->CI->session->userdata('user_id');
$segment = $this->CI->uri->segment_array();
$permission = isset($segment[2]) ? $segment[2] : '';
$ignore = array(
'blank',
'error',
'register',
'dashboard',
'column_left',
'menu',
'startup',
'login',
'logout',
'forgotten',
'reset',
'not_found',
'permission',
'footer',
'header'
);
if (isset($permission) && $permission && $user_id) {
if (in_array($permission, $ignore)) {
return true;
}
$user_group_info = $this->CI->users_model->getUser($user_id);
if (!empty($user_group_info)) {
$user_group_data = $this->CI->users_group_model->getUserGroup($user_group_info['user_group_id']);
// Error Here
if (!in_array($permission ,$user_group_data['permission']['modify'] )) {
return false;
} else {
return true;
}
} else {
return false;
}
} else {
return true;
}
}
Not sure exactly what $this->CI->users_group_model->getUserGroup()
does - however you should check the index in case if does not exist, before doing an in_array
and further examine the variable $user_group_data
to see how it looks and go through getUserGroup function to see what it returns. See modifications to the function below:
public function hasPermissionModify()
{
$user_id = $this->CI->session->userdata('user_id');
$segment = $this->CI->uri->segment_array();
$permission = isset($segment[2]) ? $segment[2] : '';
$ignore = array(
'blank',
'error',
'register',
'dashboard',
'column_left',
'menu',
'startup',
'login',
'logout',
'forgotten',
'reset',
'not_found',
'permission',
'footer',
'header'
);
if (isset($permission) && $permission && $user_id)
{
if (in_array($permission, $ignore))
{
return true;
}
$user_group_info = $this->CI->users_model->getUser($user_id);
if(!empty($user_group_info))
{
$user_group_data = $this->CI->users_group_model->getUserGroup($user_group_info['user_group_id']);
// Error Here
//$user_group_data does not have an index called modify.
//Examine it here and check getUserGroup function to see what is wrong
echo "value of user_group_data:<pre>".print_r($user_group_data,true)."</pre>";
//in any case if that index is not there return false
if((!array_key_exists('permission',$user_group_data)) ||
(!array_key_exists('modify',$user_group_data['permission']))
{
return false;
}
if (!in_array($permission ,$user_group_data['permission']['modify'] ))
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
else
{
return true;
}
}