我在codeigniter中安装了使用Ion auth的用户管理。现在我正面临着以下挑战。
登录并访问自动/索引页面后,将显示用户概述。我使用3种不同的管理级别。每个用户都连接到一家公司。 (公司ID添加在users_groups表中)
管理 公司管理员 公司用户
超级管理员应该看到所有用户, 管理员应该只看到同一公司的用户 用户无法访问auth / index(已经可以使用)
如何以管理员仅查看其公司用户的方式创建页面。下面是我的auth.php控制器索引函数的一个例子。
//redirect if needed, otherwise display the user list
function index()
{
if (!$this->ion_auth->logged_in())
{
//redirect them to the login page
redirect('dashboard/', 'refresh');
}
elseif ($this->ion_auth->in_group('company-user'))
{
//redirect them to the home page because they must be an administrator to view this
redirect('dashboard/', 'refresh');
//set the flash data error message if there is one
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
//list the users
$this->data['users'] = $this->ion_auth->users()->result();
foreach ($this->data['users'] as $k => $user)
{
$this->data['users'][$k]->groups = $this->ion_auth->get_users_groups($user->id)->result();
}
foreach ($this->data['users'] as $k => $user)
{
$this->data['users'][$k]->companies = $this->ion_auth->get_company($user->id)->result();
}
}
elseif ($this->ion_auth->logged_in() && $this->ion_auth->in_group("company-admin"))
{
//set the flash data error message if there is one
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
//list the users
$this->data['users'] = $this->ion_auth->users()->result();
foreach ($this->data['users'] as $k => $user)
{
$this->data['users'][$k]->groups = $this->ion_auth->get_users_groups($user->id)->result();
}
foreach ($this->data['users'] as $k => $user)
{
$this->data['users'][$k]->companies = $this->ion_auth->get_company($user->id)->result();
}
$this->_render_page('admin/auth/index', $this->data);
}
else
{
//set the flash data error message if there is one
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
//list the users
$this->data['users'] = $this->ion_auth->users()->result();
foreach ($this->data['users'] as $k => $user)
{
$this->data['users'][$k]->groups = $this->ion_auth->get_users_groups($user->id)->result();
}
foreach ($this->data['users'] as $k => $user)
{
$this->data['users'][$k]->companies = $this->ion_auth->get_company($user->id)->result();
}
$this->_render_page('admin/auth/index', $this->data);
}
}
有没有人知道我必须添加到elseif($ this-> ion_auth-> in_group(“company-admin”))部分,以便只显示与公司在同一公司的用户-admin?
/////////////////////////////////////////////// ///////
感谢您的回答。现在我做了以下更改:
我正在回答这个问题,有可能展示代码。我已经更改了我的控制器,但仍然看到其他公司的用户。我已将此更改为:
elseif ($this->ion_auth->in_group("company-admin"))
{
//set the flash data error message if there is one
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
// Check the company the user is in
$user_in_company = $this->ion_auth->get_users_companies(); // Return array groups
//list the users
$this->data['users'] = $this->ion_auth->users()->result($user_in_company);
foreach ($this->data['users'] as $k => $user)
{
$this->data['users'][$k]->groups = $this->ion_auth->get_users_groups($user->id)->result();
}
foreach ($this->data['users'] as $k => $user)
{
$this->data['users'][$k]->companies = $this->ion_auth->get_company($user->id)->result();
}
$this->_render_page('admin/auth/index', $this->data);
}
我希望只能查看公司内部的用户...我的控制器可能出现了问题吗?
答案 0 :(得分:2)
首先让群组登录用户。
$user_in_group = $this->ion_auth->get_users_groups(); // Return array groups
获取与已登录用户具有相同组的用户列表
$this->data['users'] = $this->ion_auth->users($user_in_group)->result(); // Pass groups array as params
列出的用户只有已登录的用户组。
答案 1 :(得分:0)
可能你需要这个
$group_id = 3; //your group id in database
$this->data['users'] = $this->ion_auth->users($group_id)->result();
然后在你的视图中循环。