ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

ErkanaAuth: A non-invasive user authentication library

October 23, 2007 9:58pm

Subscribe [37]
  • #76 / Jan 21, 2008 10:54am

    Kemik

    162 posts

    Is it worth me starting to use this now or will you be releasing the edited/updated version soon?

    Thanks

  • #77 / Jan 21, 2008 2:00pm

    Michael Wales

    2070 posts

    Kemik: If you want to use the latest version, that I use on all of my projects, see this post.

    I don’t see where I will be performing any more updates to this library - it works perfectly for me and I can’t think of anything else I would want in it.

  • #78 / Jan 28, 2008 1:54pm

    hotmeteor

    29 posts

    Hey Michael

    This is a fantastic library. However, I’m having some bizarre issue where it won’t load the library?! I’ve copied and pasted both your auth.php library and your MY_Controller.php, but I keep getting this error:

    Fatal error: Call to undefined function get_user() in <my-application-location>/system/application/libraries/MY_Controller.php on line 6

    Any ideas? I’ve tried both auto-loading it as well as calling it in place.

  • #79 / Jan 28, 2008 4:08pm

    Michael Wales

    2070 posts

    Ensure that the function is in-fact called get_user() within auth.php. There are some versions floating around that use getUser() - I renamed this function later on to comply with the EE development standards.

    I’ll be posting a complete package of my standard development suite on my blog tonight - which will include the following:
    - Asset Helper
    - xHTML Helper
    - Auth Library
    - MY_Validation
    - MY_Controller

  • #80 / Jan 29, 2008 1:08am

    Michael Wales

    2070 posts

    As promised, my CodeIgniter Development Pack has been posted.

  • #81 / Jan 30, 2008 5:05pm

    hotmeteor

    29 posts

    This is great, and very helpful. Thanks Michael.

    One fix I think:

    Line 31, should read:

    parent::Public_Controller();

    Inside the Auth_Controller function. Wouldn’t work for me until I added that.

  • #82 / Feb 02, 2008 7:55pm

    cheshirecat

    4 posts

    Hi!
    I’m using Erkanaauth for a few days and it’s great - great in its simplicity.
    But I have a small trouble in try_login(). I’m not good in PHP yet. I’ve made my auth methods similar to the ones found in this forum. There are two ways:

    //first
    $this->erkanaauth->try_login(array('username' => $username, 'password' => $password));
    
    //and the 2nd
    $this->erkanaauth->try_login(array('username' => $username), array('password' => $password));

    And that’s surprising me they both are not working good. In the first case the login form always redirects me to the login form - not to the admin panel, none the less username and password are good.
    In the 2nd case the library checks only username - you can write everything as the password and it says it is correct. So it is not very secured 😉

    Can you please tell me how it should look like?

    My admin class looks like this:

    class Admin extends Controller
    {
    function Admin()
            {
            parent::Controller();
            $this->response = array();
            $this->load->library('Erkanaauth');
        if (!$this->erkanaauth->try_session_login() and $this->uri->segment(2) != 'login') 
            {
            redirect('admin/login');
            }
            }
    
    function login()
     {
     $content = '';
     $rules['login'] = "required|max_length[15]|xss_clean";
     $rules['passwd'] = "required|max_length[15]|xss_clean";
     $this->validation->set_rules($rules);
     
     if ($this->validation->run() == FALSE)
        {
           $content .= $this->load->view('login', NULL, True);
        }
     else
        {
            $this->load->helper('security');
            $username = $this->input->post('login');
            $password = dohash($this->input->post('passwd'));
            if (!$this->erkanaauth->try_login(array('username' => $username), array('password' => $password)))
                {
                redirect('admin/login');
                }
            else
                {
                redirect('admin/index');
                }
        }
        $this->response['content'] = $content;
        $this->load->view('index', $this->response);
        }
    
    //and then some other methods
    }
  • #83 / Feb 05, 2008 5:50am

    AmazingMinds

    2 posts

    I’ve just started using the newest version of the Authlib (and CI for that matter) and though it’s wickedly versatile, I am stumbling over something, which is prolly CI related..

    I’m using a simple form in my view

    <?echo $userName;?>
    
    <form method="post">
    Login: <input type="text" size="15" name="username">
    
    Password: <input type="password" size="15" name="password">
    
    <input type="submit" name="login" value="login">
    </form>

    Login stuff is processed by the following controller:

    <?php
    
    class Main extends Controller {
    
        function Main()
        {
            parent::Controller();
            
            // load libraries
            $this->load->library('session');
            $this->load->library('auth');
        }
        
    
        function index()
        {    
            if ($this->input->post('login')) 
            {
                $username = $this->input->post('username');
                $password = md5($this->input->post('password'));
                $this->_check_login($username, $password);
            }
            // get userinfo
            if ($this->session->userdata('user_id') !== FALSE) 
            { 
                      $user = $this->auth->get_user($this->session->userdata('user_id')); 
                  $data['userName'] = $user->userName;        
            } else {
                $data['userName'] = 'Guest';
            }
            // load views
            $this->load->view('header', $data);
            $this->load->view('body', $data);
            $this->load->view('footer', $data);
        }
        
        
        function _check_login($username, $password) {
              $this->load->helper('security');
              $this->load->library('validation');
              if ($this->auth->try_login(array('userName'=>$username, 'password'=>$password))) {
                return TRUE;
              } else {
                $this->validation->set_message('_check_login', 'Incorrect login info.');
                return FALSE;
              }
        } 
    
    }
    ?>

    Now, when I fill in the login/password combo and click login, the login is granted. However, it’s not until I refresh the page (both in IE and FF) that the session-id is provided and I’m actually logged in. I’m getting the feeling there’s something really basic that I’m doing wrong, but I just seem to miss it…

  • #84 / Feb 07, 2008 9:34am

    swanky

    9 posts

    Great library!  Thank you, it is just what I was looking for.  I have a decidedly minor improvement that I thought I’d share.  I wanted to be able to choose the redirect page on a case by case basis so I made a minor edit change to the My_Controler.php code.

    // This function is used to prevent a user from accessing a method if they are logged in
    function _no_user_access($view = '') {
      if ($this->data->user !== FALSE) {
        redirect($view);
      }
    }

    So now I can put this

    $this->_no_user_access('logout');

    or

    $this->_no_user_access('super_cool_members_only_page');

    into my functions.  Or just ignore the view variable altogether for a default redirect to the home page.

    Hope someone else can use it too.

  • #85 / Mar 27, 2008 7:03pm

    greendrago

    1 posts

    Here is a cut and paste version of erkanaauth

    /libraries/erkanaauth.php

    <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
    /*****
    * ErkanaAuth is an easy to use, non-invasive, use authentication library
    * @author     Michael Wales
    * @email      .(JavaScript must be enabled to view this email address)
    * @filename   auth.php
    * @title      ErkanaAuth
    * @url        <a href="http://www.michaelwales.com/">http://www.michaelwales.com/</a>
    * @version    2.0
    *****/
    class Erkanaauth {
    
        var $db_table = 'tblUser';
        var $db_userid = 'id';
        
        var $CI;
    
        function __construct() {
            $this->CI =& get_instance();
            log_message('debug', 'Authorization class initialized.');
    
            $this->CI->load->database();
        }
    
        /***
        Determines whether the passed condition is valid to login a user, if so - sets a session variable containing the user's ID
        * @param    $condition array    The condition to query the database for
        * @return   boolean
        ***/
        function try_login($condition = array()) {
            $query = $this->CI->db->getwhere($this->db_table, $condition, 1, 0);
    
            if ($query->num_rows != 1) { return FALSE; }
    
            $row = $query->row();
            $this->CI->session->set_userdata(array('user_id' => $row->ID, 'call_user' => $row->Login));
    
            return $row;
        }
    
    
        /***
        Returns an object containing user information via the user's ID
        * @param    $id integer         The user's ID
        * @return   object              Upon valid user
        * @return   FALSE               Upon invalid user
        ***/
        function get_user($id = FALSE) {
            if ($id == FALSE) $id = $this->CI->session->userdata('user_id');
            if ($id == FALSE) return FALSE;
    
            $condition = array(($this->db_table . '.' . $this->db_userid) => $id);
    
            $query = $this->CI->db->getwhere($this->db_table, $condition, 1, 0);
    
            $row = ($query->num_rows() == 1) ? $query->row() : FALSE;
    
            return $row;
        }
    
        /***
        Logs out a user by deleting their session variables
        * @return   null
        ***/
        function logout() {
            $this->CI->session->set_userdata(array('user_id' => FALSE));
        }
    }

    and /libraries/MY_Controller.php

    <?php if (!defined('BASEPATH')) exit('No direct access allowed.');
    /*****
      * This class provides a set of base Controller classes to be utilized with ErkanaAuth
      * @author     Michael Wales
      * @email      .(JavaScript must be enabled to view this email address)
      * @filename   MY_Controller.php
      * @title      ErkanaAuth Controller Library
      * @url        <a href="http://www.michaelwales.com/">http://www.michaelwales.com/</a>
      * @version    1.0
      *****/
    
    // Controllers accessible by everyone, regardless of login status
    class Public_Controller extends Controller {
        function Public_Controller() {
            parent::Controller();
            // Get the user data, in case they are logged in
            $this->data->user = $this->erkanaauth->get_user($this->session->userdata('user_id'));
        }
        
        // This function is used to prevent a user from accessing a method if they are logged in
        function no_user_access() {
            if ($this->data->user !== FALSE) {
                redirect('');
            }
        }
    }
    
    // Controllers only accessible by logged in users
    class Auth_Controller extends Public_Controller {
        function Auth_Controller() {
            parent::Public_Controller();
    
            //- $this->data->user = $this->erkanaauth->get_user($this->session->userdata('user_id'));
            if ($this->data->user === FALSE) {
                redirect();
                return;
            }
        }
    }
    
    // Controllers only accessible to logged in users that are admins
    class Admin_Controller extends Public_Controller {
        function Admin_Controller() {
            parent::Public_Controller();
    
            if (($this->data->user === FALSE) || (strpos($this->data->user->Roles, 'admin') === FALSE)) {
                redirect();
                return;
            }
        }
    }

    and 2 examples

    Class Ct2 extends Public_Controller {
    
        function Ct2() {
            parent::Public_Controller();
    
    ......

    ———-

    <?
    class Admin extends Admin_Controller {
    
      function __construct() {
            parent::Admin_Controller();
      }
    
      function index() {
        $this->load->view('admin/cpanel');
        return;
      }
    }
    ?>
  • #86 / Apr 09, 2008 2:53pm

    halex

    7 posts

    Excuse a noob (in CI and php) question:
    I have:

    class Comm extends Auth_Controller
    {
      function Comm()
      {
        parent::Auth_Controller();
     ...

    I get a

    Call to undefined function redirect() in ...\application\libraries\MY_Controller.php on line 35

    In the log file I have:

    ......
    DEBUG - 2008-04-09 20:35:59 --> Global POST and COOKIE data sanitized
    DEBUG - 2008-04-09 20:35:59 --> Language Class Initialized
    DEBUG - 2008-04-09 20:35:59 --> Loader Class Initialized
    DEBUG - 2008-04-09 20:35:59 --> Authorization class initialized.
    DEBUG - 2008-04-09 20:35:59 --> Database Driver Class Initialized
    DEBUG - 2008-04-09 20:35:59 --> Session Class Initialized
    DEBUG - 2008-04-09 20:35:59 --> Controller Class Initialized

    When I change Auth_ to Public_ all is OK.

  • #87 / Apr 09, 2008 2:57pm

    halex

    7 posts

    I’m stupid, ignore my last post.
    All is clear and OK.

  • #88 / Apr 28, 2008 12:03pm

    RunningDan

    3 posts

    What is $this->data?

    $this->data->user

    I get this error when I request a controller that inherits Auth_controller:

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined property: Test::$data

    Filename: libraries/MY_Controller.php

    Line Number: 31

    Line 31 is in here:

    class Auth_Controller extends Public_Controller {
        function Auth_Controller() {
            ]if ($this->data->user === FALSE) {
                // The user is not logged in, send them to the homepage
                redirect('');
            }
        }
    }

    I don’t understand what data in this context?

    My controller looks like this:

    <?
    class Test extends Auth_Controller {
        
        function Test() {
            parent::Auth_Controller();
        }
        
        function index()
        {    
            $data['title'] = "Test protected page title";        
            $this->load->view('test', $data);
        }    
    }
    ?>

    Thanks

  • #89 / Apr 29, 2008 4:02pm

    popthestack

    1 posts

    Line 31 is in here:

    class Auth_Controller extends Public_Controller {
        function Auth_Controller() {
            ]if ($this->data->user === FALSE) {
                // The user is not logged in, send them to the homepage
                redirect('');
            }
        }
    }

    Open up your MY_Controller.php file and make the following changes:

    The Auth_Controller class is missing this line in its constructor:
    parent::Public_Controller();

    So it should be this:

    class Auth_Controller extends Public_Controller {
        function Auth_Controller() {
            parent::Public_Controller();
            if ($this->data->user === FALSE) {
                // The user is not logged in, send them to the homepage
                redirect('');
            }
        }
    }

    I wasn’t entirely sure about this next one, but I haven’t noticed any repercussions as a result (but I haven’t exactly used this library very much yet).

    The Admin_Controller class has:

    parent::Controller();

    Instead of:

    parent::Public_Controller();
  • #90 / Apr 30, 2008 5:28am

    RunningDan

    3 posts

    popthestack, thanks very much!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases