@DarkGrave
Thanks for the changes - I will definitely be implementing those in the newest release. I have also made numerous changes to the library since after that first release. Here’s a rundown:
The entire library is now 3 methods: try_login(), logout(), and getUser(). try_session_login() is no more, as is getRule() or getField().
getUser() now returns a user’s record and can be used to determine login status as well as retrieving user information. Right now it doesn’t support roles (so if you use that system, add in a JOIN to the method below) but it will when I actually release this version.
Here’s the code for getUser():
function getUser($id = $this->CI->session->userdata('user_id')) {
if ($id) {
$query = $this->CI->db->getwhere('users', array('users.id'=>$id), 1, 0);
if ($query->num_rows() == 1) {
return $query->row();
} else {
return FALSE;
}
} else {
return FALSE;
}
}
Here’s an example of how I use it:
function __construct() {
parent::Controller();
$this->data->user = $this->auth->getUser();
}
// This function is available to anyone:
function index() {
$this->load->view('index', $this->data);
}
// This function is only available to logged in users
function admin() {
if (!$this->data->user) redirect('');
$this->load->view('admin', $this->data);
}
Adding role information would be really simple with a join in the method (I just don’t have any use for it in my current contract project), but I envision it’s functionality as:
function admin() {
if ($this->data->user->role != 'admin') redirect('');
}
The main reason I took this route: the old getRole queried the database every time it was run. If you had 3-4 places throughout a single page that would change, dependent on user role, you either had to assign getRole() to a variable or you just queried the database a lot. This way is a bit more elegant I think and when do you not want the information about the logged in user?