Welcome Guest, Not a member yet? Register   Sign In
Easy to use CI Auth System... where are you?
#5

[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);
    }


Messages In This Thread
Easy to use CI Auth System... where are you? - by El Forum - 09-10-2009, 12:24 PM
Easy to use CI Auth System... where are you? - by El Forum - 09-10-2009, 12:41 PM
Easy to use CI Auth System... where are you? - by El Forum - 09-10-2009, 12:47 PM
Easy to use CI Auth System... where are you? - by El Forum - 09-10-2009, 01:46 PM
Easy to use CI Auth System... where are you? - by El Forum - 09-10-2009, 02:25 PM
Easy to use CI Auth System... where are you? - by El Forum - 09-10-2009, 08:42 PM
Easy to use CI Auth System... where are you? - by El Forum - 09-10-2009, 09:41 PM



Theme © iAndrew 2016 - Forum software by © MyBB