-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.php
More file actions
executable file
·209 lines (178 loc) · 6.19 KB
/
Copy pathUser.php
File metadata and controls
executable file
·209 lines (178 loc) · 6.19 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
require_once 'Helper/Database.php';
require_once 'Helper/Utils.php';
require_once 'Helper/Str.php';
require_once 'Helper/Emailer.php';
require_once 'Template/EmailTemplate.php';
require_once 'Config/Config.php';
class User {
private $id;
private $fillables = [
'firstName', 'lastName', 'email', 'username', 'passwordDigest',
'activationDigest', 'rememberDigest', 'resetDigest', 'resetSentAt', 'activatedAt',
'isActive', 'id'
];
public function __construct() {
$this->createActivationDigest();
}
public function __get($prop) {
return $this->{$prop};
}
public function __set($prop, $value = null) {
$this->{$prop} = $value;
}
public function findOne($userId) {
$db = new Database();
$row = $db->executeQuery('SELECT *, isActive+0 as isActive FROM User WHERE id = :id LIMIT 1', ['id' => $userId]);
if ($row) {
return $this->toObject($row[0]);
}
return null;
}
public function find($criteria = null) {
$db = new Database();
$sql = 'SELECT *, isActive+0 as isActive FROM User WHERE 1 ';
if ($criteria) {
$sql .= "AND $criteria";
}
return $db->executeQuery($sql, [], PDO::FETCH_OBJ);
}
public function findOneBy($criteria = null) {
$db = new Database();
$sql = 'SELECT *, isActive+0 as isActive FROM User WHERE 1 ';
if ($criteria) {
$sql .= "AND $criteria";
}
$sql .= ' LIMIT 1';
$rows = $db->executeQuery($sql);
if ($rows) {
return $this->toObject($rows[0]);
}
return null;
}
public function findAll() {
$db = new Database();
return $db->executeQuery('SELECT * FROM User');
}
public function save() {
$db = new Database();
$update = isset($this->id);
$id = $update ? $this->id : 0;
$vars = get_object_vars($this);
if ($vars) {
$script = $update ? 'UPDATE User SET ' : 'INSERT INTO User SET ';
$sp = constructScriptAndParams($vars, $script, $this->fillables);
if ($update) {
$sp['script'] .= " WHERE id = $id";
} else {
$this->createPasswordDigest();
$sp['params']['id'] = $id;
$sp['params']['passwordDigest'] = $this->passwordDigest;
$sp['script'] .= " ,passwordDigest = :passwordDigest ";
}
$result = $db->executeUpdate($sp['script'], $sp['params']);
if ($result) {
if ($update) {
return $this->findOne($this->id);
} else {
return $this->findOne($db->lastInsertId);
}
}
}
return null;
}
private function toObject(array $assocArray) {
foreach($assocArray as $key => $value) {
$this->$key = $value;
}
return $this;
}
# Returns a random token
protected function newToken($str = '') {
if ($str && strlen($str) > 0) {
return hash_hmac('sha256', $str, 'secret');
}
return hash_hmac('sha256', Str::random(40), 'secret');
}
# Remembers a user in the database for use in persistent sessions.
public function remember() {
$this->rememberToken = $this->newToken();
$this->rememberDigest = $this->digest($this->rememberToken);
$this->save();
}
# Forgets a user.
public function forget() {
$this->rememberDigest = NULL;
$this->save();
}
# Returns the hash digest of the given string.
protected function digest($str) {
return password_hash($str, PASSWORD_BCRYPT, ["cost" => 4]);
}
# Returns true if the given token matches the digest.
public function isAuthenticated($attribute, $token) {
$digest = $this->{"{$attribute}Digest"};
if (isset($digest)) {
return password_verify($token, $digest);
}
return false;
}
# Activates an account.
public function activate() {
$this->isActive = True;
$this->activatedAt = date("Y-m-d H:i:s");
$this->save();
}
# Sends activation email.
public function sendActivationEmail() {
$template = EmailTemplate::userActivation($this);
$emailConf = Config::email();
$params = [
'type' => 'activation',
'senderName' => $emailConf['sender']['name'],
'senderEmail' => $emailConf['sender']['email'],
'receiverName' => $this->firstName . ' ' . $this->lastName,
'receiverEmail' => $this->email,
'cc' => NULL,
'bcc' => NULL,
'subject' => 'Activation',
'body' => $template,
];
return Emailer::queue($params);
}
# Sets the password reset attributes.
public function createResetDigest() {
$this->resetToken = $this->newToken();
$this->resetDigest = $this->digest($this->resetToken);
$this->resetSentAt = date("Y-m-d H:i:s");
$this->save();
}
# Sends password reset email.
public function sendPasswordResetEmail() {
$template = EmailTemplate::passwordReset($this);
$emailConf = Config::email();
$params = [
'type' => 'password_reset',
'senderName' => $emailConf['sender']['name'],
'senderEmail' => $emailConf['sender']['email'],
'receiverName' => $this->firstName . ' ' . $this->lastName,
'receiverEmail' => $this->email,
'cc' => NULL,
'bcc' => NULL,
'subject' => 'Password reset',
'body' => $template,
];
return Emailer::queue($params);
}
# Returns true if a password reset has expired.
public function passwordResetExpired() {
return $this->resetSentAt <= strtotime('-2 hours');
}
private function createActivationDigest() {
$this->activationToken = $this->newToken();
$this->activationDigest = $this->digest($this->activationToken);
}
public function createPasswordDigest() {
$this->passwordDigest = $this->digest($this->password);
}
}