7

我将 Ion Auth 用于我的 codeigniter 应用程序,除了一件事之外,一切似乎都很好。

我需要显示用户列表以及他们所在的组。如何在不制作自己的模型并查询它的情况下检索特定用户的组 ID。

$this->ion_auth->user($id)->row();不检索组 ID。

4

5 回答 5

9

Ion Auth has updated and removed the get_user function in the latest version. As a result of this, the below should return the current group id of the logged in user:

$this->ion_auth->get_users_groups()->row()->id

If you're wanting to get the group id of a particular user, you can pass the user id into the get_users_groups method.

于 2012-05-24T11:34:28.010 回答
4

获取对象:

$user_groups = $this->ion_auth->get_users_groups($user->id)->result();

获取组名:

$this->ion_auth->get_users_groups($data['id'])->row()->name;

就我而言:

$data['groups'] = $this->ion_auth->get_users_groups($data['id'])->row()

您可以查看有关获取用户组的官方文档:

http://benedmunds.com/ion_auth/#get_users_groups

于 2012-07-03T11:25:54.423 回答
3

我遇到了这个问题,因为我想在成功登录后将用户的 group_id(s) 添加到会话中。如果有人感兴趣,这就是我的做法(一旦我设法弄清楚一切都在哪里完成)。

在 ion_auth_model.php 中,我使用了 set_session 函数:

    public function set_session($user)
{

    $this->trigger_events('pre_set_session');

    $session_data = array(
        'identity'             => $user->{$this->identity_column},
        'username'             => $user->username,
        'email'                => $user->email,
        'user_id'              => $user->id, //everyone likes to overwrite id so we'll use user_id
        'old_last_login'       => $user->last_login
    );

    $this->session->set_userdata($session_data);

    $this->trigger_events('post_set_session');

    return TRUE;
}

并将其修改为:

    public function set_session($user)
{

    $this->trigger_events('pre_set_session');

    $session_data = array(
        'identity'             => $user->{$this->identity_column},
        'username'             => $user->username,
        'email'                => $user->email,
        'user_id'              => $user->id, //everyone likes to overwrite id so we'll use user_id
        'old_last_login'       => $user->last_login
    );

    //get user group ids for user and pass to session
    $groups = $this->ion_auth->get_users_groups($user->id)->result();

    foreach ($groups as $row){
        $session_data['groups'][] = $row->id;
    }

    $this->session->set_userdata($session_data);

    $this->trigger_events('post_set_session');

    return TRUE;
}

现在它向会话中写入一个名为 groups 的数组,该数组是用户所属的所有组的列表。

我唯一要做的另一件事是修改 Ion_auth.php 中的注销功能(在应用程序/库中),以确保通过添加取消设置组会话变量

$this->session->unset_userdata('groups');

到其他 unset_userdata() 语句的列表。

我知道我可能应该只是扩展库/模型以保持核心不变,但你可以接受我所做的并轻松地做到这一点。

希望这可以帮助某人。

于 2013-03-06T16:36:36.163 回答
0

尝试将此添加到 ion_auth_model.php (或将查询放在其他地方)

/**
 * get_all_users_with_group
 *
 * @return array
 **/
public function get_all_users_with_group()
{
    $this->trigger_events('get_all_users_with_groups');

    return $this->db->select(
        $this->tables['users'].'.*, '.
        $this->tables['users_groups'].'.'.$this->join['groups'].' as group_id, '.
        $this->tables['groups'].'.name as group_name, '.
        $this->tables['groups'].'.description as group_desc'
        )
    ->join($this->tables['users_groups'], $this->tables['users_groups'].'.'.$this->join['users'].'='.$this->tables['users'].'.id')
    ->join($this->tables['groups'], $this->tables['users_groups'].'.'.$this->join['groups'].'='.$this->tables['groups'].'.id')
    ->get($this->tables['users']);
}
于 2012-06-08T12:31:23.510 回答
0

在控制器中使用:

$groups = array(1,2,3,4);
            $this->data['list_staff'] = $this->ion_auth->users($groups)->result(); // выбираем всех teachers из зарегистрированных пользователей
            foreach ($this->data['list_staff'] as $k => $one_staff)
            {
                $this->data['list_staff'][$k]->groups = $this->ion_auth->get_users_groups($one_staff->id)->result();
            } 

并考虑使用:

<?php foreach($list_staff as $one):?> 
    <?php foreach ($one->groups as $group):?>
                                    <?php echo $group->description;?>,
                                <?php endforeach?> 
<?endforeach;?> 

但是如何查询以获取当前登录用户的组(例如在我的个人资料中)我找到了下一个决定。控制器: $this->data['this_user_groups'] = $this->ion_auth->get_users_groups()->result(); 并仅查看:

<?php foreach ($this_user_groups as $group):?>
                                <?php echo $group->description;?>,
                            <?php endforeach?>
于 2012-06-13T18:18:38.320 回答