Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.62 KB | None | 0 0
  1. <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Auth_mod extends CI_Model
  4. {
  5.     /**
  6.      * all database tables
  7.      *
  8.      * @var array
  9.      */
  10.     private $tables = array(
  11.                         'users' => 'users',
  12.                         'cookies' => 'cookies',
  13.                         'forgotten_passwords' => 'forgotten_passwords',
  14.                         'users_roles' => 'users_roles'
  15.                     );
  16.    
  17.     /**
  18.      * construct function
  19.      */
  20.     public function __construct()
  21.     {
  22.         parent::__construct();
  23.    
  24.         $this->load->database();
  25.     }
  26.    
  27.     /**
  28.      * create a new user
  29.      *
  30.      * @param string $name
  31.      * @param string $email
  32.      * @param string $password
  33.      */
  34.     public function create_user($name, $email, $password, $role)
  35.     {
  36.         $email = trim($email);
  37.        
  38.         if ($this->check_email($email)) return FALSE;
  39.        
  40.         $name = trim($name);
  41.         $password = sha1(trim($password));
  42.        
  43.         $this->db->insert($this->tables['users'], array('name' => $name, 'email' => $email, 'password' => $password));
  44.        
  45.         $id = $this->db->insert_id();
  46.        
  47.         $this->create_user_role($id, $role);
  48.     }
  49.    
  50.     /**
  51.      * create user role
  52.      *
  53.      * @param integer $user_id
  54.      * @param integer $role
  55.      */
  56.     public function create_user_role($id, $role)
  57.     {
  58.         $this->db->insert($this->tables['users_roles'], array('id' => $id, 'role' => $role));
  59.     }
  60.    
  61.     /**
  62.      * login the user
  63.      *
  64.      * @param string $email
  65.      * @param string $password
  66.      *
  67.      * @return object|FALSE
  68.      */
  69.     public function login($email, $password)
  70.     {
  71.         $email = trim($email);
  72.         $password = sha1(trim($password));
  73.        
  74.         $query = $this->db->select('id, name, email, last_login')->from($this->tables['users'])->where(array('email' => $email, 'password' => $password, 'active' => TRUE))->get();
  75.    
  76.         if ($query->num_rows() === 1)
  77.         {
  78.             return $query->row();
  79.         }
  80.    
  81.         return FALSE;
  82.     }
  83.    
  84.     public function get_user_role($id)
  85.     {
  86.         $query = $this->db->select('role')->from($this->tables['users_roles'])->where('id', $id)->get();
  87.        
  88.         if ($query->num_rows() > 0)
  89.         {
  90.             return $query->result_array();
  91.         }
  92.        
  93.         return FALSE;
  94.     }
  95.    
  96.     /**
  97.      * auto login the user for "remember me"
  98.      *
  99.      * @param integer $id
  100.      */
  101.     public function auto_login($cookie_value)
  102.     {
  103.         $query = $this->db->select('id, name, email, last_login')
  104.                 ->from($this->tables['users'])
  105.                 ->join($this->tables['cookies'], $this->tables['cookies'] . '.user_id = ' . $this->tables['users'] . '.id AND ' . $this->tables['cookies'] . '.value = ' . $cookie_value)
  106.                 ->where('active', TRUE)
  107.                 ->get();
  108.    
  109.         if ($query->num_rows() === 1)
  110.         {
  111.             return $query->row();
  112.         }
  113.    
  114.         return FALSE;
  115.     }
  116.    
  117.     /**
  118.      * update user last login
  119.      *
  120.      * @param integer $id
  121.      */
  122.     public function update_last_login($id)
  123.     {
  124.         $this->db->where('id', $id)->update('last_login', CURRENT_TIMESTAMP);
  125.     }
  126.    
  127.     /**
  128.      * check if email is avaiable
  129.      *
  130.      * @param string $email
  131.      *
  132.      * @return boolean
  133.      */
  134.     public function check_email($email)
  135.     {
  136.         $email = trim($email);
  137.        
  138.         if ($this->db->where('email', $email)->count_all_results($this->tables['users']) === 1) return TRUE;
  139.    
  140.         return FALSE;
  141.     }
  142.    
  143.     /**
  144.      * reset user password
  145.      *
  146.      * @param string $email
  147.      */
  148.     public function update_password($user_id, $password)
  149.     {
  150.         $password = sha1(trim($password));
  151.        
  152.         $this->db->where('email', $email)->update('password', $password);
  153.     }
  154.    
  155.     /**
  156.      * insert cookie for "remember me"
  157.      *
  158.      * @param integer $user_id
  159.      */
  160.     public function create_cookie($user_id, $value)
  161.     {
  162.         $this->db->insert($this->tables['cookies'], array('user_id' => $user_id, 'value' => $value));
  163.     }
  164.    
  165.     /**
  166.      * delete cookie
  167.      *
  168.      * @param integer $user_id
  169.      */
  170.     public function delete_cookie($value)
  171.     {
  172.         $this->db->where('value', $value)->delete($this->tables['cookies']);
  173.     }
  174.    
  175.     /**
  176.      * create forgotten password value
  177.      *
  178.      * @param string $user_id
  179.      * @param string $value
  180.      */
  181.     public function create_forgotten_password($user_id, $value)
  182.     {
  183.         $this->db->insert($this->tables['forgotten_passwords'], array('user_id' => $user_id, 'value' => $value));
  184.     }
  185.    
  186.     /**
  187.      * check forgotten password value
  188.      *
  189.      * @param string $user_email
  190.      */
  191.     public function check_forgotten_password($value)
  192.     {
  193.         if ($this->db->where('value', $value)->count_all_results($this->tables['forgotten_passwords']) === 1) return TRUE;
  194.        
  195.         return FALSE;
  196.     }
  197.    
  198.     /**
  199.      * get user id by email
  200.      *
  201.      * @param string $email
  202.      */
  203.     public function get_user_id_by_email($email)
  204.     {
  205.         $email = trim($email);
  206.        
  207.         $query = $this->db->select('id')
  208.                 ->from($this->tables['users'])
  209.                 ->where('email', $email)
  210.                 ->get();
  211.        
  212.         if ($query->num_rows() === 1)
  213.         {
  214.             $row = $query->row();
  215.            
  216.             return $row->email;
  217.         }
  218.        
  219.         return FALSE;
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement