Get user group id in Ion Auth - Codeigniter

I use Ion Auth for my codeigniter application, and everything seems to be good, except for one.

I need to display a list of users along with the groups in which they are located. How can I get the group id of a specific user without creating my own models and not requesting it.

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

+6
source share
5 answers

Ion Auth updated and removed the get_user function in the latest version. As a result of this, the current group identifier of the registered user should be returned below:

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

If you want to get the group ID of a specific user, you can pass the user ID to the get_users_groups method.

+8
source

get object:

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

Get group name:

 $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 offcial doc about user groups:

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

+3
source

I stumbled upon this because I wanted to add the group_id (s) user to the session after a successful login. In case someone is interested, here is how I did it (once I managed to find out where everything is 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 changed 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 a session called groups, which is a list of all the groups the user belongs to.

The only thing I needed to do was change the exit function in Ion_auth.php (in application / libraries) to make sure the group session variable is not set by adding

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

to the list of other unset_userdata () statements.

I know that I probably should have simply expanded the libraries / models to keep the kernel intact, but you could take what I did and it is easy to do.

hope this helps someone.

Rob

+3
source

Try adding this to ion_auth_model.php (Or put the request in another place)

 /** * 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']); } 
0
source

use in 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 use:

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

but what about a query to get groups for the current user (for example, in my profile), I find the following solution. controller: $this->data['this_user_groups'] = $this->ion_auth->get_users_groups()->result(); and just look:

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

Source: https://habr.com/ru/post/1414106/


All Articles