-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathAuthProvider.php
514 lines (485 loc) · 17.7 KB
/
AuthProvider.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php
namespace Flipside;
/**
* AuthProvider class
*
* This file describes the AuthProvider Singleton
*
* PHP version 5 and 7
*
* @author Patrick Boyd / problem@burningflipside.com
* @copyright Copyright (c) 2015, Austin Artistic Reconstruction
* @license http://www.apache.org/licenses/ Apache 2.0 License
*/
/**
* Allow other classes to be loaded as needed
*/
require_once('Autoload.php');
/**
* A Singleton class to abstract access to the authentication providers.
*
* This class is the primary method to access user data, login, and other authenication information.
*/
class AuthProvider extends Provider
{
/**
* Load the authentrication providers specified in the Settings $authProviders array
*
* @SuppressWarnings("StaticAccess")
*/
protected function __construct()
{
$settings = Settings::getInstance();
$this->methods = $settings->getClassesByPropName('authProviders');
}
/**
* Get the Auth\User class instance for the specified login
*
* Unlike the AuthProvider::login() function. This function will not impact the SESSION
*
* @param string $username The username of the User
* @param string $password The password of the User
*
* @return Auth\User|false The User with the specified credentials or false if the credentials are not valid
*/
public function getUserByLogin($username, $password)
{
$res = false;
$count = count($this->methods);
for($i = 0; $i < $count; $i++)
{
$res = $this->methods[$i]->login($username, $password);
if($res !== false)
{
return $this->methods[$i]->getUser($res);
}
}
return $res;
}
/**
* Use the provided credetials to log the user on
*
* @param string $username The username of the User
* @param string $password The password of the User
*
* @return true|false true if the login was successful, false otherwise
*/
public function login($username, $password)
{
$res = false;
$count = count($this->methods);
for($i = 0; $i < $count; $i++)
{
$res = $this->methods[$i]->login($username, $password);
if($res !== false)
{
if(isset($res['extended']) && isset($res['extended']['jpegphoto']))
{
$res['extended']['jpegphoto']=true;
}
FlipSession::setVar('AuthMethod', get_class($this->methods[$i]));
FlipSession::setVar('AuthData', $res);
break;
}
}
return $res;
}
/**
* Determine if the user is still logged on from the session data
*
* @param stdClass $data The AuthData from the session
* @param string $methodName The AuthMethod from the session
*
* @return true|false true if user is logged on, false otherwise
*/
public function isLoggedIn($data, $methodName)
{
$auth = $this->getMethodByName($methodName);
return $auth->isLoggedIn($data);
}
/**
* Obtain the currently logged in user from the session data
*
* @param stdClass $data The AuthData from the session
* @param string $methodName The AuthMethod from the session
*
* @return Auth\User|false The User instance if user is logged on, false otherwise
*/
public function getUser($data, $methodName)
{
$auth = $this->getMethodByName($methodName);
return $auth->getUser($data);
}
/**
* Merge or set the returnValue as appropriate
*
* @param false|Auth\Group|Auth\User $returnValue The value to merge to
* @param Auth\Group|Auth\User $res The value to merge from
*
* @return Auth\Group|false The merged returnValue
*/
public function mergeResult(&$returnValue, $res)
{
if($res === false)
{
return;
}
if($returnValue === false)
{
$returnValue = $res;
return;
}
$returnValue->merge($res);
}
/**
* Get an Auth\Group by its name
*
* @param string $name The name of the group
* @param string $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
*
* @return Auth\Group|false The Group instance if a group with that name exists, false otherwise
*/
public function getGroupByName($name, $methodName = false)
{
if($methodName === false)
{
return $this->callOnEach('getGroupByName', array($name));
}
$auth = $this->getMethodByName($methodName);
return $auth->getGroupByName($name);
}
/**
* Get an array of Auth\User from a filtered set
*
* @param Data\Filter|boolean $filter The filter conditions or false to retreive all
* @param array|boolean $select The user fields to obtain or false to obtain all
* @param integer|boolean $top The number of users to obtain or false to obtain all
* @param integer|boolean $skip The number of users to skip or false to skip none
* @param array|boolean $orderby The field to sort by and the method to sort or false to not sort
* @param string|boolean $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
*
* @return array|boolean An array of Auth\User objects or false if no users were found
*/
public function getUsersByFilter($filter, $select = false, $top = false, $skip = false, $orderby = false, $methodName = false)
{
return $this->callFunction($methodName, 'getUsersByFilter', array($filter, $select, $top, $skip, $orderby),
'current', false, array($this, 'mergeResult'));
}
/**
* Get an array of Auth\PendingUser from a filtered set
*
* @param Data\Filter|boolean $filter The filter conditions or false to retreive all
* @param array|boolean $select The user fields to obtain or false to obtain all
* @param integer|boolean $top The number of users to obtain or false to obtain all
* @param integer|boolean $skip The number of users to skip or false to skip none
* @param array|boolean $orderby The field to sort by and the method to sort or false to not sort
* @param string|boolean $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
*
* @return array|boolean An array of Auth\PendingUser objects or false if no pending users were found
*/
public function getPendingUsersByFilter($filter, $select = false, $top = false, $skip = false, $orderby = false, $methodName = false)
{
return $this->callFunction($methodName, 'getPendingUsersByFilter', array($filter, $select, $top, $skip, $orderby),
'pending', false, array($this, 'mergeResult'));
}
/**
* Get an array of Auth\Group from a filtered set
*
* @param Data\Filter|false $filter The filter conditions or false to retreive all
* @param array|false $select The group fields to obtain or false to obtain all
* @param integer|false $top The number of groups to obtain or false to obtain all
* @param integer|false $skip The number of groups to skip or false to skip none
* @param array|false $orderby The field to sort by and the method to sort or false to not sort
* @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
*
* @return array|false An array of Auth\Group objects or false if no pending users were found
*/
public function getGroupsByFilter($filter, $select = false, $top = false, $skip = false, $orderby = false, $methodName = false)
{
return $this->callFunction($methodName, 'getGroupsByFilter', array($filter, $select, $top, $skip, $orderby),
'current', false, array($this, 'mergeResult'));
}
/**
* Get the number of currently active users on the system
*
* @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
*
* @return integer The number of currently active users on the system
*/
public function getActiveUserCount($methodName = false)
{
if($methodName === false)
{
return $this->addFromEach('getActiveUserCount', 'current');
}
$auth = $this->getMethodByName($methodName);
return $auth->getActiveUserCount();
}
/**
* Get the number of currently pending users on the system
*
* @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
*
* @return integer The number of currently pending users on the system
*/
public function getPendingUserCount($methodName = false)
{
if($methodName === false)
{
return $this->addFromEach('getPendingUserCount', 'pending');
}
$auth = $this->getMethodByName($methodName);
return $auth->getPendingUserCount();
}
/**
* Get the number of current groups on the system
*
* @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
*
* @return integer The number of current groups on the system
*/
public function getGroupCount($methodName = false)
{
if($methodName === false)
{
return $this->addFromEach('getGroupCount', 'current');
}
$auth = $this->getMethodByName($methodName);
return $auth->getGroupCount();
}
/**
* Get the login links for all supplementary Authenitcation mechanisms
*
* This will return an array of links to any supplementary authentication mechanims. For example, Goodle is
* a supplementary authentication mechanism.
*
* @return array An array of suppmentary authentication mechanism links
*/
public function getSupplementaryLinks()
{
$ret = array();
$count = count($this->methods);
for($i = 0; $i < $count; $i++)
{
if($this->methods[$i]->supplement === false)
{
continue;
}
array_push($ret, $this->methods[$i]->getSupplementLink());
}
return $ret;
}
/**
* Impersonate the user specified
*
* This will replace the user in the session with the specified user. In order
* to undo this operation a user must logout.
*
* @param array|Auth\User $userArray Data representing the user
*/
public function impersonateUser($userArray)
{
if(!is_object($userArray))
{
$userArray = new $userArray['class']($userArray);
}
\Flipside\FlipSession::setUser($userArray);
}
/**
* Get the pending user reresented by the supplied hash
*
* @param string $hash The hash value representing the Penging User
* @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
*
* @return Auth\PendingUser|false The Auth\PendingUser instance or false if no user is matched by the provided hash
*/
public function getTempUserByHash($hash, $methodName = false)
{
if($methodName === false)
{
return $this->callOnEach('getTempUserByHash', array($hash), 'pending');
}
$auth = $this->getMethodByName($methodName);
return $auth->getTempUserByHash($hash);
}
/**
* Create a pending user
*
* @param array $user An array of information about the user to create
* @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
*
* @return boolean true if the user was successfully created. Otherwise false.
*/
public function createPendingUser($user, $methodName = false)
{
if($methodName === false)
{
$count = count($this->methods);
for($i = 0; $i < $count; $i++)
{
if($this->methods[$i]->pending === false)
{
continue;
}
$ret = $this->methods[$i]->createPendingUser($user);
if($ret !== false)
{
return true;
}
}
return false;
}
$auth = $this->getMethodByName($methodName);
return $auth->createPendingUser($user);
}
/**
* Convert a Auth\PendingUser into an Auth\User
*
* This will allow a previously pending user the ability to log on in the future as an active user. It will also
* have the side effect of logging the user on now.
*
* @param Auth\PendingUser $user The user to turn into a current user
* @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
*
* @return boolean true if the user was successfully created. Otherwise false.
*/
public function activatePendingUser($user, $methodName = false)
{
if($methodName === false)
{
$count = count($this->methods);
for($i = 0; $i < $count; $i++)
{
if($this->methods[$i]->current === false)
{
continue;
}
$ret = $this->methods[$i]->activatePendingUser($user);
if($ret !== false)
{
$this->impersonateUser($ret);
return true;
}
}
return false;
}
$auth = $this->getMethodByName($methodName);
return $auth->activatePendingUser($user);
}
/**
* Create a Auth\Group in the backend
*
* This will allow a new group to be created.
*
* @param array $group The Group to create
* @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
*
* @return boolean true if the group was successfully created. Otherwise false.
*/
public function createGroup($group, $methodName = false)
{
if($methodName === false)
{
$count = count($this->methods);
for($i = 0; $i < $count; $i++)
{
if($this->methods[$i]->current === false)
{
continue;
}
$ret = $this->methods[$i]->createGroup($group);
if($ret !== false)
{
return true;
}
}
return false;
}
$auth = $this->getMethodByName($methodName);
return $auth->createGroup($user);
}
/**
* Get a current user by a password reset hash
*
* @param string $hash The current password reset hash for the user
* @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
*
* @return Auth\User|false The user if the password reset hash is valid. Otherwise false.
*/
public function getUserByResetHash($hash, $methodName = false)
{
if($methodName === false)
{
return $this->callOnEach('getUserByResetHash', array($hash), 'current');
}
$auth = $this->getMethodByName($methodName);
if($auth === false)
{
return $this->getUserByResetHash($hash, false);
}
return $auth->getUserByResetHash($hash);
}
/**
* Get the Auth\Authenticator by host name
*
* @param string $host The host name used by the supplemental authentication mechanism
*
* @return Auth\Authenticator|false The Authenticator if the host is supported by a loaded Authenticator. Otherwise false.
*/
public function getSuplementalProviderByHost($host)
{
$count = count($this->methods);
for($i = 0; $i < $count; $i++)
{
if($this->methods[$i]->supplement === false)
{
continue;
}
if($this->methods[$i]->getHostName() === $host)
{
return $this->methods[$i];
}
}
return false;
}
/**
* Delete any pending users that match the filter
*
* @param \Data\Filter|boolean $filter The filter to delete with or false to delete all
* @param string|boolean $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
*
* @return boolean True if the users were deleted, false otherwise
*/
public function deletePendingUsersByFilter($filter, $methodName = false)
{
$users = $this->getPendingUsersByFilter($filter, false, false, false, false, $methodName);
if($users === false)
{
return false;
}
$count = count($users);
for($i = 0; $i < $count; $i++)
{
$users[$i]->delete();
}
return true;
}
/**
* Get the user by the one time access code
*
* @param string $key The user's access code
* @param string|boolean $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
*
* @return boolean|\Auth\User The User specified by the access code or false otherwise
*/
public function getUserByAccessCode($key, $methodName = false)
{
if($methodName === false)
{
return $this->callOnEach('getUserByAccessCode', array($key), 'current');
}
$auth = $this->getMethodByName($methodName);
return $auth->getUserByAccessCode($key);
}
}
/* vim: set tabstop=4 shiftwidth=4 expandtab: */