【问题标题】:Ion auth only show users in certain groupIon auth 仅显示特定组中的用户
【发布时间】:2013-08-16 08:25:39
【问题描述】:

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

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

【问题讨论】:

    标签: php mysql codeigniter authentication ion-auth


    【解决方案1】:

    首先获取组登录用户。

    $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
    

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

    【讨论】:

    • 感谢您的回答。我已经更改了我的代码(有关更改,请参见上文)我希望我的 auth/index 仅显示公司内的用户,但我仍然看到所有用户。
    • @mastahb in_group 仅检查组中的用户。使用我的代码,仅列出通过组的用户。我更新了关于$this->data['users'] 行的代码。退房。
    • 我已经解决了这个问题,方法是在我的 ion_auth_model 中创建一个新函数来从数据库中获取正确的数据
    • jep 我试过但没有让它们工作。我对此很陌生,所以仍在尝试..但非常感谢您的帮助
    【解决方案2】:

    也许你需要这个

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

    然后在你的视图中循环。

    【讨论】:

      猜你喜欢
      • 2012-05-30
      • 2014-05-29
      • 2023-04-10
      • 2016-01-23
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多