Projet

Général

Profil

Paste
Télécharger (26,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_authentication / LdapAuthenticationConfAdmin.class.php @ 91af538d

1
<?php
2

    
3
/**
4
 * @file
5
 * This classextends by LdapAuthenticationConf for configuration and other admin functions.
6
 */
7

    
8
ldap_servers_module_load_include('php', 'ldap_authentication', 'LdapAuthenticationConf.class');
9
/**
10
 *
11
 */
12
class LdapAuthenticationConfAdmin extends LdapAuthenticationConf {
13

    
14
  /**
15
   *
16
   */
17
  protected function setTranslatableProperties() {
18

    
19
    /**
20
     * 0.  Logon Options
21
     */
22

    
23
    $values['authenticationModeOptions'] = [
24
      LDAP_AUTHENTICATION_MIXED => t('Mixed mode. Drupal authentication is tried first.  On failure, LDAP authentication is performed.'),
25
      LDAP_AUTHENTICATION_EXCLUSIVE => t('Only LDAP Authentication is allowed except for user 1.
26
        If selected, (1) reset password links will be replaced with links to ldap end user documentation below.
27
        (2) The reset password form will be left available at user/password for user 1; but no links to it
28
        will be provided to anonymous users.
29
        (3) Password fields in user profile form will be removed except for user 1.'),
30
    ];
31

    
32
    $values['authenticationServersDescription'] = t('Check all LDAP server configurations to use in authentication.
33
     Each will be tested for authentication until successful or
34
     until each is exhausted.  In most cases only one server configuration is selected.');
35

    
36
    /**
37
     * User Login Interface
38
     */
39
    $values['loginUIUsernameTxtDescription'] = t('Text to be displayed to user below the username field of
40
     the user login screen.');
41

    
42
    $values['loginUIPasswordTxtDescription'] = t('Text to be displayed to user below the password field of
43
     the user login screen.');
44

    
45
    $values['ldapUserHelpLinkUrlDescription'] = t('URL to LDAP user help/documentation for users resetting
46
     passwords etc. Should be of form http://domain.com/. Could be the institutions ldap password support page
47
     or a page within this drupal site that is available to anonymous users.');
48

    
49
    $values['ldapUserHelpLinkTextDescription'] = t('Text for above link e.g. Account Help or Campus Password Help Page');
50

    
51
    /**
52
     * LDAP User Restrictions
53
     */
54

    
55
    $values['allowOnlyIfTextInDnDescription'] = t('A list of text such as ou=education
56
      or cn=barclay that at least one of be found in user\'s dn string.  Enter one per line
57
      such as <pre>ou=education') . "\n" . t('ou=engineering</pre>   This test will be case insensitive.');
58

    
59
    $values['excludeIfTextInDnDescription'] = t('A list of text such as ou=evil
60
      or cn=bad that if found in a user\'s dn, exclude them from ldap authentication.
61
      Enter one per line such as <pre>ou=evil') . "\n" . t('cn=bad</pre> This test will be case insensitive.');
62

    
63
    $values['allowTestPhpDescription'] = t('PHP code which should print 1
64
        for allowing ldap authentication or 0 for not allowed.  Available variables are:
65
        $_name and $_ldap_user_entry  See readme.txt for more info.');
66

    
67
    $values['excludeIfNoAuthorizationsDescription'] = t('If the user is not granted any drupal roles,
68
      organic groups, etc. by LDAP Authorization, login will be denied.  LDAP Authorization must be
69
      enabled for this to work.');
70

    
71
    /**
72
    * Email
73
    */
74

    
75
    $values['emailOptionOptions'] = [
76
      LDAP_AUTHENTICATION_EMAIL_FIELD_REMOVE => t('Don\'t show an email field on user forms. LDAP derived email will be used for user and cannot be changed by user.'),
77
      LDAP_AUTHENTICATION_EMAIL_FIELD_DISABLE => t('Show disabled email field on user forms with LDAP derived email. LDAP derived email will be used for user and cannot be changed by user.'),
78
      LDAP_AUTHENTICATION_EMAIL_FIELD_ALLOW => t('Leave email field on user forms enabled. Generally used when provisioning to LDAP or not using email derived from LDAP.'),
79
    ];
80

    
81
    $values['emailUpdateOptions'] = [
82
      LDAP_AUTHENTICATION_EMAIL_UPDATE_ON_LDAP_CHANGE_ENABLE_NOTIFY => t('Update stored email if LDAP email differs at login and notify user.'),
83
      LDAP_AUTHENTICATION_EMAIL_UPDATE_ON_LDAP_CHANGE_ENABLE => t('Update stored email if LDAP email differs at login but don\'t notify user.'),
84
      LDAP_AUTHENTICATION_EMAIL_UPDATE_ON_LDAP_CHANGE_DISABLE => t('Don\'t update stored email if LDAP email differs at login.'),
85
    ];
86
    $values['emailTemplateHandlingOptions'] = [
87
      LDAP_AUTHENTICATION_EMAIL_TEMPLATE_NONE => t('Never use the template.'),
88
      LDAP_AUTHENTICATION_EMAIL_TEMPLATE_IF_EMPTY => t('Use the template if no email address was provided by the LDAP server.'),
89
      LDAP_AUTHENTICATION_EMAIL_TEMPLATE_ALWAYS => t('Always use the template.'),
90
    ];
91

    
92
    /**
93
    * Password
94
    */
95

    
96
    $values['passwordUpdateOptions'] = [
97
      LDAP_AUTHENTICATION_PASSWORD_FIELD_SHOW => t('Display password field disabled (Prevents password updates).'),
98
      LDAP_AUTHENTICATION_PASSWORD_FIELD_HIDE => t('Don\'t show password field on user forms except login form.'),
99
      LDAP_AUTHENTICATION_PASSWORD_FIELD_ALLOW => t('Display password field and allow updating it. In order to change password in LDAP, LDAP provisioning for this field must be enabled.'),
100
    ];
101

    
102
    /**
103
     *  Single Sign-On / Seamless Sign-On
104
     */
105

    
106
    $values['ldapImplementationOptions'] = [
107
      'mod_auth_sspi' => t('mod_auth_sspi'),
108
      'mod_auth_kerb' => t('mod_auth_kerb'),
109
    ];
110

    
111
    $values['cookieExpirePeriod'] = [-1 => t('Session'), 0 => t('Immediately')] +
112
        drupal_map_assoc([3600, 86400, 604800, 2592000, 31536000, 315360000, 630720000], 'format_interval');
113

    
114
    $values['ssoEnabledDescription'] = '<strong>' . t('Single Sign on is enabled.') .
115
        '</strong> ' . t('To disable it, disable the LDAP SSO Module on the') . ' ' . l(t('Modules Form'), 'admin/modules') . '.<p>' .
116
        t('Single Sign-On enables ' .
117
        'users of this site to be authenticated by visiting the URL ' .
118
        '"user/login/sso, or automatically if selecting "automated ' .
119
        'single sign-on" below. Set up of LDAP authentication must be ' .
120
        'performed on the web server. Please review the readme file of the ' .
121
        'ldap_sso module for more information.')
122
        . '</p>';
123

    
124
    $values['ssoExcludedPathsDescription'] = '<p>' .
125
        t("Which paths will not check for SSO? cron.php is common example.  Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard.
126
          Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.",
127
          ['%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>']);
128
    '</p>';
129

    
130
    $values['ssoExcludedHostsDescription'] = '<p>' .
131
        t('If your site is accessible via multiple hostnames, you may only want
132
          the LDAP SSO module to authenticate against some of them. To exclude
133
          any hostnames from SSO, enter them here. Enter one host per line.');
134
    '</p>';
135

    
136
    $values['ssoRemoteUserStripDomainNameDescription'] = t('Useful when the ' .
137
        'WWW server provides authentication in the form of user@realm and you ' .
138
        'want to have both SSO and regular forms based authentication ' .
139
        'available. Otherwise duplicate accounts with conflicting e-mail ' .
140
        'addresses may be created.');
141
    $values['ssoNotifyAuthenticationDescription'] = t('This displays a message to the ' .
142
        'user after they have succesfully authenticated using single sign on');
143
    $values['seamlessLogInDescription'] = t('This requires that you ' .
144
        'have operational NTLM or Kerberos authentication turned on for at least ' .
145
        'the path user/login/sso, or for the whole domain.');
146
    $values['cookieExpireDescription'] = t('If using the automated/seamless login, a ' .
147
        'cookie is necessary to prevent automatic login after a user ' .
148
        'manually logs out. Select the lifetime of the cookie.');
149
    $values['ldapImplementationDescription'] = t('Select the type of ' .
150
        'authentication mechanism you are using.');
151

    
152
    foreach ($values as $property => $default_value) {
153
      $this->$property = $default_value;
154
    }
155
  }
156

    
157
  /**
158
   * 0.  Logon Options.
159
   */
160
  public $authenticationModeDefault = LDAP_AUTHENTICATION_MIXED;
161
  public $authenticationModeOptions;
162

    
163
  protected $authenticationServersDescription;
164
  protected $authenticationServersOptions = [];
165

    
166
  /**
167
   * 1.  User Login Interface.
168
   */
169
  protected $loginUIUsernameTxtDescription;
170
  protected $loginUIPasswordTxtDescription;
171
  protected $ldapUserHelpLinkUrlDescription;
172
  protected $ldapUserHelpLinkTextDescription;
173

    
174

    
175
  /**
176
   * 2.  LDAP User Restrictions.
177
   */
178

    
179
  protected $allowOnlyIfTextInDnDescription;
180
  protected $excludeIfTextInDnDescription;
181
  protected $allowTestPhpDescription;
182

    
183
  /**
184
   * 4. Email.
185
   */
186

    
187
  public $emailOptionDefault = LDAP_AUTHENTICATION_EMAIL_FIELD_REMOVE;
188
  public $emailOptionOptions;
189

    
190
  public $emailUpdateDefault = LDAP_AUTHENTICATION_EMAIL_UPDATE_ON_LDAP_CHANGE_ENABLE_NOTIFY;
191
  public $emailUpdateOptions;
192

    
193
  public $emailTemplateHandlingDefault = LDAP_AUTHENTICATION_EMAIL_TEMPLATE_DEFAULT;
194
  public $emailTemplateHandlingOptions;
195

    
196
  public $emailTemplateDefault = LDAP_AUTHENTICATION_DEFAULT_TEMPLATE;
197

    
198
  public $templateUsagePromptUserDefault = LDAP_AUTHENTICATION_TEMPLATE_USAGE_PROMPT_USER_DEFAULT;
199

    
200
  public $templateUsagePromptRegexDefault = LDAP_AUTHENTICATION_DEFAULT_TEMPLATE_REGEX;
201

    
202
  public $templateUsageNeverUpdateDefault = LDAP_AUTHENTICATION_TEMPLATE_USAGE_NEVER_UPDATE_DEFAULT;
203

    
204
  /**
205
   * 5. Single Sign-On / Seamless Sign-On.
206
   */
207

    
208
  public $ssoEnabledDescription;
209
  public $ssoRemoteUserStripDomainNameDescription;
210
  public $ldapImplementationOptions;
211
  public $cookieExpirePeriod;
212
  public $seamlessLogInDescription;
213
  public $cookieExpireDescription;
214
  public $ldapImplementationDescription;
215

    
216

    
217
  public $errorMsg = NULL;
218
  public $hasError = FALSE;
219
  public $errorName = NULL;
220

    
221
  /**
222
   *
223
   */
224
  public function clearError() {
225
    $this->hasError = FALSE;
226
    $this->errorMsg = NULL;
227
    $this->errorName = NULL;
228
  }
229

    
230
  /**
231
   *
232
   */
233
  public function save() {
234
    foreach ($this->saveable as $property) {
235
      $save[$property] = $this->{$property};
236
    }
237
    variable_set('ldap_authentication_conf', $save);
238
    $this->load();
239
  }
240

    
241
  /**
242
   *
243
   */
244
  public static function getSaveableProperty($property) {
245
    $ldap_authentication_conf = variable_get('ldap_authentication_conf', []);
246
    return isset($ldap_authentication_conf[$property]) ? $ldap_authentication_conf[$property] : FALSE;
247

    
248
  }
249

    
250
  /**
251
   *
252
   */
253
  public static function uninstall() {
254
    variable_del('ldap_authentication_conf');
255
  }
256

    
257
  /**
258
   *
259
   */
260
  public function __construct() {
261
    parent::__construct();
262
    $this->setTranslatableProperties();
263
    if ($servers = ldap_servers_get_servers(NULL, 'enabled')) {
264
      foreach ($servers as $sid => $ldap_server) {
265
        $enabled = ($ldap_server->status) ? 'Enabled' : 'Disabled';
266
        $this->authenticationServersOptions[$sid] = $ldap_server->name . ' (' . $ldap_server->address . ') Status: ' . $enabled;
267
      }
268
    }
269
  }
270

    
271
  /**
272
   *
273
   */
274
  public function drupalForm() {
275

    
276
    if (count($this->authenticationServersOptions) == 0) {
277
      $message = ldap_servers_no_enabled_servers_msg('configure LDAP Authentication');
278
      $form['intro'] = [
279
        '#type' => 'item',
280
        '#markup' => t('<h1>LDAP Authentication Settings</h1>') . $message,
281
      ];
282
      return $form;
283
    }
284

    
285
    // Not sure what the tokens would be for this form?
286
    $tokens = [];
287

    
288
    $form['intro'] = [
289
      '#type' => 'item',
290
      '#markup' => t('<h1>LDAP Authentication Settings</h1>'),
291
    ];
292

    
293
    $form['logon'] = [
294
      '#type' => 'fieldset',
295
      '#title' => t('Logon Options'),
296
      '#collapsible' => TRUE,
297
      '#collapsed' => FALSE,
298
    ];
299

    
300
    $form['logon']['authenticationMode'] = [
301
      '#type' => 'radios',
302
      '#title' => t('Allowable Authentications'),
303
      '#required' => 1,
304
      '#default_value' => $this->authenticationMode,
305
      '#options' => $this->authenticationModeOptions,
306
    ];
307

    
308
    $form['logon']['authenticationServers'] = [
309
      '#type' => 'checkboxes',
310
      '#title' => t('Authentication LDAP Server Configurations'),
311
      '#required' => FALSE,
312
      '#default_value' => $this->sids,
313
      '#options' => $this->authenticationServersOptions,
314
      '#description' => $this->authenticationServersDescription,
315
    ];
316

    
317
    $form['login_UI'] = [
318
      '#type' => 'fieldset',
319
      '#title' => t('User Login Interface'),
320
      '#collapsible' => TRUE,
321
      '#collapsed' => FALSE,
322
    ];
323

    
324
    $form['login_UI']['loginUIUsernameTxt'] = [
325
      '#type' => 'textfield',
326
      '#title' => t('Username Description Text'),
327
      '#required' => 0,
328
      '#default_value' => $this->loginUIUsernameTxt,
329
      '#description' => $this->loginUIUsernameTxtDescription,
330
    ];
331

    
332
    $form['login_UI']['loginUIPasswordTxt'] = [
333
      '#type' => 'textfield',
334
      '#title' => t('Password Description Text'),
335
      '#required' => 0,
336
      '#default_value' => $this->loginUIPasswordTxt,
337
      '#description' => $this->loginUIPasswordTxtDescription,
338
    ];
339

    
340
    $form['login_UI']['ldapUserHelpLinkUrl'] = [
341
      '#type' => 'textfield',
342
      '#title' => t('LDAP Account User Help URL'),
343
      '#required' => 0,
344
      '#default_value' => $this->ldapUserHelpLinkUrl,
345
      '#description' => $this->ldapUserHelpLinkUrlDescription,
346
    ];
347

    
348
    $form['login_UI']['ldapUserHelpLinkText'] = [
349
      '#type' => 'textfield',
350
      '#title' => t('LDAP Account User Help Link Text'),
351
      '#required' => 0,
352
      '#default_value' => $this->ldapUserHelpLinkText,
353
      '#description' => $this->ldapUserHelpLinkTextDescription,
354
    ];
355

    
356
    $form['restrictions'] = [
357
      '#type' => 'fieldset',
358
      '#title' => t('LDAP User "Whitelists" and Restrictions'),
359
      '#collapsible' => TRUE,
360
      '#collapsed' => FALSE,
361
    ];
362

    
363
    $form['restrictions']['allowOnlyIfTextInDn'] = [
364
      '#type' => 'textarea',
365
      '#title' => t('Allow Only Text Test'),
366
      '#default_value' => $this->arrayToLines($this->allowOnlyIfTextInDn),
367
      '#cols' => 50,
368
      '#rows' => 3,
369
      '#description' => t($this->allowOnlyIfTextInDnDescription, $tokens),
370
    ];
371

    
372
    $form['restrictions']['excludeIfTextInDn'] = [
373
      '#type' => 'textarea',
374
      '#title' => t('Excluded Text Test'),
375
      '#default_value' => $this->arrayToLines($this->excludeIfTextInDn),
376
      '#cols' => 50,
377
      '#rows' => 3,
378
      '#description' => t($this->excludeIfTextInDnDescription, $tokens),
379
    ];
380

    
381
    $form['restrictions']['allowTestPhp'] = [
382
      '#type' => 'textarea',
383
      '#title' => t('PHP to Test for Allowed LDAP Users'),
384
      '#default_value' => $this->allowTestPhp,
385
      '#cols' => 50,
386
      '#rows' => 3,
387
      '#description' => t($this->allowTestPhpDescription, $tokens),
388
      '#disabled' => (boolean) (!module_exists('php')),
389
    ];
390

    
391
    if (!module_exists('php')) {
392
      $form['restrictions']['allowTestPhp']['#title'] .= ' <em>' . t('php module currently disabled') . '</em>';
393
    }
394

    
395
    $form['restrictions']['excludeIfNoAuthorizations'] = [
396
      '#type' => 'checkbox',
397
      '#title' => t('Deny access to users without Ldap Authorization Module
398
        authorization mappings such as Drupal roles.
399
        Requires LDAP Authorization to be enabled and configured!'),
400
      '#default_value' => $this->excludeIfNoAuthorizations,
401
      '#description' => t($this->excludeIfNoAuthorizationsDescription, $tokens),
402
      '#disabled' => (boolean) (!module_exists('ldap_authorization')),
403
    ];
404

    
405
    $form['email'] = [
406
      '#type' => 'fieldset',
407
      '#title' => t('Email'),
408
      '#collapsible' => TRUE,
409
      '#collapsed' => FALSE,
410
    ];
411

    
412
    $form['email']['emailOption'] = [
413
      '#type' => 'radios',
414
      '#title' => t('Email Behavior'),
415
      '#required' => 1,
416
      '#default_value' => $this->emailOption,
417
      '#options' => $this->emailOptionOptions,
418
    ];
419

    
420
    $form['email']['emailUpdate'] = [
421
      '#type' => 'radios',
422
      '#title' => t('Email Update'),
423
      '#required' => 1,
424
      '#default_value' => $this->emailUpdate,
425
      '#options' => $this->emailUpdateOptions,
426
    ];
427

    
428
    $form['email']['template'] = [
429
      '#type' => 'fieldset',
430
      '#collapsible' => TRUE,
431
      '#title' => t('Email Templates'),
432
    ];
433

    
434
    $form['email']['template']['emailTemplateHandling'] = [
435
      '#type' => 'radios',
436
      '#title' => t('Email Template Handling'),
437
      '#required' => 1,
438
      '#default_value' => $this->emailTemplateHandling,
439
      '#options' => $this->emailTemplateHandlingOptions,
440
    ];
441

    
442
    $form['email']['template']['emailTemplate'] = [
443
      '#type' => 'textfield',
444
      '#title' => t('Email Template'),
445
      '#required' => 0,
446
      '#default_value' => $this->emailTemplate,
447
    ];
448

    
449
    $form['email']['template']['templateUsageResolveConflict'] = [
450
      '#type' => 'checkbox',
451
      '#title' => t('If a Drupal account already exists with the same email, but different account name, use the email template instead of the LDAP email.'),
452
      '#default_value' => $this->templateUsageResolveConflict,
453
    ];
454

    
455
    $form['email']['template']['templateUsageNeverUpdate'] = [
456
      '#type' => 'checkbox',
457
      '#title' => t('Ignore the Email Update settings and never update the stored email if the template is used.'),
458
      '#default_value' => $this->templateUsageNeverUpdate,
459
    ];
460

    
461
    $form['email']['prompts'] = [
462
      '#type' => 'fieldset',
463
      '#collapsible' => TRUE,
464
      '#title' => t('User Email Prompt'),
465
      '#description' => t('These settings allow the user to fill in their email address after logging in if the template was used to generate their email address.'),
466
    ];
467

    
468
    $form['email']['prompts']['templateUsagePromptUser'] = [
469
      '#type' => 'checkbox',
470
      '#title' => t('Prompt user for email on every page load.'),
471
      '#default_value' => $this->templateUsagePromptUser,
472
    ];
473

    
474
    $form['email']['prompts']['templateUsageRedirectOnLogin'] = [
475
      '#type' => 'checkbox',
476
      '#title' => t('Redirect the user to the form after logging in.'),
477
      '#default_value' => $this->templateUsageRedirectOnLogin,
478
    ];
479

    
480
    $form['email']['prompts']['templateUsagePromptRegex'] = [
481
      '#type' => 'textfield',
482
      '#default_value' => $this->templateUsagePromptRegex,
483
      '#title' => t('Template Regex'),
484
      '#description' => t('This regex will be used to determine if the template was used to create an account.'),
485
    ];
486

    
487
    $form['password'] = [
488
      '#type' => 'fieldset',
489
      '#title' => t('Password'),
490
      '#collapsible' => TRUE,
491
      '#collapsed' => FALSE,
492
    ];
493
    $form['password']['passwordOption'] = [
494
      '#type' => 'radios',
495
      '#title' => t('Password Behavior'),
496
      '#required' => 1,
497
      '#default_value' => $this->passwordOption,
498
      '#options' => $this->passwordUpdateOptions,
499
    ];
500

    
501
    /**
502
     * Begin single sign-on settings
503
     */
504
    $form['sso'] = [
505
      '#type' => 'fieldset',
506
      '#title' => t('Single Sign-On'),
507
      '#collapsible' => TRUE,
508
      '#collapsed' => (boolean) (!$this->ssoEnabled),
509
    ];
510

    
511
    if ($this->ssoEnabled) {
512
      $form['sso']['enabled'] = [
513
        '#type' => 'markup',
514
        '#markup' => $this->ssoEnabledDescription,
515
      ];
516
    }
517
    else {
518
      $form['sso']['disabled'] = [
519
        '#type' => 'markup',
520
        '#markup' => '<p><em>' . t('LDAP Single Sign-On module must be enabled for options below to work.')
521
        . ' ' . t('It is currently disabled.')
522
        . ' ' . l(t('See modules form'), 'admin/modules') . '</p></em>',
523
      ];
524
    }
525

    
526
    $form['sso']['ssoRemoteUserStripDomainName'] = [
527
      '#type' => 'checkbox',
528
      '#title' => t('Strip REMOTE_USER domain name'),
529
      '#description' => t($this->ssoRemoteUserStripDomainNameDescription),
530
      '#default_value' => $this->ssoRemoteUserStripDomainName,
531
      '#disabled' => (boolean) (!$this->ssoEnabled),
532
    ];
533

    
534
    $form['sso']['seamlessLogin'] = [
535
      '#type' => 'checkbox',
536
      '#title' => t('Turn on automated/seamless single sign-on'),
537
      '#description' => t($this->seamlessLogInDescription),
538
      '#default_value' => $this->seamlessLogin,
539
      '#disabled' => (boolean) (!$this->ssoEnabled),
540
    ];
541

    
542
    $form['sso']['ssoNotifyAuthentication'] = [
543
      '#type' => 'checkbox',
544
      '#title' => t('Notify user of successful authentication'),
545
      '#description' => t($this->ssoNotifyAuthenticationDescription),
546
      '#default_value' => $this->ssoNotifyAuthentication,
547
      '#disabled' => (boolean) (!$this->ssoEnabled),
548
    ];
549

    
550
    $form['sso']['cookieExpire'] = [
551
      '#type' => 'select',
552
      '#title' => t('Cookie Lifetime'),
553
      '#description' => t($this->cookieExpireDescription),
554
      '#default_value' => $this->cookieExpire,
555
      '#options' => $this->cookieExpirePeriod,
556
      '#disabled' => (boolean) (!$this->ssoEnabled),
557
    ];
558

    
559
    $form['sso']['ldapImplementation'] = [
560
      '#type' => 'select',
561
      '#title' => t('Authentication Mechanism'),
562
      '#description' => t($this->ldapImplementationDescription),
563
      '#default_value' => $this->ldapImplementation,
564
      '#options' => $this->ldapImplementationOptions,
565
      '#disabled' => (boolean) (!$this->ssoEnabled),
566
    ];
567

    
568
    $form['sso']['ssoExcludedPaths'] = [
569
      '#type' => 'textarea',
570
      '#title' => t('SSO Excluded Paths'),
571
      '#description' => t($this->ssoExcludedPathsDescription),
572
      '#default_value' => $this->arrayToLines($this->ssoExcludedPaths),
573
      '#disabled' => (boolean) (!$this->ssoEnabled),
574
    ];
575

    
576
    $form['sso']['ssoExcludedHosts'] = [
577
      '#type' => 'textarea',
578
      '#title' => t('SSO Excluded Hosts'),
579
      '#description' => t($this->ssoExcludedHostsDescription),
580
      '#default_value' => $this->arrayToLines($this->ssoExcludedHosts),
581
      '#disabled' => (boolean) (!$this->ssoEnabled),
582
    ];
583

    
584
    $form['submit'] = [
585
      '#type' => 'submit',
586
      '#value' => 'Save',
587
    ];
588

    
589
    return $form;
590
  }
591

    
592
  /**
593
   * Validate form, not object.
594
   */
595
  public function drupalFormValidate($values) {
596

    
597
    $this->populateFromDrupalForm($values);
598

    
599
    $errors = $this->validate();
600

    
601
    return $errors;
602
  }
603

    
604
  /**
605
   * Validate object, not form.
606
   */
607
  public function validate() {
608
    $errors = [];
609

    
610
    $enabled_servers = ldap_servers_get_servers(NULL, 'enabled');
611
    if ($this->ssoEnabled) {
612
      foreach ($this->sids as $sid => $discard) {
613
        if ($enabled_servers[$sid]->bind_method == LDAP_SERVERS_BIND_METHOD_USER || $enabled_servers[$sid]->bind_method == LDAP_SERVERS_BIND_METHOD_ANON_USER) {
614
          $methods = [
615
            LDAP_SERVERS_BIND_METHOD_USER => 'Bind with Users Credentials',
616
            LDAP_SERVERS_BIND_METHOD_ANON_USER => 'Anonymous Bind for search, then Bind with Users Credentials',
617
          ];
618
          $tokens = [
619
            '!edit' => l($enabled_servers[$sid]->name, LDAP_SERVERS_INDEX_BASE_PATH . '/edit/' . $sid),
620
            '%sid' => $sid,
621
            '%bind_method' => $methods[$enabled_servers[$sid]->bind_method],
622
          ];
623

    
624
          $errors['ssoEnabled'] = t('Single Sign On is not valid with the server !edit (id=%sid) because that server configuration uses %bind_method.  Since the user\'s credentials are never available to this module with single sign on enabled, there is no way for the ldap module to bind to the ldap server with credentials.', $tokens);
625
        }
626
      }
627
    }
628
    return $errors;
629
  }
630

    
631
  /**
632
   *
633
   */
634
  protected function populateFromDrupalForm($values) {
635

    
636
    $this->authenticationMode = ($values['authenticationMode']) ? (int) $values['authenticationMode'] : NULL;
637
    $this->sids = $values['authenticationServers'];
638
    $this->allowOnlyIfTextInDn = $this->linesToArray($values['allowOnlyIfTextInDn']);
639
    $this->excludeIfTextInDn = $this->linesToArray($values['excludeIfTextInDn']);
640
    $this->allowTestPhp = $values['allowTestPhp'];
641
    $this->loginUIUsernameTxt = ($values['loginUIUsernameTxt']) ? (string) $values['loginUIUsernameTxt'] : NULL;
642
    $this->loginUIPasswordTxt = ($values['loginUIPasswordTxt']) ? (string) $values['loginUIPasswordTxt'] : NULL;
643
    $this->ldapUserHelpLinkUrl = ($values['ldapUserHelpLinkUrl']) ? (string) $values['ldapUserHelpLinkUrl'] : NULL;
644
    $this->ldapUserHelpLinkText = ($values['ldapUserHelpLinkText']) ? (string) $values['ldapUserHelpLinkText'] : NULL;
645
    $this->excludeIfNoAuthorizations = ($values['excludeIfNoAuthorizations']) ? (int) $values['excludeIfNoAuthorizations'] : NULL;
646
    $this->emailOption = ($values['emailOption']) ? (int) $values['emailOption'] : NULL;
647
    $this->emailUpdate = ($values['emailUpdate']) ? (int) $values['emailUpdate'] : NULL;
648
    $this->passwordOption = ($values['passwordOption']) ? (int) $values['passwordOption'] : NULL;
649
    $this->ssoExcludedPaths = $this->linesToArray($values['ssoExcludedPaths']);
650
    $this->ssoExcludedHosts = $this->linesToArray($values['ssoExcludedHosts']);
651
    $this->ssoRemoteUserStripDomainName = ($values['ssoRemoteUserStripDomainName']) ? (int) $values['ssoRemoteUserStripDomainName'] : NULL;
652
    $this->seamlessLogin = ($values['seamlessLogin']) ? (int) $values['seamlessLogin'] : NULL;
653
    $this->ssoNotifyAuthentication = ($values['ssoNotifyAuthentication']) ? (int) $values['ssoNotifyAuthentication'] : NULL;
654
    $this->cookieExpire = ($values['cookieExpire']) ? (int) $values['cookieExpire'] : NULL;
655
    $this->ldapImplementation = ($values['ldapImplementation']) ? (string) $values['ldapImplementation'] : NULL;
656
    $this->emailTemplateHandling = ($values['emailTemplateHandling']) ? (int) $values['emailTemplateHandling'] : NULL;
657
    $this->emailTemplate = ($values['emailTemplate']) ? $values['emailTemplate'] : '';
658
    $this->templateUsagePromptUser = ($values['templateUsagePromptUser']) ? 1 : 0;
659
    $this->templateUsageResolveConflict = ($values['templateUsageResolveConflict']) ? 1 : 0;
660
    $this->templateUsagePromptRegex = ($values['templateUsagePromptRegex']) ? $values['templateUsagePromptRegex'] : '';
661
    $this->templateUsageRedirectOnLogin = ($values['templateUsageRedirectOnLogin']) ? 1 : 0;
662
    $this->templateUsageNeverUpdate = ($values['templateUsageNeverUpdate']) ? 1 : 0;
663
  }
664

    
665
  /**
666
   *
667
   */
668
  public function drupalFormSubmit($values) {
669

    
670
    $this->populateFromDrupalForm($values);
671
    try {
672
      $save_result = $this->save();
673
    }
674
    catch (Exception $e) {
675
      $this->errorName = 'Save Error';
676
      $this->errorMsg = t('Failed to save object.  Your form data was not saved.');
677
      $this->hasError = TRUE;
678
    }
679

    
680
  }
681

    
682
  /**
683
   *
684
   */
685
  protected function arrayToLines($array) {
686
    $lines = "";
687
    if (is_array($array)) {
688
      $lines = join("\n", $array);
689
    }
690
    elseif (is_array(@unserialize($array))) {
691
      $lines = join("\n", unserialize($array));
692
    }
693
    return $lines;
694
  }
695

    
696
  /**
697
   *
698
   */
699
  protected function linesToArray($lines) {
700
    $lines = trim($lines);
701

    
702
    if ($lines) {
703
      $array = preg_split('/[\n\r]+/', $lines);
704
      foreach ($array as $i => $value) {
705
        $array[$i] = trim($value);
706
      }
707
    }
708
    else {
709
      $array = [];
710
    }
711
    return $array;
712
  }
713

    
714
}