In Codeigniter I am creating some helpers to check user in certain group/s by allowing passing group name as a string or an array for multiple groups. Now seems things are working fine if I don't pass userid for $id
argument. When I set the $id
to some userid like 2
it is giving an error below
Message: Cannot use a scalar value as an array
Here is my all helper functions and than usage.
Getting user's groups
if ( !function_exists('get_user_group') )
{
function get_user_group($id = FALSE)
{
$CI =& get_instance();
// getting user groups by id or logged in
$object = $CI->ion_auth->get_users_groups($id)->result();
foreach($object as $group):
$id[] = $group->id;
$groups[] = $group->name;
endforeach;
$groups = array_combine($id, $groups);
return $groups;
}
}
Check if User in the group/s
if ( !function_exists('in_group') )
{
function in_group($groups, $id = FALSE)
{
if(is_array($groups)){
foreach($groups as $group){
//print_r($group);
if(in_array($group, get_user_group($id))){
return TRUE;
}
}
} elseif(is_string($group)) {
if(in_array($group, get_user_group($id))){
return TRUE;
}
}
}
}
Usage
if(in_group(array('hr'), 2)){
echo '<h1>IS IN THIS GROUP</h1>';
} else {
echo '<h1>IS NOT IN THIS GROUP</h1>';
}