|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* User class |
|
4
|
|
|
* |
|
5
|
|
|
* This file describes the User classes |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 5 and 7 |
|
8
|
|
|
* |
|
9
|
|
|
* @author Patrick Boyd / [email protected] |
|
10
|
|
|
* @copyright Copyright (c) 2015, Austin Artistic Reconstruction |
|
11
|
|
|
* @license http://www.apache.org/licenses/ Apache 2.0 License |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Auth; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* A class to abstract access to Users regardless of the Authentication type used. |
|
18
|
|
|
* |
|
19
|
|
|
* This class is the primary method to access user information. |
|
20
|
|
|
*/ |
|
21
|
|
|
class User extends \SerializableObject |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* An array to cache the title to string mappings so that they don't need to be pulled from the database |
|
25
|
|
|
* everytime |
|
26
|
|
|
*/ |
|
27
|
|
|
public static $titlenames = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Is this user in the Group or a child of that group? |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $name The name of the group to check if the user is in |
|
33
|
|
|
* |
|
34
|
|
|
* @return boolean True if the user is in the group, false otherwise |
|
35
|
|
|
*/ |
|
36
|
|
|
public function isInGroupNamed($name) |
|
37
|
|
|
{ |
|
38
|
|
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function __get($propName) |
|
42
|
|
|
{ |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function __set($propName, $value) |
|
47
|
|
|
{ |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function __isset($propName) |
|
51
|
|
|
{ |
|
52
|
|
|
return false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The list of titles for the user |
|
57
|
|
|
* |
|
58
|
|
|
* @return boolean|array The user's title(s) in user friendly strings |
|
59
|
|
|
* |
|
60
|
|
|
* @SuppressWarnings("StaticAccess") |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getTitleNames() |
|
63
|
|
|
{ |
|
64
|
|
|
$titles = $this->title; |
|
|
|
|
|
|
65
|
|
|
if($titles === false) |
|
66
|
|
|
{ |
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
if(self::$titlenames === null) |
|
70
|
|
|
{ |
|
71
|
|
|
$dataSet = \DataSetFactory::getDataSetByName('profiles'); |
|
72
|
|
|
$dataTable = $dataSet['position']; |
|
73
|
|
|
$titlenames = $dataTable->read(); |
|
74
|
|
|
self::$titlenames = array(); |
|
75
|
|
|
$count = count($titlenames); |
|
76
|
|
|
for($i = 0; $i < $count; $i++) |
|
77
|
|
|
{ |
|
78
|
|
|
self::$titlenames[$titlenames[$i]['short_name']] = $titlenames[$i]; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
$count = count($titles); |
|
82
|
|
|
for($i = 0; $i < $count; $i++) |
|
83
|
|
|
{ |
|
84
|
|
|
if(isset(self::$titlenames[$titles[$i]])) |
|
85
|
|
|
{ |
|
86
|
|
|
$title = self::$titlenames[$titles[$i]]; |
|
87
|
|
|
$titles[$i] = $title['name']; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
return $titles; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* The groups the user is a part of |
|
95
|
|
|
* |
|
96
|
|
|
* @return boolean|array The user's Auth\Group structures |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getGroups() |
|
99
|
|
|
{ |
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Add a supplemental login type that the user can use to login |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $provider The hostname for the provider |
|
107
|
|
|
*/ |
|
108
|
|
|
public function addLoginProvider($provider) |
|
109
|
|
|
{ |
|
110
|
|
|
if(isset($this->host)) |
|
111
|
|
|
{ |
|
112
|
|
|
$tmp = $this->host; |
|
|
|
|
|
|
113
|
|
|
$tmp[] = $provider; |
|
114
|
|
|
$this->host = $tmp; |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
else |
|
117
|
|
|
{ |
|
118
|
|
|
$this->host = array($provider); |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Can the user login with this provider? |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $provider The hostname for the provider |
|
126
|
|
|
* |
|
127
|
|
|
* @return boolean true if they can login with the provider, false otherwise |
|
128
|
|
|
*/ |
|
129
|
|
|
public function canLoginWith($provider) |
|
130
|
|
|
{ |
|
131
|
|
|
$hosts = $this->host; |
|
|
|
|
|
|
132
|
|
|
if($hosts === false) |
|
133
|
|
|
{ |
|
134
|
|
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
$count = count($hosts); |
|
137
|
|
|
for($i = 0; $i < $count; $i++) |
|
138
|
|
|
{ |
|
139
|
|
|
if(strcasecmp($hosts[$i], $provider) === 0) |
|
140
|
|
|
{ |
|
141
|
|
|
return true; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Set the user's password without verifying the current password |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $password The new user password |
|
151
|
|
|
* |
|
152
|
|
|
* @return boolean true if the user's password was changed, false otherwise |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function setPass($password) |
|
155
|
|
|
{ |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Has the user completely filled out their user profile? |
|
161
|
|
|
* |
|
162
|
|
|
* @return boolean true if the user's profile is complete, false otherwise |
|
163
|
|
|
*/ |
|
164
|
|
|
public function isProfileComplete() |
|
165
|
|
|
{ |
|
166
|
|
|
if($this->c === false || $this->postalAddress === false || |
|
|
|
|
|
|
167
|
|
|
$this->postalCode === false || $this->l === false || |
|
|
|
|
|
|
168
|
|
|
$this->st === false || $this->mobile === false) |
|
|
|
|
|
|
169
|
|
|
{ |
|
170
|
|
|
return false; |
|
171
|
|
|
} |
|
172
|
|
|
return true; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Validate that the user's password is the specified password |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $password The user's current password |
|
179
|
|
|
* |
|
180
|
|
|
* @return boolean true if the user's password is correct, false otherwise |
|
181
|
|
|
* |
|
182
|
|
|
* @SuppressWarnings("UnusedFormalParameter") |
|
183
|
|
|
*/ |
|
184
|
|
|
public function validate_password($password) |
|
185
|
|
|
{ |
|
186
|
|
|
return false; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Validate that the user's reset hash is the sepcified hash |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $hash The user's reset hash |
|
193
|
|
|
* |
|
194
|
|
|
* @return boolean true if the user's hash is correct, false otherwise |
|
195
|
|
|
* |
|
196
|
|
|
* @SuppressWarnings("UnusedFormalParameter") |
|
197
|
|
|
*/ |
|
198
|
|
|
public function validate_reset_hash($hash) |
|
199
|
|
|
{ |
|
200
|
|
|
return false; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Change the user's password, validating the old password or reset hash |
|
205
|
|
|
* |
|
206
|
|
|
* @param string $oldpass The user's original password or reset hash if $isHash is true |
|
207
|
|
|
* @param string $newpass The user's new password |
|
208
|
|
|
* @param boolean $isHash Is $old_pass a password or a hash |
|
209
|
|
|
* |
|
210
|
|
|
* @return boolean true if the user's password was changed, false otherwise |
|
211
|
|
|
*/ |
|
212
|
|
|
public function change_pass($oldpass, $newpass, $isHash = false) |
|
213
|
|
|
{ |
|
214
|
|
|
if($isHash === false && $this->validate_password($oldpass) === false) |
|
215
|
|
|
{ |
|
216
|
|
|
throw new \Exception('Invalid Password!', 3); |
|
217
|
|
|
} |
|
218
|
|
|
if($isHash === true && $this->validate_reset_hash($oldpass) === false) |
|
219
|
|
|
{ |
|
220
|
|
|
throw new \Exception('Invalid Reset Hash!', 3); |
|
221
|
|
|
} |
|
222
|
|
|
if($this->setPass($newpass) === false) |
|
223
|
|
|
{ |
|
224
|
|
|
throw new \Exception('Unable to set password!', 6); |
|
225
|
|
|
} |
|
226
|
|
|
return true; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Allow write for the user |
|
231
|
|
|
*/ |
|
232
|
|
|
protected function enableReadWrite() |
|
233
|
|
|
{ |
|
234
|
|
|
//Make sure we are bound in write mode |
|
235
|
|
|
$auth = \AuthProvider::getInstance(); |
|
236
|
|
|
$ldap = $auth->getMethodByName('Auth\LDAPAuthenticator'); |
|
237
|
|
|
if($ldap !== false) |
|
238
|
|
|
{ |
|
239
|
|
|
$ldap->get_and_bind_server(true); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Update the user password if required |
|
245
|
|
|
*/ |
|
246
|
|
|
private function editUserPassword($data) |
|
247
|
|
|
{ |
|
248
|
|
|
if(isset($data->password)) |
|
249
|
|
|
{ |
|
250
|
|
|
if(isset($data->oldpass)) |
|
251
|
|
|
{ |
|
252
|
|
|
$this->change_pass($data->oldpass, $data->password); |
|
253
|
|
|
unset($data->oldpass); |
|
254
|
|
|
} |
|
255
|
|
|
else if(isset($data->hash)) |
|
256
|
|
|
{ |
|
257
|
|
|
$this->change_pass($data->hash, $data->password, true); |
|
258
|
|
|
unset($data->hash); |
|
259
|
|
|
} |
|
260
|
|
|
unset($data->password); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
private function editNames($data) |
|
265
|
|
|
{ |
|
266
|
|
|
if(isset($data->displayName)) |
|
267
|
|
|
{ |
|
268
|
|
|
$this->displayName = $data->displayName; |
|
|
|
|
|
|
269
|
|
|
unset($data->displayName); |
|
270
|
|
|
} |
|
271
|
|
|
if(isset($data->givenName)) |
|
272
|
|
|
{ |
|
273
|
|
|
$this->givenName = $data->givenName; |
|
|
|
|
|
|
274
|
|
|
unset($data->givenName); |
|
275
|
|
|
} |
|
276
|
|
|
if(isset($data->sn)) |
|
277
|
|
|
{ |
|
278
|
|
|
$this->sn = $data->sn; |
|
|
|
|
|
|
279
|
|
|
unset($data->sn); |
|
280
|
|
|
} |
|
281
|
|
|
if(isset($data->cn)) |
|
282
|
|
|
{ |
|
283
|
|
|
$this->cn = $data->cn; |
|
|
|
|
|
|
284
|
|
|
unset($data->cn); |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
private function checkForUnsettableElements($data) |
|
289
|
|
|
{ |
|
290
|
|
View Code Duplication |
if(isset($data->mail)) |
|
|
|
|
|
|
291
|
|
|
{ |
|
292
|
|
|
if($data->mail !== $this->mail) |
|
|
|
|
|
|
293
|
|
|
{ |
|
294
|
|
|
throw new \Exception('Unable to change email!'); |
|
295
|
|
|
} |
|
296
|
|
|
unset($data->mail); |
|
297
|
|
|
} |
|
298
|
|
View Code Duplication |
if(isset($data->uid)) |
|
|
|
|
|
|
299
|
|
|
{ |
|
300
|
|
|
if($data->uid !== $this->uid) |
|
|
|
|
|
|
301
|
|
|
{ |
|
302
|
|
|
throw new \Exception('Unable to change uid!'); |
|
303
|
|
|
} |
|
304
|
|
|
unset($data->uid); |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
private function editAddressElements($data) |
|
309
|
|
|
{ |
|
310
|
|
|
if(isset($data->postalAddress)) |
|
311
|
|
|
{ |
|
312
|
|
|
$this->postalAddress = $data->postalAddress; |
|
|
|
|
|
|
313
|
|
|
unset($data->postalAddress); |
|
314
|
|
|
} |
|
315
|
|
|
if(isset($data->l)) |
|
316
|
|
|
{ |
|
317
|
|
|
$this->l = $data->l; |
|
|
|
|
|
|
318
|
|
|
unset($data->l); |
|
319
|
|
|
} |
|
320
|
|
|
if(isset($data->st)) |
|
321
|
|
|
{ |
|
322
|
|
|
$this->st = $data->st; |
|
|
|
|
|
|
323
|
|
|
unset($data->st); |
|
324
|
|
|
} |
|
325
|
|
|
if(isset($data->postalCode)) |
|
326
|
|
|
{ |
|
327
|
|
|
$this->postalCode = $data->postalCode; |
|
|
|
|
|
|
328
|
|
|
unset($data->postalCode); |
|
329
|
|
|
} |
|
330
|
|
|
if(isset($data->c)) |
|
331
|
|
|
{ |
|
332
|
|
|
$this->c = $data->c; |
|
|
|
|
|
|
333
|
|
|
unset($data->c); |
|
334
|
|
|
} |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
private function editOrganizationElements($data) |
|
338
|
|
|
{ |
|
339
|
|
|
if(isset($data->o)) |
|
340
|
|
|
{ |
|
341
|
|
|
$this->o = $data->o; |
|
|
|
|
|
|
342
|
|
|
unset($data->o); |
|
343
|
|
|
} |
|
344
|
|
|
if(isset($data->title)) |
|
345
|
|
|
{ |
|
346
|
|
|
$this->title = $data->title; |
|
|
|
|
|
|
347
|
|
|
unset($data->title); |
|
348
|
|
|
} |
|
349
|
|
|
if(isset($data->ou)) |
|
350
|
|
|
{ |
|
351
|
|
|
$this->ou = $data->ou; |
|
|
|
|
|
|
352
|
|
|
unset($data->ou); |
|
353
|
|
|
} |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* Modify the user given the provided data object |
|
358
|
|
|
* |
|
359
|
|
|
* @param stdClass $data The user's new data |
|
360
|
|
|
* |
|
361
|
|
|
* @return boolean true if the user's data was changed, false otherwise |
|
362
|
|
|
*/ |
|
363
|
|
|
public function editUser($data) |
|
364
|
|
|
{ |
|
365
|
|
|
$this->enableReadWrite(); |
|
366
|
|
|
|
|
367
|
|
|
$this->checkForUnsettableElements($data); |
|
368
|
|
|
$this->editUserPassword($data); |
|
369
|
|
|
$this->editNames($data); |
|
370
|
|
|
$this->editAddressElements($data); |
|
371
|
|
|
$this->editOrganizationElements($data); |
|
372
|
|
|
|
|
373
|
|
|
if(isset($data->jpegPhoto)) |
|
374
|
|
|
{ |
|
375
|
|
|
$this->jpegPhoto = base64_decode($data->jpegPhoto); |
|
|
|
|
|
|
376
|
|
|
unset($data->jpegPhoto); |
|
377
|
|
|
} |
|
378
|
|
|
if(isset($data->mobile)) |
|
379
|
|
|
{ |
|
380
|
|
|
$this->mobile = $data->mobile; |
|
|
|
|
|
|
381
|
|
|
unset($data->mobile); |
|
382
|
|
|
} |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* Obtain the user's password reset hash |
|
387
|
|
|
* |
|
388
|
|
|
* @return string|false A hash if available, false otherwise |
|
389
|
|
|
*/ |
|
390
|
|
|
public function getPasswordResetHash() |
|
391
|
|
|
{ |
|
392
|
|
|
return false; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* Serialize the user data into a format usable by the json_encode method |
|
397
|
|
|
* |
|
398
|
|
|
* @return array A simple keyed array representing the user |
|
399
|
|
|
*/ |
|
400
|
|
|
public function jsonSerialize() |
|
401
|
|
|
{ |
|
402
|
|
|
$user = array(); |
|
403
|
|
|
$user['displayName'] = $this->displayName; |
|
|
|
|
|
|
404
|
|
|
$user['givenName'] = $this->givenName; |
|
|
|
|
|
|
405
|
|
|
$user['jpegPhoto'] = base64_encode($this->jpegPhoto); |
|
|
|
|
|
|
406
|
|
|
$user['mail'] = $this->mail; |
|
|
|
|
|
|
407
|
|
|
$user['mobile'] = $this->mobile; |
|
|
|
|
|
|
408
|
|
|
$user['uid'] = $this->uid; |
|
|
|
|
|
|
409
|
|
|
$user['o'] = $this->o; |
|
|
|
|
|
|
410
|
|
|
$user['title'] = $this->title; |
|
|
|
|
|
|
411
|
|
|
$user['titlenames'] = $this->getTitleNames(); |
|
412
|
|
|
$user['st'] = $this->st; |
|
|
|
|
|
|
413
|
|
|
$user['l'] = $this->l; |
|
|
|
|
|
|
414
|
|
|
$user['sn'] = $this->sn; |
|
|
|
|
|
|
415
|
|
|
$user['cn'] = $this->cn; |
|
|
|
|
|
|
416
|
|
|
$user['postalAddress'] = $this->postalAddress; |
|
|
|
|
|
|
417
|
|
|
$user['postalCode'] = $this->postalCode; |
|
|
|
|
|
|
418
|
|
|
$user['c'] = $this->c; |
|
|
|
|
|
|
419
|
|
|
$user['ou'] = $this->ou; |
|
|
|
|
|
|
420
|
|
|
$user['host'] = $this->host; |
|
|
|
|
|
|
421
|
|
|
$user['class'] = get_class($this); |
|
422
|
|
|
return $user; |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
/** |
|
426
|
|
|
* Serialize the user data into a VCARD 2.1 format |
|
427
|
|
|
* |
|
428
|
|
|
* @return string The VCARD for the user |
|
429
|
|
|
*/ |
|
430
|
|
|
public function getVcard() |
|
431
|
|
|
{ |
|
432
|
|
|
$ret = "BEGIN:VCARD\nVERSION:2.1\n"; |
|
433
|
|
|
$ret .= 'N:'.$this->sn.';'.$this->givenName."\n"; |
|
|
|
|
|
|
434
|
|
|
$ret .= 'FN:'.$this->givenName."\n"; |
|
|
|
|
|
|
435
|
|
|
$titles = $this->title; |
|
|
|
|
|
|
436
|
|
|
if($titles !== false) |
|
437
|
|
|
{ |
|
438
|
|
|
$ret .= 'TITLE:'.implode(',', $titles)."\n"; |
|
439
|
|
|
} |
|
440
|
|
|
$ret .= "ORG: Austin Artistic Reconstruction\n"; |
|
441
|
|
|
$ret .= 'TEL;TYPE=MOBILE,VOICE:'.$this->mobile."\n"; |
|
|
|
|
|
|
442
|
|
|
$ret .= 'EMAIL;TYPE=PREF,INTERNET:'.$this->mail."\n"; |
|
|
|
|
|
|
443
|
|
|
$ret .= "END:VCARD\n"; |
|
444
|
|
|
return $ret; |
|
445
|
|
|
} |
|
446
|
|
|
} |
|
447
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
|
448
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.