Advertisement
Guest User

Untitled

a guest
May 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.27 KB | None | 0 0
  1. <?php
  2. class Current_User {
  3.  
  4.     /** Returns the User object for the logged in user. */
  5.     private static $user;
  6.  
  7.     private function __construct() {}
  8.  
  9.     public static function user() {
  10.  
  11.         if(!isset(self::$user)) {
  12.             // we use the $CI object to load the session library because this is a static class.
  13.             $CI =& get_instance();
  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.     public static function login($username, $password) {
  30.  
  31.         // get User object by username
  32.         if ($u = Doctrine::getTable('User')->findOneByUsername($username)) {
  33.  
  34.             // this mutates (encrypts) the input password
  35.             $u_input = new User();
  36.             $u_input->password = $password;
  37.  
  38.             // password match (comparing encrypted passwords)
  39.             if ($u->password == $u_input->password) {
  40.                 unset($u_input);
  41.  
  42.                 $CI =& get_instance();
  43.                 $CI->load->library('session');
  44.                 $CI->session->set_userdata('user_id',$u->id);
  45.                 self::$user = $u;
  46.  
  47.                 return TRUE;
  48.             }
  49.  
  50.             unset($u_input);
  51.         }
  52.  
  53.         // login failed
  54.         return FALSE;
  55.  
  56.     }
  57.  
  58.     public function __clone() {
  59.         trigger_error('Clone is not allowed.', E_USER_ERROR);
  60.     }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement