-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
383 lines (321 loc) · 12.4 KB
/
Copy pathplugin.php
File metadata and controls
383 lines (321 loc) · 12.4 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
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
<?php
/*
Plugin Name: Authorization Manager
Plugin URI: http://code.google.com/p/yourls-authmgr-plugin/
Description: Restrict classes of users to specific functions
Version: 1.0
Author: nicwaller
Author URI: http://code.google.com/u/101717938102134699062/
*/
// No direct call
if( !defined( 'YOURLS_ABSPATH' ) ) die();
/****************** SET UP CONSTANTS ******************/
// Define constants for critical filters
define( 'AUTHMGR_ALLOW', 'filter_authmgr_allow' );
define( 'AUTHMGR_HASROLE', 'filter_authmgr_hasrole' );
// Define constants used for naming roles (but they don't work in config.php)
class AuthmgrRoles {
const Administrator = 'Administrator';
const ShortAdministrator = "ShortAdministrator";
const Editor = 'Editor';
const Contributor = 'Contributor';
}
// Define constants used for naming capabilities
class AuthmgrCapability {
const ShowAdmin = 'ShowAdmin'; // only display admin panel
const AddURL = 'AddURL';
const DeleteURL = 'DeleteURL';
const EditURL = 'EditURL';
const ManagePlugins = 'ManagePlugins';
const API = 'API';
const ViewStats = 'ViewStats';
}
/********** Add hooks to intercept functionality in CORE ********/
yourls_add_action( 'load_template_infos', 'authmgr_intercept_stats' );
function authmgr_intercept_stats() { authmgr_require_capability( AuthmgrCapability::ViewStats ); }
yourls_add_action( 'api', 'authmgr_intercept_api' );
function authmgr_intercept_api() { authmgr_require_capability( AuthmgrCapability::API ); }
yourls_add_action( 'admin_init', 'authmgr_intercept_admin' );
function authmgr_intercept_admin() {
authmgr_require_capability( AuthmgrCapability::ShowAdmin );
// we use this GET param to send up a feedback notice to user
if ( isset( $_GET['access'] ) && $_GET['access']=='denied' ) {
yourls_add_notice('Access Denied');
}
$action_capability_map = array(
'add' => AuthmgrCapability::AddURL,
'delete' => AuthmgrCapability::DeleteURL,
'edit_display' => AuthmgrCapability::EditURL,
'edit_save' => AuthmgrCapability::EditURL,
'activate' => AuthmgrCapability::ManagePlugins,
'deactivate' => AuthmgrCapability::ManagePlugins,
);
// Allow other plugins to modify the action_capability_map, they can use
// this to add capabilities for additional Ajax requests.
// Plugins that add new Ajax requests need to do this if they wan't them to
// work in combination with the Authmgr plugin.
$action_capability_map = yourls_apply_filter('authmgr_action_capability_map_filter', $action_capability_map);
// intercept requests for plugin management
if ( isset( $_REQUEST['plugin'] ) ) {
$action_keyword = $_REQUEST['action'];
$cap_needed = $action_capability_map[$action_keyword];
if ( $cap_needed !== NULL && authmgr_have_capability( $cap_needed ) !== true) {
yourls_redirect( yourls_admin_url( '?access=denied' ), 302 );
}
}
// also intercept AJAX requests
if ( yourls_is_Ajax() ) {
$action_keyword = $_REQUEST['action'];
$cap_needed = $action_capability_map[$action_keyword];
if ( authmgr_have_capability( $cap_needed ) !== true) {
$err = array();
$err['status'] = 'fail';
$err['code'] = 'error:authorization';
$err['message'] = 'Access Denied';
$err['errorCode'] = '403';
echo json_encode( $err );
die();
}
}
}
yourls_add_filter( 'logout_link', 'authmgr_html_append_roles' );
function authmgr_html_append_roles( $original ) {
$authenticated = yourls_is_valid_user();
if ( $authenticated === true ) {
$listcaps = implode(', ', authmgr_enumerate_current_capabilities());
return '<div title="'.$listcaps.'">'.$original.'</div>';
} else {
return $original;
}
}
/**************** CAPABILITY TEST/ENUMERATION ****************/
/*
* If capability is not permitted in current context, then abort.
* This is the most basic way to intercept unauthorized usage.
*/
function authmgr_require_capability( $capability ) {
if ( !authmgr_have_capability( $capability ) ) {
// TODO: display a much nicer error page
//die('Sorry, you are not authorized for the action: '.$capability);
yourls_redirect( yourls_admin_url( '?access=denied' ), 302 );
die();
}
}
/*
* Returns array of capabilities currently available.
*/
function authmgr_enumerate_current_capabilities() {
$current_capabilities = array();
$all_capabilities = authmgr_enumerate_all_capabilities();
foreach ( $all_capabilities as $cap ) {
if ( authmgr_have_capability( $cap ) ) {
$current_capabilities[] = $cap;
}
}
return $current_capabilities;
}
function authmgr_enumerate_all_capabilities() {
// TODO: generalize this, instead of just repeating the total declaration
return array(
AuthmgrCapability::ShowAdmin,
AuthmgrCapability::AddURL,
AuthmgrCapability::DeleteURL,
AuthmgrCapability::EditURL,
AuthmgrCapability::ManagePlugins,
AuthmgrCapability::API,
AuthmgrCapability::ViewStats,
);
}
/*
* Is the requested capability permitted in this context?
*/
function authmgr_have_capability( $capability ) {
return yourls_apply_filter( AUTHMGR_ALLOW, false, $capability);
}
/******************* FILTERS THAT GRANT CAPABILITIES *****************************/
/* By filtering AUTHMGR_ALLOW, you can grant capabilities without using roles. */
/*********************************************************************************/
/*
* What capabilities are always available, including anonymous users?
*/
yourls_add_filter( AUTHMGR_ALLOW, 'authmgr_check_anon_capability', 5 );
function authmgr_check_anon_capability( $original, $capability ) {
global $authmgr_anon_capabilities;
// Shortcut - trust approval given by earlier filters
if ( $original === true ) return true;
// Make sure the anon rights list has been setup
authmgr_environment_check();
// Check list of capabilities that don't require authentication
return in_array( $capability, $authmgr_anon_capabilities );
}
/*
* What capabilities are available through role assignments to the active user?
*/
yourls_add_filter( AUTHMGR_ALLOW, 'authmgr_check_user_capability', 10 );
function authmgr_check_user_capability( $original, $capability ) {
global $authmgr_role_capabilities;
// Shortcut - trust approval given by earlier filters
if ( $original === true ) return true;
// ensure $authmgr_role_capabilities has been set up
authmgr_environment_check();
// If the user is not authenticated, then give up because only users have roles.
$authenticated = yourls_is_valid_user();
if ( $authenticated !== true )
return false;
// Enumerate the capabilities available to this user through roles
$user_caps = array();
foreach ( $authmgr_role_capabilities as $rolename => $rolecaps ) {
if ( authmgr_user_has_role( YOURLS_USER, $rolename ) ) {
$user_caps = array_merge( $user_caps, $rolecaps );
}
}
$user_caps = array_unique( $user_caps );
// Is the desired capability in the enumerated list of capabilities?
return in_array( $capability, $user_caps );
}
/*
* If the user is connecting from an IP address designated for admins,
* then all capabilities are automatically granted.
*
* By default, only 127.0.0.0/8 (localhost) is an admin range.
*/
yourls_add_filter( AUTHMGR_ALLOW, 'authmgr_check_admin_ipranges', 15 );
function authmgr_check_admin_ipranges( $original, $capability ) {
global $authmgr_admin_ipranges;
// Shortcut - trust approval given by earlier filters
if ( $original === true ) return true;
// ensure $authmgr_admin_ipranges is setup
authmgr_environment_check();
foreach ($authmgr_admin_ipranges as $range) {
if ( authmgr_cidr_match( $_SERVER['REMOTE_ADDR'], $range ) )
return true;
}
return $original; // effectively returns false
}
/*
* What capabilities are available when making API requests without a username?
*/
yourls_add_filter( AUTHMGR_ALLOW, 'authmgr_check_apiuser_capability', 15 );
function authmgr_check_apiuser_capability( $original, $capability ) {
// Shortcut - trust approval given by earlier filters
if ( $original === true ) return true;
// In API mode and not using user/path authn? Let it go.
if ( yourls_is_API() && !isset($_REQUEST['username']) )
return true;
// TODO: add controls for actions, like
// shorturl, stats, db-stats, url-stats, expand
return $original;
}
/******************** ROLE TEST AND ENUMERATION ***********************/
/*
* Determine whether a specific user has a role.
*/
function authmgr_user_has_role( $username, $rolename ) {
return yourls_apply_filter( AUTHMGR_HASROLE, false, $username, $rolename );
}
// ******************* FILTERS THAT GRANT ROLE MEMBERSHIP *********************
// By filtering AUTHMGR_HASROLE, you can connect internal roles to something else.
// Any filter handlers should execute as quickly as possible.
/*
* What role memberships are defined for the user in user/config.php?
*/
yourls_add_filter( AUTHMGR_HASROLE, 'authmgr_user_has_role_in_config');
function authmgr_user_has_role_in_config( $original, $username, $rolename ) {
global $authmgr_role_assignment;
// if no role assignments are created, grant everything
// so the site still works even if stuff is configured wrong
if ( empty( $authmgr_role_assignment ) )
return true;
// do this the case-insensitive way
// the entire array was made lowercase in environment check
$username = strtolower($username);
$rolename = strtolower($rolename);
// if the role doesn't exist, give up now.
if ( !in_array( $rolename, array_keys( $authmgr_role_assignment ) ) )
return false;
$users_in_role = $authmgr_role_assignment[$rolename];
return in_array( $username, $users_in_role );
}
/********************* VALIDATE CONFIGURATION ************************/
function authmgr_environment_check() {
global $authmgr_anon_capabilities;
global $authmgr_role_capabilities;
global $authmgr_role_assignment;
if ( !isset( $authmgr_anon_capabilities) ) {
$authmgr_anon_capabilities = array(
AuthmgrCapability::API,
AuthmgrCapability::ShowAdmin,//TODO: hack! how to allow logon page?
);
// Allow plugins to change the capabilities of Anonymous users.
$authmgr_anon_capabilities = yourls_apply_filter('authmgr_anon_capabilities_filter', $authmgr_anon_capabilities);
}
if ( !isset( $authmgr_role_capabilities) ) {
$authmgr_role_capabilities = array(
AuthmgrRoles::Administrator => array(
AuthmgrCapability::ShowAdmin,
AuthmgrCapability::AddURL,
AuthmgrCapability::DeleteURL,
AuthmgrCapability::EditURL,
AuthmgrCapability::ManagePlugins,
AuthmgrCapability::API,
AuthmgrCapability::ViewStats,
),
AuthmgrRoles::ShortAdministrator => array(
AuthmgrCapability::ShowAdmin,
AuthmgrCapability::AddURL,
AuthmgrCapability::DeleteURL,
AuthmgrCapability::EditURL,
AuthmgrCapability::API,
AuthmgrCapability::ViewStats,
),
AuthmgrRoles::Editor => array(
AuthmgrCapability::ShowAdmin,
AuthmgrCapability::AddURL,
AuthmgrCapability::EditURL,
AuthmgrCapability::DeleteURL,
AuthmgrCapability::ViewStats,
),
AuthmgrRoles::Contributor => array(
AuthmgrCapability::ShowAdmin,
AuthmgrCapability::AddURL,
AuthmgrCapability::ViewStats,
),
);
// Allow plugins to change the capabilities of the roles.
$authmgr_role_capabilities = yourls_apply_filter('authmgr_role_capabilities_filter', $authmgr_role_capabilities);
}
if ( !isset( $authmgr_role_assignment ) ) {
$authmgr_role_assignment = array();
}
if ( !isset( $authmgr_iprange_roles ) ) {
$authmgr_admin_ipranges = array(
'127.0.0.0/8',
);
}
// convert role assignment table to lower case if it hasn't been done already
// this makes searches much easier!
// TODO: avoid doing this every time we validate
$authmgr_role_assignment_lower = array();
foreach ( $authmgr_role_assignment as $key => $value ) {
$t_key = strtolower( $key );
$t_value = array_map('strtolower', $value);
$authmgr_role_assignment_lower[$t_key] = $t_value;
}
$authmgr_role_assignment = $authmgr_role_assignment_lower;
unset($authmgr_role_assignment_lower);
return true;
}
// ***************** GENERAL UTILITY FUNCTIONS ********************
/*
* Borrowed from:
* http://stackoverflow.com/questions/594112/matching-an-ip-to-a-cidr-mask-in-php5
*/
function authmgr_cidr_match($ip, $range)
{
list ($subnet, $bits) = split('/', $range);
$ip = ip2long($ip);
$subnet = ip2long($subnet);
$mask = -1 << (32 - $bits);
$subnet &= $mask; # nb: in case the supplied subnet wasn't correctly aligned
return ($ip & $mask) == $subnet;
}