Welcome Guest, Not a member yet? Register   Sign In
Create an authentication library on 'Build a CMS in CodeIgniter' tutsplus
#1

[eluser]Lykos22[/eluser]
Hi I'd like some feedback please. I 'm quite new to CodeIgniter and I have been recently studying the tutorial Build a CMS in CodeIgniter on tutsplus and following it step-by-step. This is the part I'm most interested in:
Code:
<?php
class User_M extends MY_Model
{

protected $_table_name = 'users';
protected $_order_by = 'name';
public $rules = array(
  'email' => array(
   'field' => 'email',
   'label' => 'Email',
   'rules' => 'trim|required|valid_email|xss_clean'
  ),
  'password' => array(
   'field' => 'password',
   'label' => 'Password',
   'rules' => 'trim|required'
  )
);
public $rules_admin = array(
  'name' => array(
   'field' => 'name',
   'label' => 'Name',
   'rules' => 'trim|required|xss_clean'
  ),
  'email' => array(
   'field' => 'email',
   'label' => 'Email',
   'rules' => 'trim|required|valid_email|callback__unique_email|xss_clean'
  ),
  'password' => array(
   'field' => 'password',
   'label' => 'Password',
   'rules' => 'trim|matches[password_confirm]'
  ),
  'password_confirm' => array(
   'field' => 'password_confirm',
   'label' => 'Confirm password',
   'rules' => 'trim|matches[password]'
  ),
);

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

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

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

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

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

public function hash ($string)
{
  return hash('sha512', $string . config_item('encryption_key'));
}
}

AND

<?php
class User extends Admin_Controller
{

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

        ...

public function login ()
{
  // Redirect a user if he's already logged in
  $dashboard = 'admin/dashboard';
  $this->user_m->loggedin() == FALSE || redirect($dashboard);
  
  // Set form
  $rules = $this->user_m->rules;
  $this->form_validation->set_rules($rules);
  
  // Process form
  if ($this->form_validation->run() == TRUE) {
   // We can login and redirect
   if ($this->user_m->login() == TRUE) {
    redirect($dashboard);
   }
   else {
    $this->session->set_flashdata('error', 'That email/password combination does not exist');
    redirect('admin/user/login', 'refresh');
   }
  }
  
  // Load view
  $this->data['subview'] = 'admin/user/login';
  $this->load->view('admin/_layout_modal', $this->data);
}

public function logout ()
{
  $this->user_m->logout();
  redirect('admin/user/login');
}

public function _unique_email ($str)
{
  // Do NOT validate if email already exists
  // UNLESS it's the email for the current user
  
  $id = $this->uri->segment(4);
  $this->db->where('email', $this->input->post('email'));
  !$id || $this->db->where('id !=', $id);
  $user = $this->user_m->get();
  
  if (count($user)) {
   $this->form_validation->set_message('_unique_email', '%s should be unique');
   return FALSE;
  }
  
  return TRUE;
}
}

What I'd like to do is to try to abstract the login, logout and all other functions that has to do with authentication from the user model and write it on a seperate simple authentication library, just for further educational knowledge, but I'm not quite sure on how I should do it, basicly how should I seperate all functions from user model and make them more generic in order to create a library. Do I have to make the table name dynamic, how to fetch user data and join them with the cms application (e.g. a message saying 'Welcome Admin'), include the session class inside my library or not and some more stuff like that.



Messages In This Thread
Create an authentication library on 'Build a CMS in CodeIgniter' tutsplus - by El Forum - 09-16-2013, 12:50 AM



Theme © iAndrew 2016 - Forum software by © MyBB