-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-penguin-settings.php
More file actions
598 lines (504 loc) · 17.9 KB
/
class-penguin-settings.php
File metadata and controls
598 lines (504 loc) · 17.9 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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<?php
class Penguin_Settings {
public $options;
private $option_key_general, $option_general, $option_key_roles, $option_roles,
$options_set, $roles, $prefix;
public function __construct() {
$this->prefix = "pgn";
$this->option_key_general = $this->prefix . "_general";
$this->option_key_roles = $this->prefix . "_roles";
}
public function load_all_options () {
$this->option_general = (array) get_option ( $this->option_key_general );
$this->option_roles = (array) get_option ( $this->option_key_roles );
$this->options = array_merge ( $this->option_general, $this->option_roles);
$this->give_values_if_not_set();
}
public function load_options( $key ) {
$options = (array) get_option ( $this->prefix . $key );
if ( ! empty( $this->options ) ) {
$this->options = array_merge ($this->options, $options);
}
else {
$this->options = $options;
}
}
private function give_values_if_not_set () {
$this->give_value_if_not_set ( 'penguin.example.com',
'server' );
$this->give_value_if_not_set ( '389',
'port' );
$this->give_value_if_not_set ( 'company\\',
'prefix' );
$this->give_value_if_not_set ( '@example.com',
'extension' );
$this->give_value_if_not_set ( 'ldap://',
'protocol' );
$this->give_value_if_not_set ( 0,
'referrals' );
$this->give_value_if_not_set ( 3,
'protocol_version' );
$this->give_value_if_not_set ( 'DC=value,DC=value,DC=value,',
'dn' );
$this->give_value_if_not_set ( 'samaccountname',
'login_field' );
$this->give_value_if_not_set ( '',
'filter' );
$this->give_value_if_not_set ( 'userprincipalname',
'email' );
$this->give_value_if_not_set ( 'givenname',
'first_name' );
$this->give_value_if_not_set ( 'sn',
'last_name');
$this->give_value_if_not_set ( 'user',
'objectclass');
$this->sync_priority_array();
/**
* If there is no default role setting or the default setting is set to a role
* that no longer exists, set the default role to the lowest priority role to be
* safe.
*/
if ( ( ! isset ( $this->options['default_role'] ) ) ||
( ! $this->role_exists($this->options['default_role']) ) ) {
$lowest_priority_role = $this->get_lowest_priority_role();
$lowest_priority_role_name = $lowest_priority_role[0];
$this->options['default_role'] = $lowest_priority_role_name;
}
$this->give_value_if_not_set ( 0,
'enable_group_mapping' );
// If there are mapped groups
if ( isset ( $this->options['groups'] ) ) {
// Reference the group array
$groups = &$this->options['groups'];
if ( is_array ( $groups ) ) {
$group_count = count ( $groups );
/**
* For every role a group is mapped to, check to see if it exists, if it
* doesn't, then give it the lowest priority.
*/
for ($i = 0; $i < $group_count; $i ++) {
if ( ! $this->role_exists ( $groups[$i][1] ) ) {
$lowest_priority_role = $this->get_lowest_priority_role();
$lowest_priority_role_name = $lowest_priority_role[0];
$groups[$i][1] = $lowest_priority_role_name;
}
}
}
}
$this->options_set = true;
}
private function give_value_if_not_set ( $default_value, $option_index ) {
$this->options[$option_index] = $this->get_option($option_index, null, null, $default_value);
}
public function get_option ( $option_index, $sub1 = null, $sub2 = null, $default = "" ) {
if ( isset ( $this->options ) ) {
if ( isset ( $this->options[$option_index] ) ) {
if ( is_null( $sub1 ) ) {
return $this->options[$option_index];
}
elseif (isset ( $this->options[$option_index][$sub1] ) ) {
if ( is_null ( $sub2 ) ) {
return $this->options[$option_index][$sub1];
}
elseif (isset ( $this->options[$option_index][$sub1][$sub2]) ) {
return $this->options[$option_index][$sub1][$sub2];
}
else {
return $default;
}
}
else {
return $default;
}
}
else {
return $default;
}
}
else {
die ("Error: Options must be loaded in first with load_all_options.");
}
}
/**
* If these settings haven't been saved before, or a new role has been created,
* then the priority array is empty or has some values that need to be set.
* We need to look at the differences between the list of roles and the list of
* roles than have been assigned priorities using array_diff_key(). The result
* will give us all the roles that haven't been assigned a priority yet.
*/
private function sync_priority_array() {
if ( ! isset( $this->roles ) ) {
$this->load_roles();
}
// Must be a reference (&)
$priority_array = &$this->options['priority'];
// Check if it's an array
if ( is_array ( $priority_array ) ) {
// Go through the priority array and unset any roles that don't exist
foreach ( $priority_array as $role_name => $priority ) {
if ( ! $this->role_exists( $role_name ) ) {
unset( $priority_array[$role_name] );
}
}
}
// Make an array of keys that contain a list of all the roles that aren't in the
// priority array
$missing_roles = array_diff_key( (array) $this->roles, (array) $priority_array );
// Find the lowest priority in the array
if ( is_array ( $priority_array ) ) {
$lowest_priority = max ( $priority_array );
}
else {
$lowest_priority = - 1;
$missing_roles = array_reverse( $missing_roles );
}
// If there are missing roles roles to be added
if ( ! empty( $missing_roles ) ) {
// Go through the missing roles and add the index
foreach ( $missing_roles as $role_key => $role_val ) {
// Add index and increment the lowest priority value to an even lower value
$this->add_missing_priority_index( $role_key, ++ $lowest_priority );
}
}
}
private function add_missing_priority_index ($role_key, $priority) {
if ( ! isset ( $this->options['priority'] ) ) {
$this->options['priority'] = array();
}
$this->options['priority'][$role_key] = $priority;
}
private function get_lowest_priority_role () {
$priority_array = $this->options['priority'];
$role_name = $this->get_highest_value( $priority_array );
/**
* If the lowest priority in the priority array actually doesn't exist
* (the role must have been deleted recently), then unset that index and look for
* a new lowest priority that we can actually use.
*/
// Return an array with the role lowest priority low and associated label.
return array ( $role_name, $this->roles[$role_name] ); #1
}
/**s
* Search for the highest value (lowest priority) using max() in the priority
* array.
*/
private function get_highest_value( $array ) {
return array_search ( max ( $array ), $array );
}
/**
* Outputs an associative array of all editable wordpress roles with the key being
* the role name and the value being the role label
*/
public function load_roles( $selected = false ) {
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once( ABSPATH . '/wp-admin/includes/user.php' );
}
$editable_roles = array_reverse( get_editable_roles() );
$out_array = array();
foreach ( $editable_roles as $role => $details ) {
$out_array [esc_attr($role)] = translate_user_role( $details['name'] );
}
$this->roles = $out_array;
// May need removal... however I'm keeping this for now since it ended up
// saving me a good 10 minutes of time.
if ( ! isset( $this->options ) ) {
die ("use load_all_options() before you load roles!");
}
}
private function role_exists ( $role_name_that_might_exist ) {
if ( ! isset( $this->roles ) ) {
$this->load_roles();
}
return array_key_exists ( $role_name_that_might_exist, $this->roles );
}
public function add_settings () {
// ---------------------------------------------------------------------
// GENERAL SECTION
// ---------------------------------------------------------------------
add_settings_section(
'penguin_general_section', // ID
'General Section', // Title
array ($this, 'general_section_desc'), // Callback function
$this->option_key_general // Menu page (should match a menu slug)
);
add_settings_field(
'pgn_server', // ID
'Server', // Title
array ($this, 'do_general_field_row') , // Callback function
$this->option_key_general, // Menu page (should match a menu slug)
'penguin_general_section', // Setting section this field belongs to
array ( 'server' )
);
add_settings_field(
'pgn_port', // ID
'Port', // Title
array ($this, 'do_general_field_row') , // Callback function
$this->option_key_general, // Menu page (should match a menu slug)
'penguin_general_section', // Setting section this field belongs to
array ( 'port' )
);
add_settings_field(
'pgn_prefix', // ID
'Prefix', // Title
array ($this, 'do_general_field_row') , // Callback function
$this->option_key_general, // Menu page (should match a menu slug)
'penguin_general_section', // Setting section this field belongs to
array ( 'prefix' )
);
add_settings_field(
'pgn_extension', // ID
'Extension (suffix)', // Title
array ($this, 'do_general_field_row') , // Callback function
$this->option_key_general, // Menu page (should match a menu slug)
'penguin_general_section', // Setting section this field belongs to
array ( 'extension' )
);
add_settings_field(
'pgn_protocol', // ID
'Protocol', // Title
array ($this, 'do_dropdown') , // Callback function
$this->option_key_general, // Menu page (should match a menu slug)
'penguin_general_section', // Setting section this field belongs to
array ( 'protocol', array( 'ldap://', 'ldaps://' ) )
);
add_settings_field(
'pgn_referrals', // ID
'Referrals', // Title
array ($this, 'do_dropdown') , // Callback function
$this->option_key_general, // Menu page (should match a menu slug)
'penguin_general_section', // Setting section this field belongs to
array ( 'referrals', array (0, 1) )
);
add_settings_field(
'pgn_protocol_version', // ID
'Protocol Version', // Title
array ($this, 'do_general_field_row') , // Callback function
$this->option_key_general, // Menu page (should match a menu slug)
'penguin_general_section', // Setting section this field belongs to
array ( 'protocol_version' )
);
add_settings_field(
'pgn_dn', // ID
'DN', // Title
array ($this, 'do_general_field_row') , // Callback function
$this->option_key_general, // Menu page (should match a menu slug)
'penguin_general_section', // Setting section this field belongs to
array ( 'dn' )
);
add_settings_field(
'pgn_login_field', // ID
'Login Field', // Title
array ($this, 'do_general_field_row') , // Callback function
$this->option_key_general, // Menu page (should match a menu slug)
'penguin_general_section', // Setting section this field belongs to
array ( 'login_field' )
);
add_settings_field(
'pgn_filter', // ID
'Additional Search Filter', // Title
array ($this, 'do_general_field_row') , // Callback function
$this->option_key_general, // Menu page (should match a menu slug)
'penguin_general_section', // Setting section this field belongs to
array ( 'filter' )
);
add_settings_field(
'pgn_email', // ID
'Email', // Title
array ($this, 'do_general_field_row') , // Callback function
$this->option_key_general, // Menu page (should match a menu slug)
'penguin_general_section', // Setting section this field belongs to
array ( 'email' )
);
add_settings_field(
'pgn_first_name', // ID
'First Name', // Title
array ($this, 'do_general_field_row') , // Callback function
$this->option_key_general, // Menu page (should match a menu slug)
'penguin_general_section', // Setting section this field belongs to
array ( 'first_name' )
);
add_settings_field(
'pgn_last_name', // ID
'Last Name', // Title
array ($this, 'do_general_field_row') , // Callback function
$this->option_key_general, // Menu page (should match a menu slug)
'penguin_general_section', // Setting section this field belongs to
array ( 'last_name' )
);
add_settings_field(
'pgn_objectclass', // ID
'Objectclass', // Title
array ($this, 'do_general_field_row') , // Callback function
$this->option_key_general, // Menu page (should match a menu slug)
'penguin_general_section', // Setting section this field belongs to
array ( 'objectclass' )
);
register_setting(
$this->option_key_general, // Options group
$this->option_key_general // Name of the option
);
// ---------------------------------------------------------------------
// ROLES SECTION
// ---------------------------------------------------------------------
add_settings_section(
'penguin_roles_section', // ID
'Roles Section', // Title
array ($this, 'roles_section_desc'), // Callback function
$this->option_key_roles // Menu page (should match a menu slug)
);
add_settings_field(
'penguin_priority',
'Priority',
array ($this, 'do_priority_section'),
$this->option_key_roles,
'penguin_roles_section'
);
add_settings_field(
'penguin_default_role', // ID
'Default Role', // Title
array ($this, 'do_field_default_role') , // Callback function
$this->option_key_roles, // Menu page (should match a menu slug)
'penguin_roles_section' // Setting section this field belongs to
);
add_settings_field(
'penguin_enable_mapping', // ID
'Enable group mapping', // Title
array ($this, 'do_enable_mapping_checkbox') , // Callback function
$this->option_key_roles, // Menu page (should match a menu slug)
'penguin_roles_section' // Setting section this field belongs to
);
add_settings_field(
'penguin_groups', // ID
'Group Mapping', // Title
array ($this, 'do_mapping_section') , // Callback function
$this->option_key_roles, // Menu page (should match a menu slug)
'penguin_roles_section', // Setting section this field belongs to
array ( 'groups' )
);
register_setting(
$this->option_key_roles, // Options group
$this->option_key_roles // Name of the option
);
}
public function general_section_desc () {
echo "<p>Configure Basic LDAP settings.</p>";
}
public function roles_section_desc () {
echo "<p>Map LDAP groups to WordPress roles.</p>";
}
public function opt_str ( $key1, $key2 = null, $key3 = null) {
$s = $key1;
if ( isset( $key2 ) ) $s .= '[' . $key2 . ']';
if ( isset( $key3 ) ) $s .= '[' . $key3 . ']';
return $s;
}
public function do_dropdown ( $args ) {
$func_name = "do_dropdown";
if ( ! isset( $args[0] ) ) {
$this->arg_error( $func_name, 0);
}
if ( ! isset( $args[1] ) ) {
$this->arg_error( $func_name, 1);
}
$key = $args[0];
$options = (array) $args[1];
$options_length = count ($options);
echo '<select name="' . $this->opt_str( $this->option_key_general, $key ) . '">';
$value = $this->options[$key];
for ($i = 0; $i < $options_length; $i ++) {
if ($options[$i] == $value) {
$selected = 'selected="selected"';
}
else {
$selected = '';
}
echo '<option ' . $selected . ' value="' . $options[$i] . '">' . $options[$i] . '</option>';
}
echo '</select>';
}
private function arg_error ( $func_name, $index ) {
die ("argument missing at $index in function $func_name");
}
public function do_general_field_row ( $args ) {
echo '<input type="text" ' /*id="ld-'.$args[0].*/ . '" name="' . $this->opt_str( $this->option_key_general, $args[0] ) .'" value="' .
$this->get_option( $args[0] ) . '"/></td>';
}
public function do_field_default_role () {
?>
<select id="default-group" name="<?php
echo $this->opt_str( $this->option_key_roles, 'default_role' );
?>">
<?php wp_dropdown_roles( $this->options['default_role'] );?>
</select>
<?php
}
public function do_enable_mapping_checkbox () {
?>
<input id="grpmp-enable" type="checkbox" name="pgn_roles[enable_group_mapping]"
value="1" <?php checked( 1, $this->options['enable_group_mapping'] ); ?>/>
<?php
}
public function do_mapping_section ( $key ) {
if ( ! isset( $this->roles ) ) {
$this->load_roles();
}
if ( $key[0] != 'groups' ) return;
$group_mapping_options = array (
'idPrefix' => 'grpmp-',
'value' => 'pgn_roles[groups]',
'attr' => 'name',
'rowSize' => 40,
'tableHeaders' => array ('Group Name', 'Role'),
'anchorID' => 'mapping-section',
'rowDataCount' => 2,
'initialSettings' => $this->get_option( 'groups' ),
'defaultSettings' => array ( "", "editor" )
);
echo '<div id="'.$group_mapping_options['anchorID'].'"></div>';
?>
<script type="text/javascript">
var options = <?php echo json_encode( $group_mapping_options );?>;
var roles = <?php echo json_encode( $this->roles ); ?>;
<?php
/**
* lowestPriorityRole is used to when an assigned role has been deleted.
* It is safest to assign the user to the role chosen to be the least
* powerful.
*/
?>
var enabled = "<?php echo $this->options['enable_group_mapping']; ?>";
</script>
<?php
}
public function do_priority_section () {
if ( ! isset( $this->roles ) ) {
$this->load_roles();
}
$priority_array = $this->get_option( 'priority' );
echo '<ul id="sortable">';
/**
* $priority_array is an array where the keys are roles and the values are
* priorities. What we need to do is go through the array and print out
* the keys and their corresponding values.
*/
$lowest_priority = -1; // Actually the highest value though.
if ( is_array ( $priority_array ) ) {
/**
* Go through each role in the priority array and display the role if it
* exists.
*/
foreach ( $priority_array as $priority_level ) {
$role = array_search( $priority_level, $priority_array );
if ( $this->role_exists( $role ) ) {
echo '<li><input type="text" style="display:none" name="'.
$this->option_key_roles.'[priority]['. $role .']" value="' .
$this->get_option('priority', $role) .
'" readonly></input><label class="priority-grab">'. $this->roles[$role] .
'</label></li>';
$lowest_priority = max( $lowest_priority, $this->get_option( 'priority', $role ) );
}
}
}
echo '</ul>';
}
}