-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUser.php
More file actions
136 lines (107 loc) · 2.97 KB
/
Copy pathUser.php
File metadata and controls
136 lines (107 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
class User {
private $_db,
$_data,
$_sessionName,
$_cookieName,
$_isLoggedIn;
public function __construct($user = null) {
$this->_db = DB::getInstance();
$this->_sessionName = Config::get('session/session_name');
$this->_cookieName = Config::get('remember/cookie_name');
if(!$user){
if(Session::exists($this->_sessionName)){
$user = Session::get($this->_sessionName);
if($this->find($user)){
$this->_isLoggedIn = true;
} else {
// process logout
}
}
} else {
$this->find($user);
}
}
public function update($fields = array(),$id=null){
if(!$id && $this->isLoggedIn()){
$id=$this->data()->id;
}
if(!$this->_db->update('users',$id,$fields)){
throw new Exception('There was a problem updating.');
}
}
public function create($fields=array()){
if (!$this->_db->insert('users', $fields)){
throw new Exception('There was a problem creating the account.');
}
}
public function find($user = null){
if($user){
$field = (is_numeric($user)) ? 'id' : 'username';
$data = $this ->_db->get('users',array($field, "=", $user));
if($data->count()){
$this->_data = $data->first();
return true;
}
}
return false;
}
public function hasPermission($key) {
$group = $this->_db->get('groups',array('id','=',$this->data()->group));
//print_r($group->first());
if ($group->count()){
$permissions = json_decode($group->first()->permissions,true);
//print_r($permissions);
if($permissions[$key] == true){
return true;
}
}
return false;
}
public function login($username = null,$password = null, $remember = false){
$user = $this->find($username);
if(!$username && !$password && $this->exists()){
// log user in
Session::put($this->_sessionName,$this->data()->id);
} else {
//print_r($this->_data);
if ($user){
if($this->data()->password === Hash::make($password, $this->data()->salt)){
Session::put($this->_sessionName,$this->data()->id);
if($remember){
echo 'Remember';
$hash = Hash::unique();
$hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id));
if(!$hashCheck->count()){
echo 'No count'; echo $this->data()->id; //exit;
$this->_db->insert('users_session', array(
'user_id' => $this->data()->id,
'hash' => $hash
));
}else{
$hash = $hashCheck->first()->hash;
}
Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
}
return true;
//echo 'OK!';
}
}
return false;
}
}
public function exists(){
return (!empty($this->_data)) ? true : false;
}
public function logout(){
$this->_db->delete('users_session',array('user_id','=',$this->data()->id));
Session::delete($this->_sessionName);
Cookie::delete($this->_cookieName);
}
public function data(){
return $this->_data;
}
public function isLoggedIn(){
return $this->_isLoggedIn;
}
}