Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve the group id of user in Ion Auth - Codeigniter

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.

like image 203
WebNovice Avatar asked May 24 '12 09:05

WebNovice


3 Answers

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.

like image 70
MY_Mark Avatar answered Nov 08 '22 01:11

MY_Mark


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

like image 41
Alberto Fortes Avatar answered Nov 08 '22 02:11

Alberto Fortes


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

like image 3
rob s Avatar answered Nov 08 '22 03:11

rob s