[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.