Welcome Guest, Not a member yet? Register   Sign In
Creating a custom Authentication library
#1

[eluser]Lykos22[/eluser]
Hi I'd like some information and help please. I'm quite new to CodeIgniter (although I have a good knowledge of php) and I'd like to create a simple custom authentication library, for some small projects at my college, with the potential to expand it. I have done some research about it and found some other libraries to get some ideas about the structure, but the truth is that I got a little bit lost, as there are developed with different ways, so I hope that I could find some good feedback over here.

What I'm thinking of is that the authentication and authorization will be on an independent library in order to abstract it from the users, customers or admin logic (user_model - user_controller, customer_model - customer_controller etc etc) but I'm having a difficulty on designing how to code this.
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

Generally, folks put things like this into a library, and have it communicate with a model. Then you can call the library from whichever controller you want to. Where exactly are you having difficulty?
#3

[eluser]Lykos22[/eluser]
Well I want to create a library that can be adaptive, for example I want my library to adapt to a user_model (like this one below), or to a customer_model or to a admin_model, meaning that my library will hold all the authentication & authorization of my users, customers, administrators, moderators, authors etc etc. The difficulty I'm having is how can I abstract it -meaning how will I pass each time the appropriate db table- and how should I make my functions adaptable, for example in case of register() function the users might have different values than customers or administrators etc etc

Code:
<?php
class User_model extends MY_Model // MY_Model holds all generic CRUD
{

protected $_table_name = 'users'; // extended from MY_Model
  protected $_order_by = 'user_id';
  public $rules = array(
                                        'name' => array(
          'field' => 'name',
          'label' => 'Name',
          'rules' => 'trim|required|xss_clean'
         ),
      'email' => array(
          'field' => 'email',
          'label' => 'E-mail',
          'rules' => 'trim|required|valid_email|xss_clean'
         ),
      'password' => array(
          'field' => 'password',
          'label' => 'Password',
          'rules' => 'trim|required'
         )
      );

  function __construct()
  {
   parent::__construct();
  }


  // put all the authentication below in a seperate library

  public function login(){
   $user = $this->get_by(array(
         'email' => $this->input->post('email'),
         'password' => $this->do_the_hash($this->input->post('password'))
         ));
   if (count($user)) {
    // log the user in
    $data = array(
     'name' => $user->name,
     'email' => $user->email,
     'id' => $user->id,
     'logged_in' => TRUE
     );
    $this->session->set_userdata($data);
   }
  }

  public function logout(){
   $this->session->sess_destroy();
  }

  public function is_logged_in(){
   return (bool)$this->session->userdata('logged_in');
  }

  public function do_the_hash($string){
   return sha1($string);
  }

  public function get_new(){
   $user = new stdClass();
   $user->name = '';
   $user->email = '';
   $user->password = '';

   return $user;
  }
}
?>

Quote:Generally, folks put things like this into a library, and have it communicate with a model. Then you can call the library from whichever controller you want to.
- I've seen some libraries that have the authentication inside libraries folder and all the crud in an authentication model. Is this what you were refering to??? Will I always call my library though controllers or through user models??
- As you see in my code above I 've created a MY_Model inside application/core that holds all my crud by defininging the table name each time and get the job done. In this case how could I pass the appropriate db table (choose between users - customers - admins etc etc depends on my database)?

I'd be grateful if you could give me some guidelines please.




Theme © iAndrew 2016 - Forum software by © MyBB