1

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.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
Osny Netto
  • 562
  • 3
  • 9
  • 28

1 Answers1

4

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.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • sounds like the best solution. – Philip Mar 19 '12 at 04:04
  • I'm still surprised there aren't more authentication libraries available for CI, but then again not that surprised because everyone's needs are different. I prefer to roll my own. I don't like how Tank_Auth reads straight from the session for username, etc. For example if I deactivate a user, I want them booted immediately. – Wesley Murch Mar 19 '12 at 04:05
  • I agree, however like you say everyone has different needs. Personally I dont like CI native AR I prefer to use php-activerecord therefore I needed to roll my own. I think most auth libs are outdated now. – Philip Mar 19 '12 at 04:19
  • thank you. worked! but now I'm using Ion Auth due the advantages. – Osny Netto Mar 19 '12 at 12:23
  • If you use the 'remember me' feature of tank_auth, you will also need to modify the autologin function, where it's sets the session data, add is_admin there too. – James John McGuire 'Jahmic' Sep 29 '13 at 10:11