Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 18th, 2012  |  syntax: None  |  size: 1.29 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. class Current_User {
  3.  
  4.         private static $user;
  5.  
  6.         private function __construct() {}
  7.  
  8.         public static function user() {
  9.  
  10.                 if(!isset(self::$user)) {
  11.  
  12.                         $CI =& get_instance();
  13.                         $CI->load->library('session');
  14.  
  15.                         if (!$user_id = $CI->session->userdata('user_id')) {
  16.                                 return FALSE;
  17.                         }
  18.  
  19.                         if (!$u = Doctrine::getTable('User')->find($user_id)) {
  20.                                 return FALSE;
  21.                         }
  22.  
  23.                         self::$user = $u;
  24.                 }
  25.  
  26.                 return self::$user;
  27.         }
  28.  
  29.  
  30.         public static function login($email, $password) {
  31.  
  32.                 // get User object by username
  33.                 if ($u = Doctrine::getTable('User')->findOneByEmail($email)) {
  34.  
  35.  
  36.                         // to ge the mutated version of the input password
  37.                         $u_input = new User();
  38.                         $u_input->password = $password;
  39.  
  40.                         // password match
  41.                         if ($u->password == $u_input->password) {
  42.  
  43.  
  44.                                 $CI =& get_instance();
  45.                                 $CI->load->library('session');
  46.                                 $CI->session->set_userdata('user_id',$u->id);
  47.                                 $CI->session->set_userdata('user_name',$u->username);
  48.                                 $CI->session->set_userdata('first_name',$u->first_name);
  49.                                 $CI->session->set_userdata('last_name',$u->last_name);
  50.  
  51.  
  52.                                 self::$user = $u;
  53.  
  54.                                 return TRUE;
  55.                         }
  56.  
  57.                         unset($u_input);
  58.                 }
  59.  
  60.                 // login failed
  61.                 return FALSE;
  62.  
  63.         }
  64.  
  65.  
  66.         public function __clone() {
  67.                 trigger_error('Clone is not allowed.', E_USER_ERROR);
  68.         }
  69.  
  70. }