2

我已经在 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")) 部分,以便仅显示与公司管理员在同一公司的用户?

///////////////////////////////////////// ////

感谢你的回答。现在我做了以下更改:

我以这种方式回答,以便有可能显示代码。我已经更改了我的控制器,但仍然可以看到其他公司的用户。我已将其更改如下:

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);
    }   

我希望只查看公司内的用户,但我的控制器可能有问题吗?

4

2 回答 2

3

首先获取组登录用户。

$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

并且列出的用户只有登录的用户组。

于 2013-08-14T12:02:57.800 回答
0

可能你需要这个

$group_id = 3; //your group id in database $this->data['users'] = $this->ion_auth->users($group_id)->result();

然后在你的视图中循环。

于 2015-12-22T17:59:53.137 回答