我已经设置了一个用户组权限角色系统,用户可以拥有或不访问控制器或修改它们。
问题:如果我保留所有权限,则修改某个特定用户组的数组未选中。如果该用户组试图修改控制器它会抛出两个错误。取消选中修改权限时。
即使取消选中权限,我怎样才能使它能够正常工作。如果拥有该用户组的用户尝试修改它。它不会抛出这两个错误。
错误一
A PHP Error was encountered
Severity: Notice
Message: Undefined index: modify
Filename: libraries/User.php
Line Number: 206
错误二
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
用户库功能
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;
}
}
答案 0 :(得分:1)
不确定$this->CI->users_group_model->getUserGroup()
究竟是什么 - 但是如果不存在则应该检查索引,然后再执行in_array
并进一步检查变量$user_group_data
以查看其外观并通过getUserGroup函数查看它返回的内容。请参阅以下函数的修改:
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;
}
}