1
votes

I'm using Tank Auth and my project will support two access type: "normal user" and "admin". I looked at the tank auth database and there is a "isadmin" field, so I believe that it's possible redirect to /admin or /user when I log in to my system.

How can I do this? I only see is_logged_in().

I already saw this: http://codeigniter.com/forums/viewthread/207245/ but I want only a redirect to /user or /admin URL depending if admin or not.

1

1 Answers

4
votes

Unfortunately, Tank_Auth doesn't come with any kind of user group or permission features built in. I'm not sure where you saw a field called isadmin because it doesn't seem to be part of the current default setup and I couldn't even find the word "admin" in the package files.

This is the easiest solution I can think of:

Add the is_admin field you mentioned to the users table and manage it manually.

ALTER TABLE `users`
ADD COLUMN `is_admin` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0';

Working with the way Tank_Auth currently does, you'll be reading the user data from the session, so you'll have to add it to the session on login.

// Tank_Auth.php Line 71
$this->ci->session->set_userdata(array(
    'is_admin'  => (bool) $user->is_admin, // added
    'user_id'   => $user->id,
    'username'  => $user->username,
    'status'    => ($user->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED,
));

Then add your own function to the library:

function is_admin()
{
    return $this->logged_in() && $this->ci->session->userdata('is_admin') === TRUE;
}

Then you'll have to add this to the login controller for the redirect upon login:

// controllers/auth.php Line 30
function login()
{
    if ($this->tank_auth->is_admin()) {
        redirect('admin');
    } elseif ($this->tank_auth->is_logged_in()) {
        redirect('user');
    }
    // Rest of code stays untouched...
}

Then have your admin controller always check for $this->tank_auth->is_admin().

This is the shortest route I can offer to achieve your goals - probably not the best, but you might be interested in Ion_Auth which does handle user groups by default.