Projet

Général

Profil

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

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

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
class LdapAuthenticationConfAdmin extends LdapAuthenticationConf {
11

    
12
  protected function setTranslatableProperties() {
13

    
14
    /**
15
     * 0.  Logon Options
16
     */
17

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

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

    
31
    /**
32
     * User Login Interface
33
     */
34
    $values['loginUIUsernameTxtDescription'] = t('Text to be displayed to user below the username field of
35
     the user login screen.');
36

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

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

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

    
46

    
47
    /**
48
     * LDAP User Restrictions
49
     */
50

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

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

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

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

    
67
    /**
68
    * Email
69
    */
70

    
71
    $values['emailOptionOptions'] = array(
72
      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.'),
73
      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.'),
74
      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.'),
75
      );
76

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

    
88

    
89
    /**
90
    * Password
91
    */
92

    
93
    $values['passwordUpdateOptions'] = array(
94
      LDAP_AUTHENTICATION_PASSWORD_FIELD_SHOW => t('Display password field disabled (Prevents password updates).'),
95
      LDAP_AUTHENTICATION_PASSWORD_FIELD_HIDE => t('Don\'t show password field on user forms except login form.'),
96
      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.'),
97
      );
98

    
99
    /**
100
     *  Single Sign-On / Seamless Sign-On
101
     */
102

    
103
      $values['ldapImplementationOptions'] = array(
104
        'mod_auth_sspi' => t('mod_auth_sspi'),
105
        'mod_auth_kerb' => t('mod_auth_kerb'),
106
        );
107

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

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

    
121
      $values['ssoExcludedPathsDescription'] = '<p>' .
122
        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.
123
          Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.",
124
          array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>'));
125
        '</p>';
126

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

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

    
149
      foreach ($values as $property => $default_value) {
150
        $this->$property = $default_value;
151
      }
152
    }
153

    
154
  /**
155
   * 0.  Logon Options
156
   */
157
  public $authenticationModeDefault = LDAP_AUTHENTICATION_MIXED;
158
  public $authenticationModeOptions;
159

    
160
  protected $authenticationServersDescription;
161
  protected $authenticationServersOptions = array();
162

    
163
  /**
164
   * 1.  User Login Interface
165
   */
166
  protected $loginUIUsernameTxtDescription;
167
  protected $loginUIPasswordTxtDescription;
168
  protected $ldapUserHelpLinkUrlDescription;
169
  protected $ldapUserHelpLinkTextDescription;
170

    
171

    
172
  /**
173
   * 2.  LDAP User Restrictions
174
   */
175

    
176
  protected $allowOnlyIfTextInDnDescription;
177
  protected $excludeIfTextInDnDescription;
178
  protected $allowTestPhpDescription;
179

    
180
   /**
181
   * 4. Email
182
   */
183

    
184
  public $emailOptionDefault = LDAP_AUTHENTICATION_EMAIL_FIELD_REMOVE;
185
  public $emailOptionOptions;
186

    
187
  public $emailUpdateDefault = LDAP_AUTHENTICATION_EMAIL_UPDATE_ON_LDAP_CHANGE_ENABLE_NOTIFY;
188
  public $emailUpdateOptions;
189
  
190
  public $emailTemplateHandlingDefault = LDAP_AUTHENTICATION_EMAIL_TEMPLATE_DEFAULT;
191
  public $emailTemplateHandlingOptions;
192
  
193
  public $emailTemplateDefault = LDAP_AUTHENTICATION_DEFAULT_TEMPLATE;
194
  
195
  public $templateUsagePromptUserDefault = LDAP_AUTHENTICATION_TEMPLATE_USAGE_PROMPT_USER_DEFAULT;
196
  
197
  public $templateUsagePromptRegexDefault = LDAP_AUTHENTICATION_DEFAULT_TEMPLATE_REGEX;
198
  
199
  public $templateUsageNeverUpdateDefault = LDAP_AUTHENTICATION_TEMPLATE_USAGE_NEVER_UPDATE_DEFAULT;
200

    
201
   /**
202
   * 5. Single Sign-On / Seamless Sign-On
203
   */
204

    
205
  public $ssoEnabledDescription;
206
  public $ssoRemoteUserStripDomainNameDescription;
207
  public $ldapImplementationOptions;
208
  public $cookieExpirePeriod;
209
  public $seamlessLogInDescription;
210
  public $cookieExpireDescription;
211
  public $ldapImplementationDescription;
212

    
213

    
214
  public $errorMsg = NULL;
215
  public $hasError = FALSE;
216
  public $errorName = NULL;
217

    
218
  public function clearError() {
219
    $this->hasError = FALSE;
220
    $this->errorMsg = NULL;
221
    $this->errorName = NULL;
222
  }
223

    
224
  public function save() {
225
    foreach ($this->saveable as $property) {
226
      $save[$property] = $this->{$property};
227
    }
228
    variable_set('ldap_authentication_conf', $save);
229
    $this->load();
230
  }
231

    
232
  static public function getSaveableProperty($property) {
233
    $ldap_authentication_conf = variable_get('ldap_authentication_conf', array());
234
    return isset($ldap_authentication_conf[$property]) ? $ldap_authentication_conf[$property] : FALSE;
235

    
236
  }
237

    
238
  static public function uninstall() {
239
    variable_del('ldap_authentication_conf');
240
  }
241

    
242
  public function __construct() {
243
    parent::__construct();
244
    $this->setTranslatableProperties();
245
    if ($servers = ldap_servers_get_servers(NULL, 'enabled')) {
246
      foreach ($servers as $sid => $ldap_server) {
247
        $enabled = ($ldap_server->status) ? 'Enabled' : 'Disabled';
248
        $this->authenticationServersOptions[$sid] = $ldap_server->name . ' (' . $ldap_server->address . ') Status: ' . $enabled;
249
      }
250
    }
251
  }
252

    
253

    
254
  public function drupalForm() {
255

    
256
    if (count($this->authenticationServersOptions) == 0) {
257
      $message = ldap_servers_no_enabled_servers_msg('configure LDAP Authentication');
258
      $form['intro'] = array(
259
        '#type' => 'item',
260
        '#markup' => t('<h1>LDAP Authentication Settings</h1>') . $message,
261
      );
262
      return $form;
263
    }
264

    
265
    $tokens = array();  // not sure what the tokens would be for this form?
266

    
267
    $form['intro'] = array(
268
        '#type' => 'item',
269
        '#markup' => t('<h1>LDAP Authentication Settings</h1>'),
270
    );
271

    
272
    $form['logon'] = array(
273
      '#type' => 'fieldset',
274
      '#title' => t('Logon Options'),
275
      '#collapsible' => TRUE,
276
      '#collapsed' => FALSE,
277
    );
278

    
279
    $form['logon']['authenticationMode'] = array(
280
      '#type' => 'radios',
281
      '#title' => t('Allowable Authentications'),
282
      '#required' => 1,
283
      '#default_value' => $this->authenticationMode,
284
      '#options' => $this->authenticationModeOptions,
285
    );
286

    
287
    $form['logon']['authenticationServers'] = array(
288
      '#type' => 'checkboxes',
289
      '#title' => t('Authentication LDAP Server Configurations'),
290
      '#required' => FALSE,
291
      '#default_value' => $this->sids,
292
      '#options' => $this->authenticationServersOptions,
293
      '#description' => $this->authenticationServersDescription
294
    );
295

    
296
    $form['login_UI'] = array(
297
      '#type' => 'fieldset',
298
      '#title' => t('User Login Interface'),
299
      '#collapsible' => TRUE,
300
      '#collapsed' => FALSE,
301
    );
302

    
303
    $form['login_UI']['loginUIUsernameTxt'] = array(
304
      '#type' => 'textfield',
305
      '#title' => t('Username Description Text'),
306
      '#required' => 0,
307
      '#default_value' => $this->loginUIUsernameTxt,
308
      '#description' => $this->loginUIUsernameTxtDescription,
309
    );
310

    
311
    $form['login_UI']['loginUIPasswordTxt'] = array(
312
      '#type' => 'textfield',
313
      '#title' => t('Password Description Text'),
314
      '#required' => 0,
315
      '#default_value' => $this->loginUIPasswordTxt,
316
      '#description' => $this->loginUIPasswordTxtDescription,
317
    );
318

    
319
    $form['login_UI']['ldapUserHelpLinkUrl'] = array(
320
      '#type' => 'textfield',
321
      '#title' => t('LDAP Account User Help URL'),
322
      '#required' => 0,
323
      '#default_value' => $this->ldapUserHelpLinkUrl,
324
      '#description' => $this->ldapUserHelpLinkUrlDescription,
325
    );
326

    
327

    
328
    $form['login_UI']['ldapUserHelpLinkText'] = array(
329
      '#type' => 'textfield',
330
      '#title' => t('LDAP Account User Help Link Text'),
331
      '#required' => 0,
332
      '#default_value' => $this->ldapUserHelpLinkText,
333
      '#description' => $this->ldapUserHelpLinkTextDescription,
334
    );
335

    
336
    $form['restrictions'] = array(
337
      '#type' => 'fieldset',
338
      '#title' => t('LDAP User "Whitelists" and Restrictions'),
339
      '#collapsible' => TRUE,
340
      '#collapsed' => FALSE,
341
    );
342

    
343

    
344
    $form['restrictions']['allowOnlyIfTextInDn'] = array(
345
      '#type' => 'textarea',
346
      '#title' => t('Allow Only Text Test'),
347
      '#default_value' => $this->arrayToLines($this->allowOnlyIfTextInDn),
348
      '#cols' => 50,
349
      '#rows' => 3,
350
      '#description' => t($this->allowOnlyIfTextInDnDescription, $tokens),
351
    );
352

    
353
    $form['restrictions']['excludeIfTextInDn'] = array(
354
      '#type' => 'textarea',
355
      '#title' => t('Excluded Text Test'),
356
      '#default_value' => $this->arrayToLines($this->excludeIfTextInDn),
357
      '#cols' => 50,
358
      '#rows' => 3,
359
      '#description' => t($this->excludeIfTextInDnDescription, $tokens),
360
    );
361

    
362
    $form['restrictions']['allowTestPhp'] = array(
363
      '#type' => 'textarea',
364
      '#title' => t('PHP to Test for Allowed LDAP Users'),
365
      '#default_value' => $this->allowTestPhp,
366
      '#cols' => 50,
367
      '#rows' => 3,
368
      '#description' => t($this->allowTestPhpDescription, $tokens),
369
      '#disabled' => (boolean)(!module_exists('php')),
370
    );
371

    
372
    if (!module_exists('php')) {
373
      $form['restrictions']['allowTestPhp']['#title'] .= ' <em>' . t('php module currently disabled') . '</em>';
374
    }
375

    
376
    $form['restrictions']['excludeIfNoAuthorizations'] = array(
377
      '#type' => 'checkbox',
378
      '#title' => t('Deny access to users without Ldap Authorization Module
379
        authorization mappings such as Drupal roles.
380
        Requires LDAP Authorization to be enabled and configured!'),
381
      '#default_value' => $this->excludeIfNoAuthorizations,
382
      '#description' => t($this->excludeIfNoAuthorizationsDescription, $tokens),
383
      '#disabled' => (boolean)(!module_exists('ldap_authorization')),
384
    );
385

    
386
    $form['email'] = array(
387
      '#type' => 'fieldset',
388
      '#title' => t('Email'),
389
      '#collapsible' => TRUE,
390
      '#collapsed' => FALSE,
391
    );
392

    
393
    $form['email']['emailOption'] = array(
394
      '#type' => 'radios',
395
      '#title' => t('Email Behavior'),
396
      '#required' => 1,
397
      '#default_value' => $this->emailOption,
398
      '#options' => $this->emailOptionOptions,
399
    );
400

    
401
    $form['email']['emailUpdate'] = array(
402
      '#type' => 'radios',
403
      '#title' => t('Email Update'),
404
      '#required' => 1,
405
      '#default_value' => $this->emailUpdate,
406
      '#options' => $this->emailUpdateOptions,
407
      );
408
    
409
    $form['email']['template'] = array(
410
      '#type' => 'fieldset',
411
      '#collapsible' => TRUE,
412
      '#title' => t('Email Templates'),
413
    );
414
    
415
    $form['email']['template']['emailTemplateHandling'] = array(
416
      '#type' => 'radios',
417
      '#title' => t('Email Template Handling'),
418
      '#required' => 1,
419
      '#default_value' => $this->emailTemplateHandling,
420
      '#options' => $this->emailTemplateHandlingOptions
421
    );
422
    
423
    $form['email']['template']['emailTemplate'] = array(
424
      '#type' => 'textfield',
425
      '#title' => t('Email Template'),
426
      '#required' => 0,
427
      '#default_value' => $this->emailTemplate,
428
    );
429
    
430
    $form['email']['template']['templateUsageResolveConflict'] = array(
431
      '#type' => 'checkbox',
432
      '#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.'),
433
      '#default_value' => $this->templateUsageResolveConflict,
434
    );
435
    
436
    $form['email']['template']['templateUsageNeverUpdate'] = array(
437
      '#type' => 'checkbox',
438
      '#title' => t('Ignore the Email Update settings and never update the stored email if the template is used.'),
439
      '#default_value' => $this->templateUsageNeverUpdate,
440
    );
441
    
442
    $form['email']['prompts'] = array(
443
      '#type' => 'fieldset',
444
      '#collapsible' => TRUE,
445
      '#title' => t('User Email Prompt'),
446
      '#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.'),      
447
    );
448
    
449
    $form['email']['prompts']['templateUsagePromptUser'] = array(
450
      '#type' => 'checkbox',
451
      '#title' => t('Prompt user for email on every page load.'),
452
      '#default_value' => $this->templateUsagePromptUser,
453
    );
454
    
455
    $form['email']['prompts']['templateUsageRedirectOnLogin'] = array(
456
      '#type' => 'checkbox',
457
      '#title' => t('Redirect the user to the form after logging in.'),
458
      '#default_value' => $this->templateUsageRedirectOnLogin,
459
    );
460
    
461
    $form['email']['prompts']['templateUsagePromptRegex'] = array(
462
      '#type' => 'textfield',
463
      '#default_value' => $this->templateUsagePromptRegex,
464
      '#title' => t('Template Regex'),
465
      '#description' => t('This regex will be used to determine if the template was used to create an account.'),
466
    );
467
    
468

    
469
    $form['password'] = array(
470
      '#type' => 'fieldset',
471
      '#title' => t('Password'),
472
      '#collapsible' => TRUE,
473
      '#collapsed' => FALSE,
474
    );
475
    $form['password']['passwordOption'] = array(
476
      '#type' => 'radios',
477
      '#title' => t('Password Behavior'),
478
      '#required' => 1,
479
      '#default_value' => $this->passwordOption,
480
      '#options' => $this->passwordUpdateOptions,
481
    );
482

    
483
    /**
484
     * Begin single sign-on settings
485
     */
486
    $form['sso'] = array(
487
      '#type' => 'fieldset',
488
      '#title' => t('Single Sign-On'),
489
      '#collapsible' => TRUE,
490
      '#collapsed' => (boolean)(!$this->ssoEnabled),
491
    );
492

    
493
    if ($this->ssoEnabled) {
494
      $form['sso']['enabled'] = array(
495
        '#type' => 'markup',
496
        '#markup' => $this->ssoEnabledDescription,
497
      );
498
    }
499
    else {
500
      $form['sso']['disabled'] = array(
501
        '#type' => 'markup',
502
        '#markup' => '<p><em>' . t('LDAP Single Sign-On module must be enabled for options below to work.')
503
        . ' ' . t('It is currently disabled.')
504
        . ' ' . l(t('See modules form'), 'admin/modules') . '</p></em>',
505
      );
506
    }
507

    
508
    $form['sso']['ssoRemoteUserStripDomainName'] = array(
509
      '#type' => 'checkbox',
510
      '#title' => t('Strip REMOTE_USER domain name'),
511
      '#description' => t($this->ssoRemoteUserStripDomainNameDescription),
512
      '#default_value' => $this->ssoRemoteUserStripDomainName,
513
      '#disabled' => (boolean)(!$this->ssoEnabled),
514
    );
515

    
516
    $form['sso']['seamlessLogin'] = array(
517
      '#type' => 'checkbox',
518
      '#title' => t('Turn on automated/seamless single sign-on'),
519
      '#description' => t($this->seamlessLogInDescription),
520
      '#default_value' => $this->seamlessLogin,
521
      '#disabled' => (boolean)(!$this->ssoEnabled),
522
      );
523

    
524
    $form['sso']['ssoNotifyAuthentication'] = array(
525
      '#type' => 'checkbox',
526
      '#title' => t('Notify user of successful authentication'),
527
      '#description' => t($this->ssoNotifyAuthenticationDescription),
528
      '#default_value' => $this->ssoNotifyAuthentication,
529
      '#disabled' => (boolean)(!$this->ssoEnabled),
530
      );
531

    
532
    $form['sso']['cookieExpire'] = array(
533
      '#type' => 'select',
534
      '#title' => t('Cookie Lifetime'),
535
      '#description' => t($this->cookieExpireDescription),
536
      '#default_value' => $this->cookieExpire,
537
      '#options' => $this->cookieExpirePeriod,
538
      '#disabled' => (boolean)(!$this->ssoEnabled),
539
    );
540

    
541
    $form['sso']['ldapImplementation'] = array(
542
      '#type' => 'select',
543
      '#title' => t('Authentication Mechanism'),
544
      '#description' => t($this->ldapImplementationDescription),
545
      '#default_value' => $this->ldapImplementation,
546
      '#options' => $this->ldapImplementationOptions,
547
      '#disabled' => (boolean)(!$this->ssoEnabled),
548
    );
549

    
550
    $form['sso']['ssoExcludedPaths'] = array(
551
      '#type' => 'textarea',
552
      '#title' => t('SSO Excluded Paths'),
553
      '#description' => t($this->ssoExcludedPathsDescription),
554
      '#default_value' => $this->arrayToLines($this->ssoExcludedPaths),
555
      '#disabled' => (boolean)(!$this->ssoEnabled),
556
    );
557

    
558
    $form['sso']['ssoExcludedHosts'] = array(
559
      '#type' => 'textarea',
560
      '#title' => t('SSO Excluded Hosts'),
561
      '#description' => t($this->ssoExcludedHostsDescription),
562
      '#default_value' => $this->arrayToLines($this->ssoExcludedHosts),
563
      '#disabled' => (boolean)(!$this->ssoEnabled),
564
    );
565

    
566
    $form['submit'] = array(
567
      '#type' => 'submit',
568
      '#value' => 'Save',
569
    );
570

    
571
  return $form;
572
}
573

    
574
/**
575
 * validate form, not object
576
 */
577
  public function drupalFormValidate($values)  {
578

    
579
    $this->populateFromDrupalForm($values);
580

    
581
    $errors = $this->validate();
582

    
583
    return $errors;
584
  }
585

    
586
/**
587
 * validate object, not form
588
 */
589
  public function validate() {
590
    $errors = array();
591

    
592
    $enabled_servers = ldap_servers_get_servers(NULL, 'enabled');
593
    if ($this->ssoEnabled) {
594
      foreach ($this->sids as $sid => $discard) {
595
        if ($enabled_servers[$sid]->bind_method == LDAP_SERVERS_BIND_METHOD_USER || $enabled_servers[$sid]->bind_method == LDAP_SERVERS_BIND_METHOD_ANON_USER) {
596
          $methods = array(
597
            LDAP_SERVERS_BIND_METHOD_USER => 'Bind with Users Credentials',
598
            LDAP_SERVERS_BIND_METHOD_ANON_USER => 'Anonymous Bind for search, then Bind with Users Credentials',
599
          );
600
          $tokens = array(
601
            '!edit' => l($enabled_servers[$sid]->name, LDAP_SERVERS_INDEX_BASE_PATH . '/edit/' . $sid),
602
            '%sid' => $sid,
603
            '%bind_method' => $methods[$enabled_servers[$sid]->bind_method],
604
          );
605

    
606
          $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);
607
        }
608
      }
609
    }
610
    return $errors;
611
  }
612

    
613
  protected function populateFromDrupalForm($values) {
614

    
615
    $this->authenticationMode = ($values['authenticationMode']) ? (int)$values['authenticationMode'] : NULL;
616
    $this->sids = $values['authenticationServers'];
617
    $this->allowOnlyIfTextInDn = $this->linesToArray($values['allowOnlyIfTextInDn']);
618
    $this->excludeIfTextInDn = $this->linesToArray($values['excludeIfTextInDn']);
619
    $this->allowTestPhp = $values['allowTestPhp'];
620
    $this->loginUIUsernameTxt = ($values['loginUIUsernameTxt']) ? (string)$values['loginUIUsernameTxt'] : NULL;
621
    $this->loginUIPasswordTxt = ($values['loginUIPasswordTxt']) ? (string)$values['loginUIPasswordTxt'] : NULL;
622
    $this->ldapUserHelpLinkUrl = ($values['ldapUserHelpLinkUrl']) ? (string)$values['ldapUserHelpLinkUrl'] : NULL;
623
    $this->ldapUserHelpLinkText = ($values['ldapUserHelpLinkText']) ? (string)$values['ldapUserHelpLinkText'] : NULL;
624
    $this->excludeIfNoAuthorizations = ($values['excludeIfNoAuthorizations']) ? (int)$values['excludeIfNoAuthorizations'] : NULL;
625
    $this->emailOption  = ($values['emailOption']) ? (int)$values['emailOption'] : NULL;
626
    $this->emailUpdate  = ($values['emailUpdate']) ? (int)$values['emailUpdate'] : NULL;
627
    $this->passwordOption  = ($values['passwordOption']) ? (int)$values['passwordOption'] : NULL;
628
    $this->ssoExcludedPaths = $this->linesToArray($values['ssoExcludedPaths']);
629
    $this->ssoExcludedHosts = $this->linesToArray($values['ssoExcludedHosts']);
630
    $this->ssoRemoteUserStripDomainName = ($values['ssoRemoteUserStripDomainName']) ? (int)$values['ssoRemoteUserStripDomainName'] : NULL;
631
    $this->seamlessLogin = ($values['seamlessLogin']) ? (int)$values['seamlessLogin'] : NULL;
632
    $this->ssoNotifyAuthentication = ($values['ssoNotifyAuthentication']) ? (int)$values['ssoNotifyAuthentication'] : NULL;
633
    $this->cookieExpire = ($values['cookieExpire']) ? (int)$values['cookieExpire'] : NULL;
634
    $this->ldapImplementation = ($values['ldapImplementation']) ? (string)$values['ldapImplementation'] : NULL;
635
    $this->emailTemplateHandling = ($values['emailTemplateHandling']) ? (int) $values['emailTemplateHandling'] : NULL;
636
    $this->emailTemplate = ($values['emailTemplate']) ? $values['emailTemplate'] : '';
637
    $this->templateUsagePromptUser = ($values['templateUsagePromptUser']) ? 1 : 0;
638
    $this->templateUsageResolveConflict = ($values['templateUsageResolveConflict']) ? 1 : 0;
639
    $this->templateUsagePromptRegex = ($values['templateUsagePromptRegex']) ? $values['templateUsagePromptRegex'] : '';
640
    $this->templateUsageRedirectOnLogin = ($values['templateUsageRedirectOnLogin']) ? 1 : 0;
641
    $this->templateUsageNeverUpdate = ($values['templateUsageNeverUpdate']) ? 1 : 0;
642
  }
643

    
644
  public function drupalFormSubmit($values) {
645

    
646
    $this->populateFromDrupalForm($values);
647
    try {
648
      $save_result = $this->save();
649
    }
650
    catch (Exception $e) {
651
      $this->errorName = 'Save Error';
652
      $this->errorMsg = t('Failed to save object.  Your form data was not saved.');
653
      $this->hasError = TRUE;
654
    }
655

    
656
  }
657

    
658
  protected function arrayToLines($array) {
659
        $lines = "";
660
        if (is_array($array)) {
661
          $lines = join("\n", $array);
662
        }
663
        elseif (is_array(@unserialize($array))) {
664
          $lines = join("\n", unserialize($array));
665
        }
666
        return $lines;
667
      }
668

    
669
  protected function linesToArray($lines) {
670
    $lines = trim($lines);
671

    
672
    if ($lines) {
673
      $array = preg_split('/[\n\r]+/', $lines);
674
      foreach ($array as $i => $value) {
675
        $array[$i] = trim($value);
676
      }
677
    }
678
    else {
679
      $array = array();
680
    }
681
    return $array;
682
  }
683

    
684
}