Projet

Général

Profil

Paste
Télécharger (41,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_servers / LdapServerAdmin.class.php @ 91af538d

1
<?php
2

    
3
/**
4
 * @file
5
 * LDAP Server Admin Class.
6
 */
7

    
8
module_load_include('php', 'ldap_servers', 'LdapServer.class');
9
/**
10
 *
11
 */
12
class LdapServerAdmin extends LdapServer {
13

    
14
  public $bindpw_new = FALSE;
15
  public $bindpw_clear = FALSE;
16

    
17
  /**
18
   * @param $type
19
   *   = 'all', 'enabled'
20
   */
21
  public static function getLdapServerObjects($sid = NULL, $type = NULL, $class = 'LdapServer', $reset = FALSE) {
22
    $servers = [];
23
    if (module_exists('ctools')) {
24
      ctools_include('export');
25
      if ($reset) {
26
        ctools_export_load_object_reset('ldap_servers');
27
      }
28
      $select = ctools_export_load_object('ldap_servers', 'all');
29
    }
30
    else {
31
      try {
32
        $select = db_select('ldap_servers', 'ldap_servers')
33
          ->fields('ldap_servers')
34
          ->execute();
35
      }
36
      catch (Exception $e) {
37
        drupal_set_message(t('server index query failed. Message = %message, query= %query',
38
          ['%message' => $e->getMessage(), '%query' => $e->query_string]), 'error');
39
        return [];
40
      }
41
    }
42
    foreach ($select as $result) {
43
      $servers[$result->sid] = ($class == 'LdapServer') ? new LdapServer($result->sid) : new LdapServerAdmin($result->sid);
44
    }
45
    return $servers;
46

    
47
  }
48

    
49
  /**
50
   *
51
   */
52
  public function __construct($sid) {
53
    parent::__construct($sid);
54
  }
55

    
56
  /**
57
   *
58
   */
59
  protected function populateFromDrupalForm($op, $values) {
60
    $this->inDatabase = ($op == 'edit');
61
    $this->sid = trim($values['sid']);
62
    $this->name = trim($values['name']);
63
    $this->status = ($values['status']) ? 1 : 0;
64
    $this->ldap_type = trim($values['ldap_type']);
65
    $this->address = trim($values['address']);
66
    $this->port = trim($values['port']);
67
    $this->tls = trim($values['tls']);
68
    $this->followrefs = trim($values['followrefs']);
69
    $this->bind_method = trim($values['bind_method']);
70
    $this->binddn = trim($values['binddn']);
71
    if (trim($values['bindpw'])) {
72
      $this->bindpw_new = trim($values['bindpw']);
73
    }
74
    $this->user_dn_expression = trim($values['user_dn_expression']);
75
    $this->basedn = $this->linesToArray(trim($values['basedn']));
76
    $this->user_attr = drupal_strtolower(trim($values['user_attr']));
77
    $this->picture_attr = drupal_strtolower(trim($values['picture_attr']));
78
    $this->account_name_attr = drupal_strtolower(trim($values['account_name_attr']));
79
    $this->mail_attr = drupal_strtolower(trim($values['mail_attr']));
80
    $this->mail_template = trim($values['mail_template']);
81
    $this->unique_persistent_attr = drupal_strtolower(trim($values['unique_persistent_attr']));
82
    $this->unique_persistent_attr_binary = trim($values['unique_persistent_attr_binary']);
83
    $this->ldapToDrupalUserPhp = $values['ldap_to_drupal_user'];
84
    $this->testingDrupalUsername = trim($values['testing_drupal_username']);
85
    $this->testingDrupalUserDn = trim($values['testing_drupal_user_dn']);
86
    $this->groupFunctionalityUnused = $values['grp_unused'];
87
    $this->groupObjectClass = drupal_strtolower(trim($values['grp_object_cat']));
88
    $this->groupNested = trim($values['grp_nested']);
89

    
90
    $this->groupUserMembershipsAttrExists = trim($values['grp_user_memb_attr_exists']);
91
    $this->groupUserMembershipsAttr = drupal_strtolower(trim($values['grp_user_memb_attr']));
92

    
93
    $this->groupMembershipsAttr = drupal_strtolower(trim($values['grp_memb_attr']));
94

    
95
    $this->groupMembershipsAttrMatchingUserAttr = drupal_strtolower(trim($values['grp_memb_attr_match_user_attr']));
96

    
97
    $this->groupDeriveFromDn = trim($values['grp_derive_from_dn']);
98
    $this->groupDeriveFromDnAttr = drupal_strtolower(trim($values['grp_derive_from_dn_attr']));
99
    $this->groupTestGroupDn = trim($values['grp_test_grp_dn']);
100
    $this->groupTestGroupDnWriteable = trim($values['grp_test_grp_dn_writeable']);
101

    
102
    $this->searchPagination = ($values['search_pagination']) ? 1 : 0;
103
    $this->searchPageSize = trim($values['search_page_size']);
104

    
105
  }
106

    
107
  /**
108
   * @param string enum $op
109
   *   'add', 'update'.
110
   */
111
  public function save($op) {
112

    
113
    $values = new stdClass();
114

    
115
    foreach ($this->field_to_properties_map() as $field_name => $property_name) {
116
      $field_name_lcase = drupal_strtolower($field_name);
117
      $values->{$field_name_lcase} = $this->{$property_name};
118
    }
119
    if (isset($this->bindpw) && $this->bindpw) {
120
      $values->bindpw = ldap_servers_encrypt($this->bindpw);
121
    }
122
    if ($this->bindpw_new) {
123
      $values->bindpw = ldap_servers_encrypt($this->bindpw_new);
124
    }
125
    elseif ($this->bindpw_clear) {
126
      $values->bindpw = NULL;
127
    }
128

    
129
    $values->tls = (int) $this->tls;
130
    $values->followrefs = (int) $this->followrefs;
131

    
132
    if (module_exists('ctools')) {
133
      ctools_include('export');
134
      // Populate our object with ctool's properties.
135
      $object = ctools_export_crud_new('ldap_servers');
136

    
137
      foreach ($object as $property => $value) {
138
        $property_lcase = drupal_strtolower($property);
139
        if (!isset($values->$property) || !isset($values->$property_lcase)) {
140
          $values->$property_lcase = $value;
141
        }
142
      }
143

    
144
      try {
145
        $values->export_type = NULL;
146
        $result = ctools_export_crud_save('ldap_servers', $values);
147
      }
148
      catch (Exception $e) {
149
        $values->export_type = EXPORT_IN_DATABASE;
150
        $result = ctools_export_crud_save('ldap_servers', $values);
151
      }
152

    
153
      // ctools_export_crud_save doesn't invalidate cache.
154
      ctools_export_load_object_reset('ldap_servers');
155

    
156
    }
157
    // Directly via db.
158
    else {
159
      unset($values->numeric_sid);
160
      if ($op == 'add') {
161
        $result = drupal_write_record('ldap_servers', $values);
162
      }
163
      else {
164
        $result = drupal_write_record('ldap_servers', $values, 'sid');
165
      }
166
      ldap_servers_cache_clear();
167

    
168
    }
169

    
170
    if ($result) {
171
      $this->inDatabase = TRUE;
172
    }
173
    else {
174
      drupal_set_message(t('Failed to write LDAP Server to the database.'));
175
    }
176
  }
177

    
178
  /**
179
   *
180
   */
181
  public function delete($sid) {
182
    if ($sid == $this->sid) {
183
      $result = db_delete('ldap_servers')->condition('sid', $sid)->execute();
184
      if (module_exists('ctools')) {
185
        ctools_include('export');
186
        // Invalidate cache.
187
        ctools_export_load_object_reset('ldap_servers');
188
      }
189
      $this->inDatabase = FALSE;
190
      return $result;
191
    }
192
    else {
193
      return FALSE;
194
    }
195
  }
196

    
197
  /**
198
   *
199
   */
200
  public function getLdapServerActions() {
201
    $switch = ($this->status) ? 'disable' : 'enable';
202
    $actions = [];
203
    $actions[] = l(t('edit'), LDAP_SERVERS_MENU_BASE_PATH . '/servers/edit/' . $this->sid);
204
    if (property_exists($this, 'type')) {
205
      if ($this->type == 'Overridden') {
206
        $actions[] = l(t('revert'), LDAP_SERVERS_MENU_BASE_PATH . '/servers/delete/' . $this->sid);
207
      }
208
      if ($this->type == 'Normal') {
209
        $actions[] = l(t('delete'), LDAP_SERVERS_MENU_BASE_PATH . '/servers/delete/' . $this->sid);
210
      }
211
    }
212
    else {
213
      $actions[] = l(t('delete'), LDAP_SERVERS_MENU_BASE_PATH . '/servers/delete/' . $this->sid);
214
    }
215
    $actions[] = l(t('test'), LDAP_SERVERS_MENU_BASE_PATH . '/servers/test/' . $this->sid);
216
    $actions[] = l($switch, LDAP_SERVERS_MENU_BASE_PATH . '/servers/' . $switch . '/' . $this->sid);
217
    return $actions;
218
  }
219

    
220
  /**
221
   *
222
   */
223
  public function drupalForm($op) {
224

    
225
    $form['server'] = [
226
      '#type' => 'fieldset',
227
      '#title' => t('Connection settings'),
228
      '#collapsible' => TRUE,
229
      '#collapsed' => TRUE,
230
    ];
231

    
232
    $form['bind_method'] = [
233
      '#type' => 'fieldset',
234
      '#title' => t('Binding Method'),
235
      '#description' => t('How the Drupal system is authenticated by the LDAP server.'),
236
      '#collapsible' => TRUE,
237
      '#collapsed' => TRUE,
238
    ];
239

    
240
    $form['users'] = [
241
      '#type' => 'fieldset',
242
      '#title' => t('LDAP User to Drupal User Relationship'),
243
      '#description' => t('How are LDAP user entries found based on Drupal username or email?  And vice-versa?
244
       Needed for LDAP Authentication and Authorization functionality.'),
245
      '#collapsible' => TRUE,
246
      '#collapsed' => TRUE,
247
    ];
248

    
249
    $form['groups'] = [
250
      '#type' => 'fieldset',
251
      '#title' => t('LDAP Group Configuration'),
252
      '#description' => t('How are groups defined on your LDAP server?  This varies slightly from one LDAP implementation to another
253
      such as Active Directory, Novell, OpenLDAP, etc. Check everything that is true and enter all the values you know.'),
254
      '#collapsible' => TRUE,
255
      '#collapsed' => TRUE,
256
    ];
257

    
258
    $supports = (ldap_servers_php_supports_pagination()) ? t('support pagination!') : t('NOT support pagination.');
259
    $form['pagination'] = [
260
      '#type' => 'fieldset',
261
      '#title' => t('LDAP Pagination'),
262
      '#description' => t('In PHP 5.4, pagination is supported in ldap queries.
263
      A patch to earlier versions of PHP also supports this.')
264
      . ' <strong>' . t('This PHP installation appears to') . ' ' . $supports . '</strong> '
265
      . '<p>' . t('The advantage to pagination support is that if an ldap server is setup to return only
266
      1000 entries at a time,
267
      you can use page through 1000 records at a time;
268
      without pagination you would never see more than the first 1000 entries.
269
      Pagination is most useful when large queries for batch creating or
270
      synching accounts are used.  If you are not using this server for such
271
      tasks, its recommended to leave pagination disabled.') . '</p>',
272
      '#collapsible' => TRUE,
273
      '#collapsed' => !ldap_servers_php_supports_pagination(),
274
    ];
275

    
276
    $field_to_prop_maps = $this->field_to_properties_map();
277
    foreach ($this->fields() as $field_id => $field) {
278
      if (isset($field['form'])) {
279

    
280
        if (!isset($field['form']['required']) && isset($field['schema']['not null']) && $field['form']['#type'] != 'checkbox') {
281
          $field['form']['#required'] = (boolean) $field['schema']['not null'];
282
        }
283
        if (isset($field['schema']['length']) && !isset($field['form']['#maxlength'])) {
284
          $field['form']['#maxlength'] = $field['schema']['length'];
285
        }
286
        if (isset($field_to_prop_maps[$field_id])) {
287
          $field['form']['#default_value'] = $this->{$field_to_prop_maps[$field_id]};
288
        }
289
        $fieldset = @$field['form']['fieldset'];
290
        if ($fieldset) {
291
          unset($field['form']['fieldset']);
292
          $form[$fieldset][$field_id] = $field['form'];
293
        }
294
        else {
295
          $form[$field_id] = $field['form'];
296
        }
297
      }
298
    }
299

    
300
    $form['server']['sid']['#disabled'] = ($op == 'edit');
301

    
302
    if (!function_exists('ldap_set_rebind_proc')) {
303
      $form['server']['followrefs']['#disabled'] = TRUE;
304
      $form['server']['followrefs']['#description'] = t('This functionality is disabled because the function ldap_set_rebind_proc can not be found on this server.  Perhaps your version of php does not have this function.  See php.net/manual/en/function.ldap-set-rebind-proc.php') . $form['server']['followrefs']['#description'];
305
    }
306

    
307
    $form['server']['tls']['#required'] = FALSE;
308
    $form['server']['followrefs']['#required'] = FALSE;
309
    $form['bind_method']['bind_method']['#default_value'] = ($this->bind_method) ? $this->bind_method : LDAP_SERVERS_BIND_METHOD_DEFAULT;
310
    $form['users']['basedn']['#default_value'] = $this->arrayToLines($this->basedn);
311

    
312
    if ($this->bindpw) {
313
      $pwd_directions = t('You currently have a password stored in the database.
314
      Leave password field empty to leave password unchanged.  Enter a new password
315
      to replace the current password.  Check the checkbox below to simply
316
      remove it from the database.');
317
      $pwd_class = 'ldap-pwd-present';
318
    }
319
    else {
320
      $pwd_directions = t('No password is currently stored in the database.
321
      If you are using a service account, enter one.');
322
      if ($this->bind_method == LDAP_SERVERS_BIND_METHOD_SERVICE_ACCT) {
323
        $pwd_class = 'ldap-pwd-abscent';
324
      }
325
      else {
326
        $pwd_class = 'ldap-pwd-not-applicable';
327
      }
328
    }
329

    
330
    $action = ($op == 'add') ? 'Add' : 'Update';
331
    $form['submit'] = [
332
      '#type' => 'submit',
333
      '#value' => $action,
334
      '#weight' => 100,
335
    ];
336

    
337
    return $form;
338

    
339
  }
340

    
341
  /**
342
   *
343
   */
344
  public function drupalFormValidate($op, $values) {
345
    $errors = [];
346

    
347
    if ($op == 'delete') {
348
      if (!$this->sid) {
349
        $errors['server_id_missing'] = 'Server id missing from delete form.';
350
      }
351
      $warnings = module_invoke_all('ldap_server_in_use', $this->sid, $this->name);
352
      if (count($warnings)) {
353
        $errors['status'] = join("<br/>", array_values($warnings));
354
      }
355

    
356
    }
357
    else {
358
      $this->populateFromDrupalForm($op, $values);
359
      $errors = $this->validate($op);
360
    }
361
    return $errors;
362
  }
363

    
364
  /**
365
   *
366
   */
367
  protected function validate($op) {
368
    $errors = [];
369
    if ($op == 'add') {
370
      $ldap_servers = $this->getLdapServerObjects(NULL, 'all');
371
      if (count($ldap_servers)) {
372
        foreach ($ldap_servers as $sid => $ldap_server) {
373
          if ($this->name == $ldap_server->name) {
374
            $errors['name'] = t('An LDAP server configuration with the  name %name already exists.', ['%name' => $this->name]);
375
          }
376
          elseif ($this->sid == $ldap_server->sid) {
377
            $errors['sid'] = t('An LDAP server configuration with the  id %sid  already exists.', ['%sid' => $this->sid]);
378
          }
379
        }
380
      }
381
    }
382

    
383
    // Check that no modules use this server.
384
    if ($this->status == 0) {
385
      $warnings = module_invoke_all('ldap_server_in_use', $this->sid, $this->name);
386
      if (count($warnings)) {
387
        $errors['status'] = join("<br/>", array_values($warnings));
388
      }
389
    }
390

    
391
    if (!is_numeric($this->port)) {
392
      $errors['port'] = t('The TCP/IP port must be an integer.');
393
    }
394

    
395
    if ($this->bind_method == LDAP_SERVERS_BIND_METHOD_USER && !$this->user_dn_expression) {
396
      $errors['user_dn_expression'] = t('When using "Bind with Users Credentials", Expression for user DN is required');
397
    }
398

    
399
    if ($this->mail_attr && $this->mail_template) {
400
      $errors['mail_attr'] = t('Mail attribute or Mail Template may be used.  Not both.');
401
    }
402

    
403
    if ($this->bind_method == LDAP_SERVERS_BIND_METHOD_SERVICE_ACCT && !$this->binddn) {
404
      $errors['binddn'] = t('When using "Bind with Service Account", Bind DN is required.');
405
    }
406
    if ($op == 'add') {
407
      if ($this->bind_method == LDAP_SERVERS_BIND_METHOD_SERVICE_ACCT &&
408
        (($op == 'add' && !$this->bindpw_new) || ($op != 'add' && !$this->bindpw))
409
      ) {
410
        $errors['bindpw'] = t('When using "Bind with Service Account", Bind password is required.');
411
      }
412
    }
413

    
414
    return $errors;
415
  }
416

    
417
  /**
418
   *
419
   */
420
  public function drupalFormWarnings($op, $values, $has_errors = NULL) {
421
    $errors = [];
422

    
423
    if ($op == 'delete') {
424
      if (!$this->sid) {
425
        $errors['server_id_missing'] = t('Server id missing from delete form.');
426
      }
427
    }
428
    else {
429
      $this->populateFromDrupalForm($op, $values);
430
      $warnings = $this->warnings($op, $has_errors);
431
    }
432
    return $warnings;
433
  }
434

    
435
  /**
436
   *
437
   */
438
  protected function warnings($op, $has_errors = NULL) {
439

    
440
    $warnings = [];
441
    if ($this->ldap_type) {
442
      $defaults = ldap_servers_ldaps_option_array();
443
      if (isset($defaults['user']['user_attr']) && ($this->user_attr != $defaults['user']['user_attr'])) {
444
        $tokens = ['%name' => $defaults['name'], '%default' => $defaults['user']['user_attr'], '%user_attr' => $this->user_attr];
445
        $warnings['user_attr'] = t('The standard UserName attribute in %name is %default.  You have %user_attr. This may be correct
446
          for your particular LDAP.', $tokens);
447
      }
448

    
449
      if (isset($defaults['user']['mail_attr']) && $this->mail_attr && ($this->mail_attr != $defaults['user']['mail_attr'])) {
450
        $tokens = ['%name' => $defaults['name'], '%default' => $defaults['user']['mail_attr'], '%mail_attr' => $this->mail_attr];
451
        $warnings['mail_attr'] = t('The standard mail attribute in %name is %default.  You have %mail_attr.  This may be correct
452
          for your particular LDAP.', $tokens);
453
      }
454
    }
455
    if (!$this->mail_attr && !$this->mail_template) {
456
      $warnings['mail_attr'] = t('Mail attribute or Mail Template should be used for most user account functionality.');
457
    }
458

    
459
    return $warnings;
460
  }
461

    
462
  /**
463
   *
464
   */
465
  public function drupalFormSubmit($op, $values) {
466

    
467
    $this->populateFromDrupalForm($op, $values);
468

    
469
    if ($values['clear_bindpw']) {
470
      $this->bindpw_clear = TRUE;
471
    }
472

    
473
    if ($op == 'delete') {
474
      $this->delete($this);
475
    }
476
    // Add or edit.
477
    else {
478
      try {
479
        $save_result = $this->save($op);
480
      }
481
      catch (Exception $e) {
482
        $this->setError('Save Error',
483
        t('Failed to save object.  Your form data was not saved.'));
484
      }
485
    }
486
  }
487

    
488
  /**
489
   *
490
   */
491
  protected function arrayToLines($array) {
492
    $lines = "";
493
    if (is_array($array)) {
494
      $lines = join("\n", $array);
495
    }
496
    elseif (is_array(@unserialize($array))) {
497
      $lines = join("\n", unserialize($array));
498
    }
499
    return $lines;
500
  }
501

    
502
  /**
503
   *
504
   */
505
  protected function linesToArray($lines) {
506
    $lines = trim($lines);
507

    
508
    if ($lines) {
509
      $array = preg_split('/[\n\r]+/', $lines);
510
      foreach ($array as $i => $value) {
511
        $array[$i] = trim($value);
512
      }
513
    }
514
    else {
515
      $array = [];
516
    }
517
    return $array;
518
  }
519

    
520
  /**
521
   *
522
   */
523
  public static function fields() {
524

    
525
    /**
526
     * consumer_type is tag (unique alphanumeric id) of consuming authorization such as
527
     *   drupal_roles, og_groups, civicrm_memberships
528
     */
529
    $fields = [
530

    
531
      'sid' => [
532
        'form' => [
533
          'fieldset' => 'server',
534
          '#type' => 'textfield',
535
          '#size' => 20,
536
          '#title' => t('Machine name for this server configuration.'),
537
          '#description' => t('May only contain alphanumeric characters (a-z, A-Z, 0-9, and _)'),
538
          '#required' => TRUE,
539
        ],
540
        'schema' => [
541
          'type' => 'varchar',
542
          'length' => 20,
543
          'not null' => TRUE,
544
        ],
545
      ],
546

    
547
      'numeric_sid' => [
548
        'schema' => [
549
          'type' => 'serial',
550
          'unsigned' => TRUE,
551
          'not null' => TRUE,
552
          'description' => 'Primary ID field for the table.  Only used internally.',
553
          'no export' => TRUE,
554
        ],
555
      ],
556

    
557
      'name' => [
558
        'form' => [
559
          'fieldset' => 'server',
560
          '#type' => 'textfield',
561
          '#size' => 50,
562
          '#title' => 'Name',
563
          '#description' => t('Choose a <em><strong>unique</strong></em> name for this server configuration.'),
564
        ],
565
        'schema' => [
566
          'type' => 'varchar',
567
          'length' => 255,
568
          'not null' => FALSE,
569
        ],
570
      ],
571

    
572
      'status' => [
573
        'form' => [
574
          'fieldset' => 'server',
575
          '#type' => 'checkbox',
576
          '#title' => t('Enabled'),
577
          '#description' => t('Disable in order to keep configuration without having it active.'),
578
          '#required' => FALSE,
579
        ],
580
        'schema' => [
581
          'type' => 'int',
582
          'size' => 'tiny',
583
          'not null' => FALSE,
584
          'default' => 0,
585
        ],
586
      ],
587

    
588
      'ldap_type' => [
589
        'form' => [
590
          'fieldset' => 'server',
591
          '#type' => 'select',
592
          '#options' => ldap_servers_ldaps_option_array(),
593
          '#title' => t('LDAP Server Type'),
594
          '#description' => t('This field is informative.  It\'s purpose is to assist with default values and give validation warnings.'),
595
        ],
596
        'schema' => [
597
          'type' => 'varchar',
598
          'length' => 20,
599
          'not null' => FALSE,
600
        ],
601
      ],
602

    
603
      'address' => [
604
        'form' => [
605
          'fieldset' => 'server',
606
          '#type' => 'textfield',
607
          '#title' => t('LDAP server'),
608
          '#description' => t('The domain name or IP address of your LDAP Server such as "ad.unm.edu". For SSL
609
        use the form ldaps://DOMAIN such as "ldaps://ad.unm.edu"'),
610
          '#size' => 50,
611
        ],
612
        'schema' => [
613
          'type' => 'varchar',
614
          'length' => 255,
615
          'not null' => FALSE,
616
        ],
617
      ],
618

    
619
      'port' => [
620
        'form' => [
621
          'fieldset' => 'server',
622
          '#type' => 'textfield',
623
          '#title' => t('LDAP port'),
624
          '#size' => 5,
625
          '#description' => t('The TCP/IP port on the above server which accepts LDAP connections. Must be an integer.'),
626
        ],
627
        'schema' => [
628
          'type' => 'int',
629
          'not null' => FALSE,
630
          'default' => 389,
631
        ],
632
      ],
633

    
634
      'tls' => [
635
        'form' => [
636
          'fieldset' => 'server',
637
          '#type' => 'checkbox',
638
          '#title' => t('Use Start-TLS'),
639
          '#description' => t('Secure the connection between the Drupal and the LDAP servers using TLS.<br /><em>Note: To use START-TLS, you must set the LDAP Port to 389.</em>'),
640
        ],
641
        'schema' => [
642
          'type' => 'int',
643
          'size' => 'tiny',
644
          'not null' => FALSE,
645
          'default' => 0,
646
        ],
647
      ],
648

    
649
      'followrefs' => [
650
        'form' => [
651
          'fieldset' => 'server',
652
          '#type' => 'checkbox',
653
          '#title' => t('Follow LDAP Referrals'),
654
          '#description' => t('Makes the LDAP client follow referrals (in the responses from the LDAP server) to other LDAP servers. This requires that the Bind Settings you give, is ALSO valid on these other servers.'),
655
        ],
656
        'schema' => [
657
          'type' => 'int',
658
          'size' => 'tiny',
659
          'not null' => FALSE,
660
          'default' => 0,
661
        ],
662
      ],
663

    
664
      'bind_method' => [
665
        'form' => [
666
          'fieldset' => 'bind_method',
667
          '#type' => 'radios',
668
          '#title' => t('Binding Method for Searches (such as finding user object or their group memberships)'),
669
          '#options' => [
670
            LDAP_SERVERS_BIND_METHOD_SERVICE_ACCT => t('Service Account Bind: Use credentials in the
671
            <strong>Service Account</strong> field to bind to LDAP.  <em>This option is usually a best practice.</em>'),
672

    
673
            LDAP_SERVERS_BIND_METHOD_USER => t('Bind with Users Credentials: Use user\'s entered credentials
674
            to bind to LDAP.<br/> This is only useful for modules that execute during user logon such
675
            as LDAP Authentication and LDAP Authorization.  <em>This option is not a best practice in most cases.</em>
676
            This option skips the initial anonymous bind and anonymous search to determine the LDAP user DN, but you
677
            can only use this option if your user DNs follow a consistent pattern, for example all of them being of
678
            the form "cn=[username],[base dn]", or all of them being of the form "uid=[username],ou=accounts,[base dn]".
679
            You specify the pattern under "Expression for user DN" in the next configuration block below.'),
680

    
681
            LDAP_SERVERS_BIND_METHOD_ANON_USER => t('Anonymous Bind for search, then Bind with Users Credentials:
682
            Searches for user dn then uses user\'s entered credentials to bind to LDAP.<br/> This is only useful for
683
            modules that work during user logon such as LDAP Authentication and LDAP Authorization.
684
            The user\'s dn must be discovered by an anonymous search for this option to work.'),
685

    
686
            LDAP_SERVERS_BIND_METHOD_ANON => t('Anonymous Bind: Use no credentials to bind to LDAP server.<br/>
687
            <em>This option will not work on most LDAPS connections.</em>'),
688
          ],
689
        ],
690
        'schema' => [
691
          'type' => 'int',
692
          'size' => 'small',
693
          'not null' => FALSE,
694
          'default' => 0,
695
          'boolean' => FALSE,
696
        ],
697
      ],
698

    
699
      'binding_service_acct' => [
700
        'form' => [
701
          'fieldset' => 'bind_method',
702
          '#type' => 'markup',
703
          '#markup' => t('<label>Service Account</label> Some LDAP configurations
704
          prohibit or restrict the results of anonymous searches. These LDAPs require a DN//password pair
705
          for binding. For security reasons, this pair should belong to an
706
          LDAP account with stripped down permissions.
707
          This is also required for provisioning LDAP accounts and groups!'),
708
        ],
709
      ],
710

    
711
      'binddn' => [
712
        'form' => [
713
          'fieldset' => 'bind_method',
714
          '#type' => 'textfield',
715
          '#title' => t('DN for non-anonymous search'),
716
          '#size' => 80,
717
          '#states' => [
718
            'enabled' => [
719
              ':input[name=bind_method]' => ['value' => (string) LDAP_SERVERS_BIND_METHOD_SERVICE_ACCT],
720
            ],
721
          ],
722
        ],
723
        'schema' => [
724
          'type' => 'varchar',
725
          'length' => 511,
726
        ],
727
      ],
728

    
729
      'bindpw' => [
730
        'form' => [
731
          'fieldset' => 'bind_method',
732
          '#type' => 'password',
733
          '#title' => t('Password for non-anonymous search'),
734
          '#size' => 20,
735
          '#states' => [
736
            'enabled' => [
737
              ':input[name=bind_method]' => ['value' => (string) LDAP_SERVERS_BIND_METHOD_SERVICE_ACCT],
738
            ],
739
          ],
740
        ],
741
        'schema' => [
742
          'type' => 'varchar',
743
          'length' => 255,
744
        ],
745
      ],
746

    
747
      'clear_bindpw' => [
748
        'form' => [
749
          'fieldset' => 'bind_method',
750
          '#type' => 'checkbox',
751
          '#title' => t('Clear existing password from database.  Check this when switching away from Service Account Binding.'),
752
          '#default_value' => 0,
753
        ],
754
      ],
755

    
756
      'basedn' => [
757
        'form' => [
758
          'fieldset' => 'users',
759
          '#type' => 'textarea',
760
          '#cols' => 50,
761
          '#rows' => 6,
762
          '#title' => t('Base DNs for LDAP users, groups, and other entries.'),
763
          '#description' => '<div>' . t('What DNs have entries relavant to this configuration?
764
            e.g. <code>ou=campus accounts,dc=ad,dc=uiuc,dc=edu</code>
765
            Keep in mind that every additional basedn likely doubles the number of queries.  Place the
766
            more heavily used one first and consider using one higher base DN rather than 2 or more lower base DNs.
767
            Enter one per line in case if you need more than one.') . '</div>',
768
        ],
769
        'schema' => [
770
          'type' => 'text',
771
          'serialize' => TRUE,
772
        ],
773
      ],
774

    
775
      'user_attr' => [
776
        'form' => [
777
          'fieldset' => 'users',
778
          '#type' => 'textfield',
779
          '#size' => 30,
780
          '#title' => t('AuthName attribute'),
781
          '#description' => t('The attribute that holds the users\' login name. (eg. <code>cn</code> for eDir or <code>sAMAccountName</code> for Active Directory).'),
782
        ],
783
        'schema' => [
784
          'type' => 'varchar',
785
          'length' => 255,
786
          'not null' => FALSE,
787
        ],
788
      ],
789

    
790
      'account_name_attr' => [
791
        'form' => [
792
          'fieldset' => 'users',
793
          '#type' => 'textfield',
794
          '#size' => 30,
795
          '#title' => t('AccountName attribute'),
796
          '#description' => t('The attribute that holds the unique account name. Defaults to the same as the AuthName attribute.'),
797
        ],
798
        'schema' => [
799
          'type' => 'varchar',
800
          'length' => 255,
801
          'not null' => FALSE,
802
          'default' => '',
803
        ],
804
      ],
805

    
806
      'mail_attr' => [
807
        'form' => [
808
          'fieldset' => 'users',
809
          '#type' => 'textfield',
810
          '#size' => 30,
811
          '#title' => t('Email attribute'),
812
          '#description' => t('The attribute that holds the users\' email address. (eg. <code>mail</code>). Leave empty if no such attribute exists'),
813
        ],
814
        'schema' => [
815
          'type' => 'varchar',
816
          'length' => 255,
817
          'not null' => FALSE,
818
        ],
819
      ],
820

    
821
      'mail_template' => [
822
        'form' => [
823
          'fieldset' => 'users',
824
          '#type' => 'textfield',
825
          '#size' => 30,
826
          '#title' => t('Email template'),
827
          '#description' => t('If no attribute contains the user\'s email address, but it can be derived from other attributes,
828
            enter an email "template" here.
829
            Templates should have the user\'s attribute name in form such as [cn], [uin], etc.
830
            such as <code>[cn]@mycompany.com</code>.
831
            See http://drupal.org/node/997082 for additional documentation on ldap tokens.
832
            '),
833
        ],
834
        'schema' => [
835
          'type' => 'varchar',
836
          'length' => 255,
837
          'not null' => FALSE,
838
        ],
839
      ],
840

    
841
      'picture_attr' => [
842
        'form' => [
843
          'fieldset' => 'users',
844
          '#type' => 'textfield',
845
          '#size' => 30,
846
          '#title' => t('Thumbnail attribute'),
847
          '#description' => t('The attribute that holds the users\' thumnail image. (eg. <code>thumbnailPhoto</code>). Leave empty if no such attribute exists'),
848
        ],
849
        'schema' => [
850
          'type' => 'varchar',
851
          'length' => 255,
852
          'not null' => FALSE,
853
        ],
854
      ],
855

    
856
      'unique_persistent_attr' => [
857
        'form' => [
858
          'fieldset' => 'users',
859
          '#type' => 'textfield',
860
          '#size' => 30,
861
          '#title' => t('Persistent and Unique User ID Attribute'),
862
          '#description' => t('In some LDAPs, a user\'s DN, CN, or mail value may
863
            change when a user\'s name changes or for other reasons.
864
            In order to avoid creation of multiple accounts for that user or other ambiguities,
865
            enter a unique and persistent ldap attribute for users.  In cases
866
            where DN does not change, enter "dn" here.
867
            If no such attribute exists, leave this blank.'
868
          ),
869
        ],
870
        'schema' => [
871
          'type' => 'varchar',
872
          'length' => 64,
873
          'not null' => FALSE,
874
        ],
875
      ],
876

    
877
      'unique_persistent_attr_binary' => [
878
        'form' => [
879
          'fieldset' => 'users',
880
          '#type' => 'checkbox',
881
          '#title' => t('Does the <em>Persistent and Unique User ID
882
            Attribute</em> hold a binary value?'),
883
          '#description' => t('You need to set this if you are using a binary
884
             attribute such as objectSid in ActiveDirectory for the PUID.<br>
885
             If you don\'t want this consider switching to another attribute,
886
             such as samaccountname.'),
887
        ],
888
        'schema' => [
889
          'type' => 'int',
890
          'size' => 'tiny',
891
          'not null' => FALSE,
892
          'default' => 0,
893
        ],
894
      ],
895

    
896
      'user_dn_expression' => [
897
        'form' => [
898
          'fieldset' => 'users',
899
          '#type' => 'textfield',
900
          '#size' => 80,
901
          '#title' => t('Expression for user DN. Required when "Bind with Users Credentials" method selected.'),
902
          '#description' => t('%username and %basedn are valid tokens in the expression.
903
            Typically it will be:<br/> <code>cn=%username,%basedn</code>
904
             which might evaluate to <code>cn=jdoe,ou=campus accounts,dc=ad,dc=mycampus,dc=edu</code>
905
             Base DNs are entered above.'),
906
        ],
907
        'schema' => [
908
          'type' => 'varchar',
909
          'length' => 255,
910
          'not null' => FALSE,
911
        ],
912
      ],
913

    
914
      'ldap_to_drupal_user' => [
915
        'form' => [
916
          'fieldset' => 'users',
917
          '#disabled' => (!module_exists('php')),
918
          '#type' => 'textarea',
919
          '#cols' => 25,
920
          '#rows' => 5,
921
          '#title' => t('PHP to transform Drupal login username to LDAP UserName attribute.'),
922
          '#description' => t('This will appear as disabled unless the "PHP filter" core module is enabled. Enter PHP to transform Drupal username to the value of the UserName attribute.
923
            The code should print the UserName attribute.
924
            PHP filter module must be enabled for this to work.
925
            The variable $name is available and is the user\'s login username.
926
            Careful, bad PHP code here will break your site. If left empty, no name transformation will be done.
927
            <br/>Example:<br/>Given the user will logon with jdoe@xyz.com and you want the ldap UserName attribute to be
928
            jdoe.<br/><code>$parts = explode(\'@\', $name); if (count($parts) == 2) {print $parts[0]};</code>'),
929
        ],
930
        'schema' => [
931
          'type' => 'varchar',
932
          'length' => 1024,
933
          'not null' => FALSE,
934
        ],
935
      ],
936

    
937
      'testing_drupal_username' => [
938
        'form' => [
939
          'fieldset' => 'users',
940
          '#type' => 'textfield',
941
          '#size' => 30,
942
          '#title' => t('Testing Drupal Username'),
943
          '#description' => t('This is optional and used for testing this server\'s configuration against an actual username.  The user need not exist in Drupal and testing will not affect the user\'s LDAP or Drupal Account.'),
944
        ],
945
        'schema' => [
946
          'type' => 'varchar',
947
          'length' => 255,
948
          'not null' => FALSE,
949
        ],
950
      ],
951

    
952
      'testing_drupal_user_dn' => [
953
        'form' => [
954
          'fieldset' => 'users',
955
          '#type' => 'textfield',
956
          '#size' => 120,
957
          '#title' => t('DN of testing username, e.g. cn=hpotter,ou=people,dc=hogwarts,dc=edu'),
958
          '#description' => t('This is optional and used for testing this server\'s configuration against an actual username.  The user need not exist in Drupal and testing will not affect the user\'s LDAP or Drupal Account.'),
959
        ],
960
        'schema' => [
961
          'type' => 'varchar',
962
          'length' => 255,
963
          'not null' => FALSE,
964
        ],
965
      ],
966

    
967
      'grp_unused' => [
968
        'form' => [
969
          'fieldset' => 'groups',
970
          '#type' => 'checkbox',
971
          '#title' => t('Groups are not relevant to this Drupal site.  This is generally true if LDAP Groups, LDAP Authorization, etc are not it use.'),
972
          '#disabled' => FALSE,
973
        ],
974
        'schema' => [
975
          'type' => 'int',
976
          'size' => 'tiny',
977
          'not null' => FALSE,
978
          'default' => 0,
979
        ],
980
      ],
981

    
982
      'grp_object_cat' => [
983
        'form' => [
984
          'fieldset' => 'groups',
985
          '#type' => 'textfield',
986
          '#size' => 30,
987
          '#title' => t('Name of Group Object Class'),
988
          '#description' => t('e.g. groupOfNames, groupOfUniqueNames, group.'),
989
          '#states' => [
990
            'visible' => [
991
              ':input[name=grp_unused]' => ['checked' => FALSE],
992
            ],
993
          ],
994
        ],
995
        'schema' => [
996
          'type' => 'varchar',
997
          'length' => 64,
998
          'not null' => FALSE,
999
        ],
1000
      ],
1001

    
1002
      'grp_nested' => [
1003
        'form' => [
1004
          'fieldset' => 'groups',
1005
          '#type' => 'checkbox',
1006
          '#title' => t('Nested groups are used in my LDAP'),
1007
          '#disabled' => FALSE,
1008
          '#description' => t('If a user is a member of group A and group A is a member of group B,
1009
             user should be considered to be in group A and B.  If your LDAP has nested groups, but you
1010
             want to ignore nesting, leave this unchecked.'),
1011
          '#states' => [
1012
            'visible' => [
1013
              ':input[name=grp_unused]' => ['checked' => FALSE],
1014
            ],
1015
          ],
1016
        ],
1017
        'schema' => [
1018
          'type' => 'int',
1019
          'size' => 'tiny',
1020
          'not null' => FALSE,
1021
          'default' => 0,
1022
        ],
1023
      ],
1024

    
1025
      'grp_user_memb_attr_exists' => [
1026
        'form' => [
1027
          'fieldset' => 'groups',
1028
          '#type' => 'checkbox',
1029
          '#title' => t('A user LDAP attribute such as <code>memberOf</code> exists that contains a list of their groups.
1030
            Active Directory and openLdap with memberOf overlay fit this model.'),
1031
          '#disabled' => FALSE,
1032
          '#states' => [
1033
            'visible' => [
1034
              ':input[name=grp_unused]' => ['checked' => FALSE],
1035
            ],
1036
          ],
1037
        ],
1038
        'schema' => [
1039
          'type' => 'int',
1040
          'size' => 'tiny',
1041
          'not null' => FALSE,
1042
          'default' => 0,
1043
        ],
1044
      ],
1045

    
1046
      'grp_user_memb_attr' => [
1047
        'form' => [
1048
          'fieldset' => 'groups',
1049
          '#type' => 'textfield',
1050
          '#size' => 30,
1051
          '#title' => t('Attribute in User Entry Containing Groups'),
1052
          '#description' => t('e.g. memberOf'),
1053
          '#states' => [
1054
            'enabled' => [
1055
              ':input[name=grp_user_memb_attr_exists]' => ['checked' => TRUE],
1056
            ],
1057
            'visible' => [
1058
              ':input[name=grp_unused]' => ['checked' => FALSE],
1059
            ],
1060
          ],
1061
        ],
1062
        'schema' => [
1063
          'type' => 'varchar',
1064
          'length' => 255,
1065
          'not null' => FALSE,
1066
        ],
1067
      ],
1068

    
1069
      'grp_memb_attr' => [
1070
        'form' => [
1071
          'fieldset' => 'groups',
1072
          '#type' => 'textfield',
1073
          '#size' => 30,
1074
          '#title' => t('LDAP Group Entry Attribute Holding User\'s DN, CN, etc.'),
1075
          '#description' => t('e.g uniquemember, memberUid'),
1076
          '#states' => [
1077
            'visible' => [
1078
              ':input[name=grp_unused]' => ['checked' => FALSE],
1079
            ],
1080
          ],
1081
        ],
1082
        'schema' => [
1083
          'type' => 'varchar',
1084
          'length' => 255,
1085
          'not null' => FALSE,
1086
        ],
1087
      ],
1088

    
1089
      'grp_memb_attr_match_user_attr' => [
1090
        'form' => [
1091
          'fieldset' => 'groups',
1092
          '#type' => 'textfield',
1093
          '#size' => 30,
1094
          '#title' => t('User attribute held in "LDAP Group Entry Attribute Holding..."'),
1095
          '#description' => t('This is almost always "dn" (which technically isn\'t an attribute).  Sometimes its "cn".'),
1096
          '#states' => [
1097
            'visible' => [
1098
              ':input[name=grp_unused]' => ['checked' => FALSE],
1099
            ],
1100
          ],
1101
        ],
1102
        'schema' => [
1103
          'type' => 'varchar',
1104
          'length' => 255,
1105
          'not null' => FALSE,
1106
        ],
1107
      ],
1108

    
1109
      'grp_derive_from_dn' => [
1110
        'form' => [
1111
          'fieldset' => 'groups',
1112
          '#type' => 'checkbox',
1113
          '#title' => t('Groups are derived from user\'s LDAP entry DN.') . '<em>' .
1114
          t('This
1115
            group definition has very limited functionality and most modules will
1116
            not take this into account.  LDAP Authorization will.') . '</em>',
1117
          '#disabled' => FALSE,
1118
          '#states' => [
1119
            'visible' => [
1120
              ':input[name=grp_unused]' => ['checked' => FALSE],
1121
            ],
1122
          ],
1123
        ],
1124
        'schema' => [
1125
          'type' => 'int',
1126
          'size' => 'tiny',
1127
          'not null' => FALSE,
1128
          'default' => 0,
1129
        ],
1130
      ],
1131

    
1132
      'grp_derive_from_dn_attr' => [
1133
        'form' => [
1134
          'fieldset' => 'groups',
1135
          '#type' => 'textfield',
1136
          '#size' => 30,
1137
          '#title' => t('Attribute of the User\'s LDAP Entry DN which contains the group'),
1138
          '#description' => t('e.g. ou'),
1139
          '#states' => [
1140
            'enabled' => [
1141
              ':input[name=grp_derive_from_dn]' => ['checked' => TRUE],
1142
            ],
1143
            'visible' => [
1144
              ':input[name=grp_unused]' => ['checked' => FALSE],
1145
            ],
1146
          ],
1147
        ],
1148
        'schema' => [
1149
          'type' => 'varchar',
1150
          'length' => 255,
1151
          'not null' => FALSE,
1152
        ],
1153
      ],
1154

    
1155
      'grp_test_grp_dn' => [
1156
        'form' => [
1157
          'fieldset' => 'groups',
1158
          '#type' => 'textfield',
1159
          '#size' => 120,
1160
          '#title' => t('Testing LDAP Group DN'),
1161
          '#description' => t('This is optional and can be useful for debugging and validating forms.'),
1162
          '#states' => [
1163
            'visible' => [
1164
              ':input[name=grp_unused]' => ['checked' => FALSE],
1165
            ],
1166
          ],
1167
        ],
1168
        'schema' => [
1169
          'type' => 'varchar',
1170
          'length' => 255,
1171
          'not null' => FALSE,
1172
        ],
1173
      ],
1174

    
1175
      'grp_test_grp_dn_writeable' => [
1176
        'form' => [
1177
          'fieldset' => 'groups',
1178
          '#type' => 'textfield',
1179
          '#size' => 120,
1180
          '#title' => t('Testing LDAP Group DN that is writable.  WARNING the test script for the server will create, delete, and add members to this group!'),
1181
          '#description' => t('This is optional and can be useful for debugging and validating forms.'),
1182
          '#states' => [
1183
            'visible' => [
1184
              ':input[name=grp_unused]' => ['checked' => FALSE],
1185
            ],
1186
          ],
1187
        ],
1188
        'schema' => [
1189
          'type' => 'varchar',
1190
          'length' => 255,
1191
          'not null' => FALSE,
1192
        ],
1193
      ],
1194

    
1195
      'search_pagination' => [
1196
        'form' => [
1197
          'fieldset' => 'pagination',
1198
          '#type' => 'checkbox',
1199
          '#title' => t('Use LDAP Pagination.'),
1200
          '#disabled' => !ldap_servers_php_supports_pagination(),
1201
        ],
1202
        'schema' => [
1203
          'type' => 'int',
1204
          'size' => 'tiny',
1205
          'not null' => FALSE,
1206
          'default' => 0,
1207
        ],
1208
      ],
1209

    
1210
      'search_page_size' => [
1211
        'form' => [
1212
          'fieldset' => 'pagination',
1213
          '#type' => 'textfield',
1214
          '#size' => 10,
1215
          '#disabled' => !ldap_servers_php_supports_pagination(),
1216
          '#title' => t('Pagination size limit.'),
1217
          '#description' => t('This should be equal to or smaller than the max
1218
            number of entries returned at a time by your ldap server.
1219
            1000 is a good guess when unsure. Other modules such as LDAP Query
1220
            or LDAP Feeds will be allowed to set a smaller page size, but not
1221
            a larger one.'),
1222
          '#states' => [
1223
            'visible' => [
1224
              ':input[name="search_pagination"]' => ['checked' => TRUE],
1225
            ],
1226
          ],
1227
        ],
1228
        'schema' => [
1229
          'type' => 'int',
1230
          'size' => 'medium',
1231
          'not null' => FALSE,
1232
          'default' => 1000,
1233
        ],
1234
      ],
1235

    
1236
      'weight' => [
1237
        'schema' => [
1238
          'type' => 'int',
1239
          'not null' => FALSE,
1240
          'default' => 0,
1241
        ],
1242
      ],
1243

    
1244
    ];
1245

    
1246
    return $fields;
1247

    
1248
  }
1249

    
1250
}