Advertisement
rico23

Untitled

Jan 24th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | None | 0 0
  1. <?php
  2. class User {
  3.     private $_db,
  4.             $_data,
  5.             $_sessionName,
  6.             $_cookieName,
  7.             $_isLoggedIn,
  8.             $_id;
  9.  
  10.     public function __construct($user = null) {
  11.         $this->_db = DB::getinstance();
  12.  
  13.         $this->_sessionName = Config::get('session/session_name');
  14.         $this->_cookieName = Config::get('remember/cookie_name');
  15.  
  16.         if (!$user) {
  17.             if (Session::exists($this->_sessionName)) {
  18.                 $user = Session::get($this->_sessionName);
  19.  
  20.                 if ($this->find($user)) {
  21.                     $this->_isLoggedIn = true;
  22.                 } else {
  23.                     //
  24.                 }
  25.             }
  26.         } else {
  27.             $this->find($user);
  28.         }
  29.     }
  30.  
  31.     public function update($fields = array(), $_id = null) {
  32.  
  33.         if (!$id && $this->isLoggedIn()) {
  34.             $_id = $this->data()->id;
  35.         }
  36.  
  37.         if (!$this->_db->update('users', $_id, $fields)) {
  38.             throw new Exception('There was a problem updating.');
  39.         }
  40.     }
  41.  
  42.     public function create($fields = array()) {
  43.         if (!$this->_db->insert('users', $fields)) {
  44.             throw new Excepction('There was a problem creating an account.');
  45.         }
  46.     }
  47.  
  48.  
  49.     public function find($user = null) {
  50.         if ($user) {
  51.             $field = (is_numeric($user)) ? 'id' : 'email';
  52.             $data = $this->_db->get('users', array($field, '=', $user));
  53.  
  54.             if ($data->count()) {
  55.                 $this->_data = $data->first();
  56.                 return true;
  57.             }
  58.         }
  59.         return false;
  60.     }
  61.  
  62.     public function login($email = null, $password = null, $remember = false) {
  63.        
  64.  
  65.         if (!$email && !$password && $this->exists()) {
  66.             Session::put($this->_sessionName, $this->data()->id);
  67.         } else {
  68.             $user = $this->find($email);
  69.  
  70.             if ($user) {
  71.                 if ($this->data()->password === Hash::make($password, $this->data()->salt)) {
  72.                     Session::put($this->_sessionName, $this->data()->id);
  73.  
  74.                     if ($remember) {
  75.                         $hash = Hash::unique();
  76.                         $hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id));
  77.  
  78.                         if (!$hashCheck->count()) {
  79.                             $this->_db->insert('users_session', array(
  80.                                 'user_id' => $this->data()->id,
  81.                                 'hash' => $hash
  82.                             ));
  83.                         } else {
  84.                             $hash = $hashCheck->first()->hash;
  85.                         }
  86.  
  87.                         Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
  88.  
  89.                     }
  90.                    
  91.                     return true;
  92.                 }
  93.             }
  94.         }
  95.  
  96.         return false;
  97.     }
  98.  
  99.     public function hasPermission($key) {
  100.         $group = $this->_db->get('groups', array('id', '=', $this->data()->group));
  101.  
  102.         if ($group->count()) {
  103.             $permissions = json_decode($group->first()->permissions, true);
  104.  
  105.             if ($permissions[$key] == true) {
  106.                 return true;
  107.             }
  108.         }
  109.         return false;
  110.     }
  111.  
  112.     public static function userId() {  
  113.         return $this->_id;
  114.     }
  115.  
  116.     public function exists() {
  117.         return (!empty($this->_data)) ? true : false;
  118.     }
  119.  
  120.     public function logout() {
  121.  
  122.         $this->_db->delete('users_session', array('user_id', '=', $this->data()->id));
  123.  
  124.         Session::delete($this->_sessionName);
  125.         Cookie::delete($this->_cookieName);
  126.     }
  127.  
  128.     public function data() {
  129.         return $this->_data;
  130.     }
  131.  
  132.     public function isLoggedIn() {
  133.         return $this->_isLoggedIn;
  134.     }
  135.  
  136.    
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement