Step 3.3 – Administer users with Ion Auth

Created at: February 4, 2015; Last update: July 20, 2015

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.

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

  1. Federico

    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.
    Ciao

    Reply
  2. aleix

    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.

    Reply
    1. aleix

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

      Reply
    2. avenirer Post author

      That might have happened because you didn’t set “username” as identity column in the configuration file of the Ion Auth.

      Reply
        1. avenirer Post author

          Is the “Username” a field name in your table? Or “username”? Always make sure you know which is which…

          Reply
  3. Pablo

    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.

    Reply
  4. Giorgi

    I have found one mistake. For example, yourdomain.com/admin/users/edit/0 or yourdomain.com/admin/users/edit/sometext 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. 🙂

    Reply

Leave a Reply

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

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