Step 3.3 – Administer users with Ion Auth

This tutorial is part of a larger one, called Create a CMS with CodeIgniter 3. So, if you don’t understand how we’ve got to this point, I would advise you to try and read the previous tutorials. At the end of next tutorial I will post an archive with what we’ve done so far.

Once we’ve finished administering the groups, it’s about time for us to administer the users.

So let’s start by creating a Users.php file inside application/controllers/admin directory:

We will start by working at the list of users. We will display that in the index() method. Also, let’s allow this method to receive a group id so that when this is set the list will only show the users inside that group:

As you can see, to retrieve the users we’ve used the Ion Auth’s users() method. Now let’s create our list_users_view.php file inside application/views/admin/users directory:

As you can see, I didn’t allow the user to delete himself (in most parts of the world the suicide is forbidden :))). If you remember, in a previous tutorial we’ve created $current_user object in the Admin_Controller. Now we’ve accessed this object to verify if the current user is the user in the list. We will not allow him/her to change anything regarding himself. He will be able to change this in the personal profile page.

The create user page

The create() method

The Users’ create() method goes almost the same as the Groups‘ one:

The create_user_view view

Well… it should work…

The edit page

The edit() method

The edit_user_view view

Now that was a looooooong one…

But… we should return to the edit() method because we actually need to do some form validation callbacks for the username and email. We need to make sure that if those are changed by the admins, they do not already exist in the users table. I leave this to you.

The delete page

The delete() method

Cool, finished the the Users controller too.

But let’s return for a moment to the application/views/admin/groups/list_groups_view.php and make the group name clickable so that the links will send to the users of that group:

And that’s it. Next we look at our profile page.

0 thoughts on “Step 3.3 – Administer users with Ion Auth

  • Great tutorial! Thanks for all your great lessons.
    Just a note…maybe there is a missing “)” at line 18 in the list_groups_view.php., to close the first anchor.
    Sorry for that.

  • Hi, Y encountred a one problem to create a new user, in database table not insert a username, but the others things ok and edit the username correctly.

    • Solved. The problem is in Ion_auth_model on $data array

      $data = array(
      $this->identity_column => $identity,
      ‘password’ => $password,
      ’email’ => $email,
      ‘ip_address’ => $ip_address,
      ‘created_on’ => time(),
      ‘active’ => ($manual_activation === false ? 1 : 0)
      change to

      $data = array(
      $this->identity_column => $identity,
      ‘username’ => $identity,
      ‘password’ => $password,
      ’email’ => $email,
      ‘ip_address’ => $ip_address,
      ‘created_on’ => time(),
      ‘active’ => ($manual_activation === false ? 1 : 0)

  • Hello Avenirer! When I try create a new user but I get the error “Field ‘username’ doesn’t have a default value”, I want that the email column is the identity column.

  • I have found one mistake. For example, or return the data of admin.

    I think, reason is that (int) $user_id returns 0 and $this->ion_auth->user((int) $user_id)->row() returns the data of admin. Line 20 in the edit() method.

    Sorry for my bad English. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

No spam? * Time limit is exhausted. Please reload CAPTCHA.