Advertisement
Guest User

class User

a guest
Oct 23rd, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.40 KB | None | 0 0
  1. <?php
  2. class User{
  3.     private $_db,
  4.             $_data,
  5.             $_sessionName,
  6.             $_isLoggedIn;
  7.  
  8.     public function __construct($user = NULL){
  9.         $this->_db = DB::getInstance();
  10.  
  11.         $this->_sessionName = Config::get('session/session_name');
  12.  
  13.         if(!$user){
  14.             if(Session::exists($this->_sessionName)){
  15.                 $user = Session::get($this->_sessionName);
  16.                
  17.                 if($this->find($user)){
  18.                     $this->_isLoggedIn = true;
  19.                 } else {
  20.                     //logout
  21.                 }
  22.             }
  23.         } else {
  24.             $this->find($user);
  25.         }
  26.     }
  27.  
  28.     public function create($fields){
  29.         if(!$this->_db->insert('users', $fields)){
  30.             throw new Exception('There is a problem creating user');
  31.         }      
  32.     }
  33.  
  34.     public function find($user = NULL){
  35.         if($user){
  36.             $field = (is_numeric($user)) ? 'id' : 'email';
  37.             $data  = $this->_db->get('users', array($field, '=', $user));
  38.  
  39.             if($data->count()){
  40.                 $this->_data = $data->first();
  41.                 return true;
  42.             }
  43.         }
  44.         return false;
  45.     }
  46.  
  47.     public function login($email = NULL, $password = NULL){
  48.         $user = $this->find($email);
  49.        
  50.         if($user){
  51.             if($this->data()->password === Hash::make($password, $this->data()->salt)){
  52.                 Session::put($this->_sessionName, $this->data()->id);
  53.                 return true;
  54.             }
  55.         }
  56.         return false;
  57.     }
  58.  
  59.     public function logout(){
  60.         Session::delete($this->_sessionName);
  61.     }
  62.  
  63.     public function data(){
  64.         return $this->_data;
  65.     }
  66.  
  67.     public function isLoggedIn(){
  68.         return $this->_isLoggedIn;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement