-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.php
140 lines (114 loc) · 3.57 KB
/
User.php
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
137
138
139
140
<?php
class User
{
private $db, $data, $sessionName, $isLoggedIn, $cookieName;
public function __construct($user = null) {
$this->db = Database::getInstance();
$this->sessionName = Config::get('session.user_session');
$this->cookieName = Config::get('cookie.cookie_name');
if (!$user) {
if (Session::exists($this->sessionName)) {
$user = Session::get($this->sessionName); //id
if ($this->find($user)) {
$this->isLoggedIn = true;
}
}
} else {
$this->find($user);
}
}
public function create($fields = []) {
$this->db->insert('users', $fields);
}
/**
* @param string|null $email
* @param string|null $password
* @param bool $remember
* @return bool
*/
public function login(string $email = null, string $password = null, bool $remember = false) {
if (!$email && !$password && $this->exists()) {
Session::put($this->sessionName, $this->data()->id);
} else {
$user = $this->find($email);
if ($user) {
if (password_verify($password, $this->data()->password)) {
Session::put($this->sessionName, $this->data()->id);
if ($remember) {
$hash = hash('sha256', uniqid());
//$hashCheck = $this->db->get('users', ['id', '=', $this->data()->id]);
$hashCheck = $this->db->get('user_sessions', ['user_id', '=', $this->data()->id]);
/*if(!$hashCheck->first()->hash) {
//$this->db->update('users', $this->data()->id, ['hash' => $hash]);
$this->update(['hash' => $hash], $this->data()->id);*/
if (!$hashCheck->count()) {
$this->db->insert('user_sessions', [
'user_id' => $this->data()->id,
'hash' => $hash
]);
} else {
$hash = $hashCheck->first()->hash;
}
Cookie::put($this->cookieName, $hash, Config::get('cookie.cookie_expiry'));
}
return true;
}
}
}
return false;
}
public function find($value = null) {
if (is_numeric($value)) {
$this->data = $this->db->get('users', ['id', '=', $value])->first();
} else {
$this->data = $this->db->get('users', ['email', '=', $value])->first();
}
if ($this->data) return true;
return false;
}
public function data() {
return $this->data;
}
public function isLoggedIn() {
return $this->isLoggedIn;
}
public function logout() {
//$this->db->update('users', $this->data()->id, ['hash' => '']);
//$this->update(['hash' => ''], $this->data()->id);
$this->db->delete('user_sessions', ['user_id', '=', $this->data()->id]);
//Session::delete($this->sessionName);
Session::destroy();
Cookie::delete($this->cookieName);
}
public function exists() {
return (!empty($this->data())) ? true : false;
}
public function update($fields = [], $id = null) {
if (!$id && $this->isLoggedIn()) {
$id = $this->data()->id;
}
$this->db->update('users', $id, $fields);
}
public function remove($id = null) {
// if(!$id && $this->isLoggedIn()) {
// $id = $this->data()->id;
// }
$this->db->delete('users', ['id', '=', $this->data()->id]);
}
// проверят наличие прав с подключением к БД
public function hasPermissions($key = null) {
if ($key) {
$group = $this->db->get('groups', ['id', '=', $this->data()->group_id]);
return $group->count() ? $this->checkPermissions($group->first()->permissions, $key) : false;
}
return false;
}
// проверяет наличие прав по переданному JSON из БД
public static function checkPermissions($json, $key = null) {
if ($json && $key) {
$permissions = json_decode($json, true);
if ($permissions[$key]) return true;
}
return false;
}
}