Easy to use CI Auth System... where are you? - El Forum - 09-10-2009
[eluser]codelearn[/eluser]
Does anyone know of the EASIEST to implement and simplest authentication system built using CI?
Thanks!
Easy to use CI Auth System... where are you? - El Forum - 09-10-2009
[eluser]jedd[/eluser]
[quote author="codelearn" date="1252625059"]
Does anyone know of the EASIEST to implement and simplest authentication system built using CI?
[/quote]
You might need to describe your requirements using slightly less vague terms.
Did you check out the [url="http://github.com/thody/User-Library/tree"]User Library[/url] that thody published?
The general trend - presumably because Simple and Easy pre-built systems tend to not end up being Satisfying - is that most people write their own. A genuinely simple system shouldn't take more than a few days to crank up from scratch.
Easy to use CI Auth System... where are you? - El Forum - 09-10-2009
[eluser]codelearn[/eluser]
Jedd - No I haven't.
All I need:
A registration form
A login form
2 different roles (Admin and User)
A strip of code I can place into controllers that prevents anyone who hasn't logged in from seeing.
This is for a simple project, and really doesn't need anymore than that.
That's it. Anything out there like that?
Easy to use CI Auth System... where are you? - El Forum - 09-10-2009
[eluser]NateL[/eluser]
I've found The Authentication Library by Adam Griffiths to be a fairly simple and easy to use library. For what you say you need, I think it will do the trick for you.
Easy to use CI Auth System... where are you? - El Forum - 09-10-2009
[eluser]bretticus[/eluser]
I kinda felt the same way. I wanted something utterly simple and pretty generic. So easy it was just a stand-alone library in fact. So I wrote one. It's far from perfect, but it is working fine for me on a couple projects. It has no notion of roles but it's so small that it'd be easy to incorporate in any way you need. I don't mind feedback either (tell me my coding follies please.)
*NOTE* for PHP5 only (but could be easily modified to work with PHP4.)
Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*** Brett: Sun May 24 03:10:41 GMT 2009
* A simple portable authentication library. ***/
class Authenticate
{
public $user_table;
public $user_field_identity;
public $user_field_password;
public $user_field_first_name;
public $user_field_last_name;
public $user_field_user_id;
public $referrer;
public $login_uri;
public $first_name;
public $last_name;
public $user_id;
public $query;
private $ci;
private $auth_pfix;
function __construct()
{
// Instantiate the CI libraries so we can work with them
$this->ci =& get_instance();
$this->ci->load->library('session');
$this->ci->load->library('user_agent');
$this->ci->load->helper('url');
$this->ci->load->database();
}
function check_credentials($identity, $password)
{
$this->ci =& get_instance();
if ( !empty($this->query) ) {
$query = $this->ci->db->query($this->query);
} else {
$this->ci->db->where($this->user_field_identity, $identity)
->where($this->user_field_password, $password);
$query = $this->ci->db->get($this->user_table);
}
if ( $query->num_rows() > 0 ) {
$this->ci->session->set_userdata($this->auth_pfix . 'authenticated', TRUE);
$row = $query->row_array();
if ( !empty($this->user_field_user_id) )
$this->ci->session->set_userdata($this->auth_pfix . 'user_id', $row[$this->user_field_user_id]);
if ( !empty($this->user_field_first_name) )
$this->ci->session->set_userdata($this->auth_pfix . 'user_first_name', $row[$this->user_field_first_name]);
if ( !empty($this->user_field_last_name) )
$this->ci->session->set_userdata($this->auth_pfix . 'user_last_name', $row[$this->user_field_last_name]);
return TRUE;
}
return FALSE;
}
function logout()
{
$this->ci->session->set_userdata($this->auth_pfix . 'authenticated', FALSE);
$this->ci->session->set_userdata($this->auth_pfix . 'referrer', '');
}
function set_intended()
{
$referrer = uri_string(); //current url
if ( strpos($referrer, $this->login_uri) === FALSE )
$this->ci->session->set_userdata($this->auth_pfix . 'referrer', $referrer);
}
function is_logged_in()
{
$this->set_intended();
return ( $this->ci->session->userdata($this->auth_pfix . 'authenticated') ) ? TRUE : FALSE;
}
function show_login($url='')
{
$login_uri = ( !empty($url) ) ? $url : $this->login_uri;
redirect( $login_uri );
}
function redirect_to_intended()
{
redirect($this->referrer);
}
function init()
{
$this->auth_pfix = preg_replace('/[^a-z]/i', '_', strtolower($this->login_uri) ) . '_';
$this->referrer = $this->ci->session->userdata($this->auth_pfix . 'referrer');
$this->user_id = $this->ci->session->userdata($this->auth_pfix . 'user_id');
$this->first_name = $this->ci->session->userdata($this->auth_pfix . 'user_first_name');
$this->last_name = $this->ci->session->userdata($this->auth_pfix . 'user_last_name');
}
}
?>
Usage Example:
Code: function __construct() {
parent::Controller();
$this->load->library('authenticate');
// authenticate class settings
$this->authenticate->login_uri = 'reports/login';
$this->authenticate->user_table = 'Users';
$this->authenticate->init();
$this->authenticate->user_field_identity = 'Username';
$this->authenticate->user_field_password = 'Password';
$this->authenticate->user_field_user_id = 'PersonID';
$this->menu_links = array(
'Floor Report'=>'reports/floor',
'Accounting Report'=>'reports/accounting'
);
}
function index() {
if ( $this->authenticate->is_logged_in() ) {
$data = array();
$data['menu_links'] = $this->menu_links;
$this->load->view('reports/index', $data);
} else {
$this->authenticate->show_login();
}
}
function login()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$data = Array();
$this->form_validation->set_rules('username', 'Username', 'required|callback__login_check');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_message('_login_check', 'Your credentials are invalid. Please try again.');
if ($this->form_validation->run() == FALSE) {
$this->load->view('reports/login', $data);
} else {
$this->authenticate->redirect_to_intended();
}
}
function _login_check($username)
{
$password = $this->input->post('password', TRUE);
return $this->authenticate->check_credentials($username, $password);
}
Easy to use CI Auth System... where are you? - El Forum - 09-10-2009
[eluser]Thorpe Obazee[/eluser]
It's actually much easier to implement yours. But if you are looking for a premade one then you could check The Authentication Library by Adam Griffiths.
Easy to use CI Auth System... where are you? - El Forum - 09-10-2009
[eluser]jayrulez[/eluser]
this might interest you http://net.tutsplus.com/videos/screencasts/codeigniter-from-scratch-day-6-login/
|