-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathafUser.inc.php
More file actions
282 lines (197 loc) · 7.78 KB
/
afUser.inc.php
File metadata and controls
282 lines (197 loc) · 7.78 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
////////////////////////////////////////////////////////////////////////////////
// IMPORT REQUIRED MODULES
////////////////////////////////////////////////////////////////////////////////
\af\module('node');
\af\module('permission');
\af\module('preference');
//TODO: ADD A TRAIT FOR 'ATTRIBUTES'
// CURRENTLY ONLY ATTRIBUTE IS UNVERIFIED PASSWORD
////////////////////////////////////////////////////////////////////////////////
// HANDLES AN INSTANCE OF A SINGLE USER
////////////////////////////////////////////////////////////////////////////////
class afUser
extends \pudlOrm
implements afUrlx {
use \af\node;
use \af\permission;
use \af\preference;
////////////////////////////////////////////////////////////////////////////
// CONSTRUCTOR
////////////////////////////////////////////////////////////////////////////
public function __construct(\pudl $pudl=NULL, $item=false, $fetch=false) {
parent::__construct($pudl, $item, $fetch);
$this->has_password
= !empty($this->user_pass)
|| !empty($this->auth_password);
}
////////////////////////////////////////////////////////////////////////////
// OVERWRITE THE PUDL PARAMETERS FOR PULLING A COLLECTION
////////////////////////////////////////////////////////////////////////////
protected static function schema() {
return [
'column' => [
static::prefix.'.*',
'ua.auth_account',
'ua.auth_password',
'ua.auth_verified',
],
//TODO: WE SHOULD NOT BE PULLING IN AUTHENTICATION TABLE HERE.
// AUTHENTICATION TABLE SHOULD ONLY BE PULLED FOR LOGIN/SESSION INFO ONLY.
'table' => [static::prefix => [static::table,
['left' => ['ua'=>'user_auth'], 'using'=>'user_id'],
]],
];
}
////////////////////////////////////////////////////////////////////////////
// UPDATE THE USER TABLE IN THE DATABASE
////////////////////////////////////////////////////////////////////////////
public function update($data) {
global $af;
if (!$this->loggedIn()) return false;
$return = parent::update($data);
$af->purgeSession(false, $this);
return $return;
}
////////////////////////////////////////////////////////////////////////////
// LOAD OR UPDATE THE PROFILE TABLE IN THE DATABASE
////////////////////////////////////////////////////////////////////////////
public function profile($data=false) {
if (!$this->loggedIn()) return false;
if ($data !== false) {
$this->merge($data);
return $this->pudl()->updateId('user_profile', $data, $this);
}
return $this->merge($this->pudl()->rowId('user_profile', $this));
}
////////////////////////////////////////////////////////////////////////////
// TRUE IF THE USER IS LOGGED IN, FALSE OTHERWISE
////////////////////////////////////////////////////////////////////////////
public function loggedIn() {
return !!$this->id();
}
////////////////////////////////////////////////////////////////////////////
// GENERATE 401 ERROR PAGE AND EXIT SCRIPT IF USER ISN'T LOGGED IN
////////////////////////////////////////////////////////////////////////////
public function requireLogin($code=401) {
\af\affirm($code, $this->loggedIn());
return $this;
}
////////////////////////////////////////////////////////////////////////////
// GET THE URL PATH OF THE USER
////////////////////////////////////////////////////////////////////////////
public function url() {
return empty($this->user_url) ? $this->id() : $this->user_url;
}
////////////////////////////////////////////////////////////////////////////
// GET THE ICON OF THE USER
////////////////////////////////////////////////////////////////////////////
public function image() {
global $afurl;
return $afurl->cdn($this, 'thumb_hash', 'mime_id');
}
////////////////////////////////////////////////////////////////////////////
// SET THE USER ACCOUNT PASSWORD
////////////////////////////////////////////////////////////////////////////
public function setPassword($account, $password, $algo=PASSWORD_DEFAULT) {
$this->auth_account = $account;
return $this->pudl()->upsert('user_auth', [
static::column => $this->id(),
'auth_account' => $account,
'auth_password' => password_hash($password, $algo),
]);
}
////////////////////////////////////////////////////////////////////////////
// VALIDATE A GIVEN PASSWORD BASED ON CONFIGURATION DATA
////////////////////////////////////////////////////////////////////////////
public static function validatePassword($password) {
global $afconfig;
if (!is_string($password)) {
return 'Invalid password format';
}
if (empty($password)) {
return 'No password specified';
}
if (strlen($password) < $afconfig->password['length']) {
return 'Password must be at least '
. $afconfig->password['length']
. ' characters long';
}
if (!empty($afconfig->password['upper'])) {
if (!preg_match('/[a-z]/', $password) || !preg_match('/[A-Z]/', $password)) {
return 'Password must contain both upper and lower case letters';
}
}
if (!empty($afconfig->password['number'])) {
if (!preg_match('/[0-9]/', $password)) {
return 'Password must contain numbers';
}
}
if (!empty($afconfig->password['symbol'])) {
if (!preg_match('/[^0-9a-zA-Z]/', $password)) {
return 'Password must contain special character symbols';
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////
// ADD OR DELETE AN ITEM FROM THE USER'S MESSAGE QUEUE
////////////////////////////////////////////////////////////////////////////
function queue($service, $type, $data=false) {
if ($data === false) {
$this->pudl()->delete('queue', [
'queue_user' => $this->id(),
'queue_service' => $service,
'queue_type' => $type,
]);
return;
}
if (!tbx_array($data)) $data = [$data];
$this->pudl()->insert('queue', [
'queue_user' => $this->id(),
'queue_service' => $service,
'queue_type' => $type,
'queue_time' => $this->pudl()->time(),
'queue_message' => $data,
], [
'queue_time' => $this->pudl()->time(),
'queue_message' => $data,
'queue_count' => pudl::_increment(1),
]
);
}
////////////////////////////////////////////////////////////////////////////
// HOW LONG SHOULD THE FETCHED DATA BE CACHED FOR (IN SECONDS)
////////////////////////////////////////////////////////////////////////////
protected function _fetchCache() { return AF_HOUR; }
////////////////////////////////////////////////////////////////////////////
// LATE STATIC BINDING VARIABLES FROM PUDL ORM
////////////////////////////////////////////////////////////////////////////
const column = 'user_id';
const icon = 'user_icon';
const json = 'user_json';
const table = 'user';
const prefix = 'us';
}
////////////////////////////////////////////////////////////////////////////////
// SHORTCUT CLASS FOR ANONYMOUS USER
////////////////////////////////////////////////////////////////////////////////
class afAnonymous extends afUser {
public function __construct(\pudl $pudl) {
try {
parent::__construct($pudl, 0, true);
} catch (\pudlException $e) {
$this->user_id = 0;
$this->user_name = 'anonymous';
$this->user_json = [];
$this->user_icon = NULL;
}
}
protected function _fetchCache() { return AF_DAY; }
}
////////////////////////////////////////////////////////////////////////////////
//SHORTCUT CLASS FOR A CACHELESS ACCOUNT - USEFUL FOR ADMIN PAGES
////////////////////////////////////////////////////////////////////////////////
class afAccount extends afUser {
protected function _fetchCache() { return 0; }
}