Retrieve the group id of user in Ion Auth - Codeigniter

15.6k Views Asked by At

I am using Ion Auth for my codeigniter application and all seems good except for one thing.

I need to display a list of users along with the groups they are in. How can I retrieve the group id of a particular user without making my own models and querying for it.

$this->ion_auth->user($id)->row(); does not retrieve the group id.

5

There are 5 best solutions below

3
On

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.

0
On

Try adding this to ion_auth_model.php (Or place the query somewhere else)

/**
 * 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']);
}
1
On

I came across this because I wanted to add a user's group_id(s) to the session upon successful login. In case anyone is interested, here's how I did it (once i managed to figure out where everything was being done).

In ion_auth_model.php, I took the set_session function:

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

and modified it to this:

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

now it writes an array to the session called groups that is a list of all the groups the user belongs to.

The only other thing I had to do was to modify the logout function in Ion_auth.php (in application/libraries) to make sure the group session variable is unset by adding

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

to the list of other unset_userdata() statements.

I know I probably should have just extended the libraries/models to keep the core untouched, but you could take what I did and easily do that.

hope this helps someone.

Rob

0
On

use in the controller:

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

and in view use:

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

but what about query to get groups for current logged in user (for example in my profile) I find next decision. Controller: $this->data['this_user_groups'] = $this->ion_auth->get_users_groups()->result(); and view just:

<?php foreach ($this_user_groups as $group):?>
                                <?php echo $group->description;?>,
                            <?php endforeach?>
1
On

get the Object:

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

Get the name of the group:

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

In my case:

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

You can check the offcial doc about get users groups:

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