[eluser]Michael Wales[/eluser]
I've had a few requests for the most recent version of ErkanaAuth (the one including the changes listed here in this thread). Well, it doesn't exist, unfortunately.
What I do have is the version of ErkanaAuth that I use within all of my development. I'm down to 3 methods and none of the User Management stuff (roles and such). Here, it is - in all it's glory, with some examples to follow.
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Auth {
var $CI;
function __construct() {
$this->CI =& get_instance();
log_message('debug', 'Authorization class initialized.');
$this->CI->load->database();
}
function try_login($condition = array()) {
$this->CI->db->select('id');
$query = $this->CI->db->getwhere('users', $condition, 1, 0);
if ($query->num_rows != 1) {
return FALSE;
} else {
$row = $query->row();
$this->CI->session->set_userdata(array('user_id'=>$row->id));
return TRUE;
}
}
function logout() {
$this->CI->session->set_userdata(array('user_id'=>FALSE));
}
function get_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;
}
}
}
?>
How I Use It
logout() is still used the same way - a direct call within a controller, then redirect somewhere.
I still use try_login() the same way as well - within a callback function
get_user() is a bit different than what we've seen in the past. I usually extend my Controller class, having all of my classes assign the return value to a variable I can us within my view (for echoing out user info, as needed). For any controllers needing Authorization - I make a valid get_user() return value a requirement.
application/libraries/MY_Controller.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Public_Controller extends CI_Controller {
function Public_Controller() {
parent::Controller();
$this->data->user = get_user($this->session->userdata('user_id'));
}
}
class Admin_Controller extends Public_Controller {
function Admin_Controller() {
parent::Controller();
if ($this->data->user === FALSE) {
redirect('user/login');
return;
}
}
}