shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

user.php (3000B)


      1 <?php
      2 namespace Cart;
      3 class User {
      4 	private $user_id;
      5 	private $user_group_id;
      6 	private $username;
      7 	private $permission = array();
      8 
      9 	public function __construct($registry) {
     10 		$this->db = $registry->get('db');
     11 		$this->request = $registry->get('request');
     12 		$this->session = $registry->get('session');
     13 
     14 		if (isset($this->session->data['user_id'])) {
     15 			$user_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user WHERE user_id = '" . (int)$this->session->data['user_id'] . "' AND status = '1'");
     16 
     17 			if ($user_query->num_rows) {
     18 				$this->user_id = $user_query->row['user_id'];
     19 				$this->username = $user_query->row['username'];
     20 				$this->user_group_id = $user_query->row['user_group_id'];
     21 
     22 				$this->db->query("UPDATE " . DB_PREFIX . "user SET ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE user_id = '" . (int)$this->session->data['user_id'] . "'");
     23 
     24 				$user_group_query = $this->db->query("SELECT permission FROM " . DB_PREFIX . "user_group WHERE user_group_id = '" . (int)$user_query->row['user_group_id'] . "'");
     25 
     26 				$permissions = json_decode($user_group_query->row['permission'], true);
     27 
     28 				if (is_array($permissions)) {
     29 					foreach ($permissions as $key => $value) {
     30 						$this->permission[$key] = $value;
     31 					}
     32 				}
     33 			} else {
     34 				$this->logout();
     35 			}
     36 		}
     37 	}
     38 
     39 	public function login($username, $password) {
     40 		$user_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user WHERE username = '" . $this->db->escape($username) . "' AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1('" . $this->db->escape($password) . "'))))) OR password = '" . $this->db->escape(md5($password)) . "') AND status = '1'");
     41 
     42 		if ($user_query->num_rows) {
     43 			$this->session->data['user_id'] = $user_query->row['user_id'];
     44 
     45 			$this->user_id = $user_query->row['user_id'];
     46 			$this->username = $user_query->row['username'];
     47 			$this->user_group_id = $user_query->row['user_group_id'];
     48 
     49 			$user_group_query = $this->db->query("SELECT permission FROM " . DB_PREFIX . "user_group WHERE user_group_id = '" . (int)$user_query->row['user_group_id'] . "'");
     50 
     51 			$permissions = json_decode($user_group_query->row['permission'], true);
     52 
     53 			if (is_array($permissions)) {
     54 				foreach ($permissions as $key => $value) {
     55 					$this->permission[$key] = $value;
     56 				}
     57 			}
     58 
     59 			return true;
     60 		} else {
     61 			return false;
     62 		}
     63 	}
     64 
     65 	public function logout() {
     66 		unset($this->session->data['user_id']);
     67 
     68 		$this->user_id = '';
     69 		$this->username = '';
     70 	}
     71 
     72 	public function hasPermission($key, $value) {
     73 		if (isset($this->permission[$key])) {
     74 			return in_array($value, $this->permission[$key]);
     75 		} else {
     76 			return false;
     77 		}
     78 	}
     79 
     80 	public function isLogged() {
     81 		return $this->user_id;
     82 	}
     83 
     84 	public function getId() {
     85 		return $this->user_id;
     86 	}
     87 
     88 	public function getUserName() {
     89 		return $this->username;
     90 	}
     91 
     92 	public function getGroupId() {
     93 		return $this->user_group_id;
     94 	}
     95 }