Welcome Guest, Not a member yet? Register   Sign In
Ion Auth edit admin - yes, edit user - refresh
#1

I have got the most of Ion Auth working on my site: login/logout, create user/view users. I got adventurous and separated the auth controller into two controllers: auth just contains non-admin functions and a new controller, admin, contains the admin functions. Each controller inherits a separate my_controller that checks for login and admin. I've removed the admin and login checks from the methods in the admin and auth controllers.

If you are with me so far: when I view the Ion Auth users (admin/index), I am able to edit the admin user (admin/edit_user/1), but when I click on any other user (admin/edit_user/2) it simply reloads the view users page (admin/index)

The problem seems to occur here:

if (!($this->ion_auth->user()->row()->id == $id))
{
redirect('admin', 'refresh');
}

$id equals 1, 2, or 3 depending on who I click on to edit
ion_auth->user()->row()->id   always equals 1    (which is equal to the admin user id)

Any advice would be much appreciated.
Reply
#2

Why would you use the "auth controller" (although there is no auth controller but only an example of usage for the Ion Auth library) to create two controllers? Secondly, what is your question in there? You ask why you can't edit other users? the code lines tell you from the start: you can edit only your user. If you want to edit everyone else, you can use the is_admin() method. So, instead of writing what you've wrote, you can write:

PHP Code:
if (!$this->ion_auth->is_admin())
{
 
 redirect('admin','refresh');

Reply
#3

By the time this code is reached the parent controllers have already checked whether the user has logged in and is admin (thanks to your tutorial: no more my_controller). The admin is viewing a list of users and clicks on edit which calls the controller and function with the $id of that user:

function edit_user($id)
{
if (!$this->ion_auth->user()->row()->id == $id)
{
redirect('auth', 'refresh');
}
...

I've got it working now.

Your first question is something I have been pondering (as an MVC newbie): should I use one controller for each function (Eg. delete user, view users, etc.,) or does it ever make sense to group closely related functions into a single controller?
Reply
#4

It's not a good idea to have so many controllers for every single action in your application. Considering this Ion Auth thing, I did a tutorial about administering users using this library and just like you I created two controllers, one for the average user (User.php), and one for the administrator (Users.php), and in there I've grouped everything. In the parent controller I only made sure that these pages can only be accessed by the logged in users. Also, in the Users.php, inside the constructor, I've made sure that the users accessing that controller is admin an not any logged in user. Among the reasons for grouping related functions in a single controller I think you can enumerate the following:

- You can load a model for all the functions inside that controller (inside the constructor)
- Also, if you think you will have heavy usage of some libraries or helpers you can load them once in the constructor and forget about them.
- You won't have to deal with redirections. If you use a single method per controller, and you will have parameters passed to it you would have to create a route redirection toward that particular method.
- You can at any time reuse some methods that are used inside that particular controller.

Beside grouping methods inside controllers, I also find useful to group views that are required by a controller. For example, for a User.php controller, I create a "user" directory inside the "views" folder, and put all those views in there.

I sure hope you enjoy CodeIgniter. Is the best framework especially for those that start to learn about MVC frameworks (and even for those that start learning about OOP).
Reply
#5

(This post was last modified: 04-28-2015, 08:22 PM by Shawn.)

Thanks for your advice! The framework has helped me get up and running very quickly.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB