Step 3.2 – Administer groups with Ion Auth

Created at: February 4, 2015; Last update: August 11, 2015

Ion Auth library organizes users in groups. In this tutorials we will create a group admin controller. First of all, let’s modify the top menu that can be found inside application/templates/_parts/admin_master_header_view.php so that if the user is an administrator, he/she should see some more links:

But wouldn’t that mean that we’ve moved the decisional part of MVC inside the View? You are right. We should compose the the menu in the Admin_Controller (if we go to extreme we should make the menu inside a model…) and only output it inside the View.

So let’s go to our Admin_Controller.php and create a different user menu depending on the group that the user is in:

I also passed the current user’s data inside a variable called current_user, so that in the views it will be easier to access that data.

Now, we create the user_menu_admin_view.php inside the application/templates/_parts directory:

And that’s it.

Now let’s create the Groups page. To do this we will create a new controller file named Groups.php inside application/controllers/admin, and first we make sure that only the admins have access to this controller. If the user is not part of admin group he will be redirected to the dashboard and announced that he/she is not allowed on the groups page:

Let’s put the announcement just after the top menu. We open application/templates/_parts/admin_master_header_view.php and just after the closing </nav> tag we echo the message:

Now, inside Groups.php let’s just try and return a list with the available groups:

In the view (admin/groups/list_groups_view.php) we create the table:

“admin” and “members”, being the default groups, should not have a delete link. So, let’s remove it:

Now, let’s return to our Groups controller, by making three new methods: create(), edit() and delete().

The groups/create page

The create() method:

The create_group_view view:

The groups/edit page

The edit() method:

Maybe this one needs a little explanation: if the run() method of the form_validation library returns true, then everything is a go. Else if we get the group data from the ion_auth library by passing the id of the group from the URL, we setup the form with the group data. Else, there is no group data, and that id doesn’t exist, so we redirect.

The edit_group_view view:

The groups/delete “page”

Well… this one is not really a page… it’s only a method that will redirect to the groups page after it does its job:

And that’s it… Next we will see how we can administer the users using Ion Auth library.

21 thoughts on “Step 3.2 – Administer groups with Ion Auth

  1. Edward

    Is there anywhere to see the finished work? To catch any errors. I was having problem with session. Also, I’m getting menus listed on the left side of the page. Somewhere I got the impressions the menu was horizontal at the top of the page.

    Reply
  2. Riza

    It all almost works for me thank you..
    I still have a problem, when I go to ‘admin/groups/create’ page
    and try to click the administrator menu, it won’t show the dropdown list.. why..?

    Reply
  3. Federico

    Great tutorial..as usual. I’m following all your tutorial to build my new project, Learning CI and OOP. Keep up the great work.
    Just a litte note (sorry for that..hope you don’t mind and maybe I’m wrong): in the create() method I’ve had to remove the following code at row 21:
    $this->render(‘admin/groups/create_group_view’);
    or my body page is printed twice on browser
    Thanks

    Reply
  4. no name

    Hi, in the code of the groups/delete “page” the function called for deleting a group is “delete_group” instead of “delte_group” (mistake in code example row no. 9 😉
    Great tutorial !!! =)

    Reply
  5. nanart

    Hello,
    Just a note:
    we can’t delete groups whose name is ‘admin’ or ‘ members’.
    But…. if we rename them: we can now do it !
    So I see two possible solutions:
    1) does not allow to edit the name of these groups
    or
    2 ) allow rename them, but do not let delete groups with id 1 and 2.
    It’s not ideal, but it is a additional precaution, isn’t it?

    Reply
    1. avenirer Post author

      Indeed. You are very right. “admin” and “members” groups are mentioned inside the configuration file, which means that any change on their name will affect the library unless you do the same changes to the configuration file. Thank you for pointing that out.

      Reply
  6. Tony Jifkins

    Loving your tuts, they are the best I have come across so far and I’ll definitely buy you a coffee once I’ve finished. As I’m fairly new to php and CI, can you explain two things for me though in the edit() method?
    1. NetBeans IDE is giving me a message on line 3 about dual assignments?
    2. also on line 13 NetBeans is saying, accidental assignment in a condition, I’m assuming it has to do with ((int) $group_id).
    Everything works so far with the tuts, I would just like some clarification on what the two lines mean.

    Thanks
    Tony

    Reply
    1. avenirer Post author

      I am not using NetBeans…
      1. I do not know what dual assignments mean; I do not know what the third line is 🙂
      2. What you assume is somewhat wrong. That is in fact named “typecasting” of a variable (we are making sure that we are sending an integer).
      “Accidental assignment in a condition”? Is not accidental 🙂 At least in PHP, if inside an “if” statement we put an assignment, the “if” will either receive a true or a false depending on the assignment. What I mean by this is that if “$this->ion_auth->group((int) $group_id)->row()” would have returned FALSE (the model returns false if there is no group with that ID), the “if” condition would have not been ran.

      You’re welcome. And thank you for the coffee in advance 🙂

      Reply
      1. Tony Jifkins

        Thanks for the quick response. Cool so I understand the typecasting, the third line reads:
        $group_id = $this->input->post(‘group_id’) ? $this->input->post(‘group_id’) : $group_id;
        I’m unsure of what this means.

        Thanks again

        Tony

        Reply
        1. avenirer Post author

          That is a ternary operator. It’s some sort of shorter version of an if-else statement. it translates to (is it true that…) ? (do this if true) : (do this if false);

          In that particular line I am asking “is it true that there exists a post data value for “group_id”?”. If it’s true, return the post data, else use the $group_id variable. After the check was done, and a value was returned, that value is assigned to $group_id. You may find this line to to be superfluous, as there is no sane person that will change the url when using a form, but… who knows?… Is better not to trust the users’ sanity 😉

          Reply
  7. Liberal

    Hi,

    First of all, thanks. Your tutorials are the best, they help me a lot lerning codeigniter.
    I use a few cases in a project i have in hands. Now i like to ask something, if possible, you could explain how to use HMVC with the templates structure you have implemented here?

    Many thanks.

    Kind regards,
    Liberal

    Reply
  8. /tede

    Hi, thanks for the great tut, really appreciated!

    in this section i found 3 typos – maybe you want to amend it:

    1) you make changes to Admin_Controller.php – it should be MY_Controller.php
    2) a few lines below that the path to views is missing the “views” part 🙂
    3) moving the decisional code from admin_master_header_view.php into MY_Controller.php could maybe improved as in this case the master controller actually builds the menue…i would use a dedicated piece of code for that. either way – there is a mistake as you don’t amend the admin_master_header_view.php after moving the code – it should at lease include

    Hoping for many more of your great tuts!
    /tede

    Reply
  9. neha gosavi

    Hello,Great tutorial,
    But I can edit my groups,it is returning with the message ‘The group doesn’t exist.’
    why?

    Reply

Leave a Reply

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

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