forked from flourishlib/flourish-classes
-
Notifications
You must be signed in to change notification settings - Fork 10
/
fAuthorization.php
539 lines (458 loc) · 13.7 KB
/
fAuthorization.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<?php
/**
* Allows defining and checking user authentication via ACLs, authorization levels or a simple logged in/not logged in scheme
*
* @copyright Copyright (c) 2007-2011 Will Bond
* @author Will Bond [wb] <will@flourishlib.com>
* @license http://flourishlib.com/license
*
* @package Flourish
* @link http://flourishlib.com/fAuthorization
*
*/
class fAuthorization
{
// The following constants allow for nice looking callbacks to static methods
const addNamedIPRange = 'fAuthorization::addNamedIPRange';
const checkACL = 'fAuthorization::checkACL';
const checkAuthLevel = 'fAuthorization::checkAuthLevel';
const checkIP = 'fAuthorization::checkIP';
const checkLoggedIn = 'fAuthorization::checkLoggedIn';
const destroyUserInfo = 'fAuthorization::destroyUserInfo';
const getAuthLevels = 'fAuthorization::getAuthLevels';
const getLoginPage = 'fAuthorization::getLoginPage';
const getRequestedURL = 'fAuthorization::getRequestedURL';
const getUserACLs = 'fAuthorization::getUserACLs';
const getUserAuthLevel = 'fAuthorization::getUserAuthLevel';
const getUserToken = 'fAuthorization::getUserToken';
const requireACL = 'fAuthorization::requireACL';
const requireAuthLevel = 'fAuthorization::requireAuthLevel';
const requireLoggedIn = 'fAuthorization::requireLoggedIn';
const reset = 'fAuthorization::reset';
const setAuthLevels = 'fAuthorization::setAuthLevels';
const setLoginPage = 'fAuthorization::setLoginPage';
const setRequestedURL = 'fAuthorization::setRequestedURL';
const setUserACLs = 'fAuthorization::setUserACLs';
const setUserAuthLevel = 'fAuthorization::setUserAuthLevel';
const setUserToken = 'fAuthorization::setUserToken';
/**
* The valid auth levels
*
* @var array
*/
static private $levels = NULL;
/**
* The login page
*
* @var string
*/
static private $login_page = NULL;
/**
* Named IP ranges
*
* @var array
*/
static private $named_ip_ranges = array();
/**
* Adds a named IP address or range, or array of addresses and/or ranges
*
* This method allows ::checkIP() to be called with a name instead of the
* actual IPs.
*
* @param string $name The name to give the IP addresses/ranges
* @param mixed $ip_ranges This can be string (or array of strings) of the IPs or IP ranges to restrict to - please see ::checkIP() for format details
* @return void
*/
static public function addNamedIPRange($name, $ip_ranges)
{
self::$named_ip_ranges[$name] = $ip_ranges;
}
/**
* Checks to see if the logged in user meets the requirements of the ACL specified
*
* @param string $resource The resource we are checking permissions for
* @param string $permission The permission to require from the user
* @return boolean If the user has the required permissions
*/
static public function checkACL($resource, $permission)
{
if (self::getUserACLs() === NULL) {
return FALSE;
}
$acls = self::getUserACLs();
if (!isset($acls[$resource]) && !isset($acls['*'])) {
return FALSE;
}
if (isset($acls[$resource])) {
if (in_array($permission, $acls[$resource]) || in_array('*', $acls[$resource])) {
return TRUE;
}
}
if (isset($acls['*'])) {
if (in_array($permission, $acls['*']) || in_array('*', $acls['*'])) {
return TRUE;
}
}
return FALSE;
}
/**
* Checks to see if the logged in user has the specified auth level
*
* @param string $level The level to check against the logged in user's level
* @return boolean If the user has the required auth level
*/
static public function checkAuthLevel($level)
{
if (self::getUserAuthLevel()) {
self::validateAuthLevel(self::getUserAuthLevel());
self::validateAuthLevel($level);
$user_number = self::$levels[self::getUserAuthLevel()];
$required_number = self::$levels[$level];
if ($user_number >= $required_number) {
return TRUE;
}
}
return FALSE;
}
/**
* Checks to see if the user is from the IPs or IP ranges specified
*
* The `$ip_ranges` parameter can be either a single string, or an array of
* strings, each of which should be in one of the following formats:
*
* - A single IP address:
* - 192.168.1.1
* - 208.77.188.166
* - A CIDR range
* - 192.168.1.0/24
* - 208.77.188.160/28
* - An IP/subnet mask combination
* - 192.168.1.0/255.255.255.0
* - 208.77.188.160/255.255.255.240
*
* @param mixed $ip_ranges A string (or array of strings) of the IPs or IP ranges to restrict to - see method description for details
* @return boolean If the user is coming from (one of) the IPs or ranges specified
*/
static public function checkIP($ip_ranges)
{
// Check to see if a named IP range was specified
if (is_string($ip_ranges) && isset(self::$named_ip_ranges[$ip_ranges])) {
$ip_ranges = self::$named_ip_ranges[$ip_ranges];
}
if (!isset($_SERVER['REMOTE_ADDR'])) {
return FALSE;
}
// Get the remote IP and remove any IPv6 to IPv4 mapping
$user_ip = str_replace('::ffff:', '', $_SERVER['REMOTE_ADDR']);
$user_ip_long = ip2long($user_ip);
settype($ip_ranges, 'array');
foreach ($ip_ranges as $ip_range) {
if (strpos($ip_range, '/') === FALSE) {
$ip_range .= '/32';
}
list($range_ip, $range_mask) = explode('/', $ip_range);
if (strlen($range_mask) < 3) {
$mask_long = pow(2, 32) - pow(2, 32 - $range_mask);
} else {
$mask_long = ip2long($range_mask);
}
$range_ip_long = ip2long($range_ip);
if (($range_ip_long & $mask_long) != $range_ip_long) {
$proper_range_ip = long2ip($range_ip_long & $mask_long);
throw new fProgrammerException(
'The range base IP address specified, %1$s, is invalid for the CIDR range or subnet mask provided (%2$s). The proper IP is %3$s.',
$range_ip,
'/' . $range_mask,
$proper_range_ip
);
}
if (($user_ip_long & $mask_long) == $range_ip_long) {
return TRUE;
}
}
return FALSE;
}
/**
* Checks to see if the user has an auth level or ACLs defined
*
* @return boolean If the user is logged in
*/
static public function checkLoggedIn()
{
if (fSession::get(__CLASS__ . '::user_auth_level', NULL) !== NULL ||
fSession::get(__CLASS__ . '::user_acls', NULL) !== NULL ||
fSession::get(__CLASS__ . '::user_token', NULL) !== NULL) {
return TRUE;
}
return FALSE;
}
/**
* Destroys the user's auth level and/or ACLs
*
* @return void
*/
static public function destroyUserInfo()
{
fSession::delete(__CLASS__ . '::user_auth_level');
fSession::delete(__CLASS__ . '::user_acls');
fSession::delete(__CLASS__ . '::user_token');
fSession::delete(__CLASS__ . '::requested_url');
}
/**
* Gets the available auth levels for all users.
*/
static public function getAuthLevels()
{
return array_keys(self::$levels);
}
/**
* Returns the login page set via ::setLoginPage()
*
* @return string The login page users are redirected to if they don't have the required authorization
*/
static public function getLoginPage()
{
return self::$login_page;
}
/**
* Returns the URL requested before the user was redirected to the login page
*
* @param boolean $clear If the requested url should be cleared from the session after it is retrieved
* @param string $default_url The default URL to return if the user was not redirected
* @return string The URL that was requested before they were redirected to the login page
*/
static public function getRequestedURL($clear, $default_url=NULL)
{
$requested_url = fSession::get(__CLASS__ . '::requested_url', $default_url);
if ($clear) {
fSession::delete(__CLASS__ . '::requested_url');
}
return $requested_url;
}
/**
* Gets the ACLs for the logged in user
*
* @return array The logged in user's ACLs
*/
static public function getUserACLs()
{
return fSession::get(__CLASS__ . '::user_acls', NULL);
}
/**
* Gets the authorization level for the logged in user
*
* @return string The logged in user's auth level
*/
static public function getUserAuthLevel()
{
return fSession::get(__CLASS__ . '::user_auth_level', NULL);
}
/**
* Gets the value that was set as the user token, `NULL` if no token has been set
*
* @return mixed The user token that had been set, `NULL` if none
*/
static public function getUserToken()
{
return fSession::get(__CLASS__ . '::user_token', NULL);
}
/**
* Redirects the user to the login page
*
* @return void
*/
static private function redirect()
{
self::setRequestedURL(fURL::getWithQueryString());
fURL::redirect(self::$login_page);
}
/**
* Redirect the user to the login page if they do not have the permissions required
*
* @param string $resource The resource we are checking permissions for
* @param string $permission The permission to require from the user
* @return void
*/
static public function requireACL($resource, $permission)
{
self::validateLoginPage();
if (self::checkACL($resource, $permission)) {
return;
}
self::redirect();
}
/**
* Redirect the user to the login page if they do not have the auth level required
*
* @param string $level The level to check against the logged in user's level
* @return void
*/
static public function requireAuthLevel($level)
{
self::validateLoginPage();
if (self::checkAuthLevel($level)) {
return;
}
self::redirect();
}
/**
* Redirect the user to the login page if they do not have an auth level or ACLs
*
* @return void
*/
static public function requireLoggedIn()
{
self::validateLoginPage();
if (self::checkLoggedIn()) {
return;
}
self::redirect();
}
/**
* Resets the configuration of the class
*
* @internal
*
* @return void
*/
static public function reset()
{
self::$level = NULL;
self::$login_page = NULL;
self::$named_ip_ranges = array();
}
/**
* Sets the authorization levels to use for level checking
*
* @param array $levels An associative array of `(string) {level} => (integer) {value}`, for each level
* @return void
*/
static public function setAuthLevels($levels)
{
self::$levels = $levels;
}
/**
* Sets the login page to redirect users to
*
* @param string $url The URL of the login page
* @return void
*/
static public function setLoginPage($url)
{
self::$login_page = $url;
}
/**
* Sets the restricted URL requested by the user
*
* @param string $url The URL to save as the requested URL
* @return void
*/
static public function setRequestedURL($url)
{
fSession::set(__CLASS__ . '::requested_url', $url);
}
/**
* Sets the ACLs for the logged in user.
*
* Array should be formatted like:
*
* {{{
* array (
* (string) {resource name} => array(
* (mixed) {permission}, ...
* ), ...
* )
* }}}
*
* The resource name or the permission may be the single character `'*'`
* which acts as a wildcard.
*
* @param array $acls The logged in user's ACLs - see method description for format
* @return void
*/
static public function setUserACLs($acls)
{
fSession::set(__CLASS__ . '::user_acls', $acls);
fSession::regenerateID();
}
/**
* Sets the authorization level for the logged in user
*
* @param string $level The logged in user's auth level
* @return void
*/
static public function setUserAuthLevel($level)
{
self::validateAuthLevel($level);
fSession::set(__CLASS__ . '::user_auth_level', $level);
fSession::regenerateID();
}
/**
* Sets some piece of information to use to identify the current user
*
* @param mixed $token The user's token. This could be a user id, an email address, a user object, etc.
* @return void
*/
static public function setUserToken($token)
{
fSession::set(__CLASS__ . '::user_token', $token);
fSession::regenerateID();
}
/**
* Makes sure auth levels have been set, and that the specified auth level is valid
*
* @param string $level The level to validate
* @return void
*/
static private function validateAuthLevel($level=NULL)
{
if (self::$levels === NULL) {
throw new fProgrammerException(
'No authorization levels have been set, please call %s',
__CLASS__ . '::setAuthLevels()'
);
}
if ($level !== NULL && !isset(self::$levels[$level])) {
throw new fProgrammerException(
'The authorization level specified, %1$s, is invalid. Must be one of: %2$s.',
$level,
join(', ', array_keys(self::$levels))
);
}
}
/**
* Makes sure a login page has been defined
*
* @return void
*/
static private function validateLoginPage()
{
if (self::$login_page === NULL) {
throw new fProgrammerException(
'No login page has been set, please call %s',
__CLASS__ . '::setLoginPage()'
);
}
}
}
/**
* Copyright (c) 2007-2011 Will Bond <will@flourishlib.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/