Search code examples
phpcodeigniterion-auth

Updating group in codeigniter ion auth


I am new to codeigniter ion auth and applied it in a project. Everything works fine by following the documentation. While exploring, I noticed that when I update/edit any user data, it throws no error but the users_groups not updated.

Code:

$id = $this->input->post('id');

$data = array(
  'first_name' => $this->input->post('first_name'),
  'last_name' => $this->input->post('last_name'),
  'group' => array($this->input->post('group_id')),
  'active' => $this->input->post('active')
  );

$result = $this->ion_auth->update($id, $data);

Any help would be much appreciated. Thanks!


Solution

  • With ion_auth->update method you can update only the user attributes stored in the users table, you cannot modify the user group.
    (internally it queries the columns from users table and update only those parameters which are valid attributes of the user)
    To modify user group you need to do:

    $user_id = 123;
    $group_id = 3;// let's say 3 is the ID of the 'publisher' user group
    
    // to remove the user (#ID:123) from the 'publisher' group call this:
    $this->ion_auth->remove_from_group($group_id, $user_id);
    // to remove the user (#ID:123) from all of the assigned groups call this:
    $this->ion_auth->remove_from_group(false, $user_id);
    
    // to add the user (#ID:123) to the 'publisher' group call this:
    $this->ion_auth->add_to_group($group_id, $user_id);