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 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5 32700c57 Assos Assos
 * This classextends by LdapAuthenticationConf for configuration and other admin functions.
6 85ad3d82 Assos Assos
 */
7
8
ldap_servers_module_load_include('php', 'ldap_authentication', 'LdapAuthenticationConf.class');
9 32700c57 Assos Assos
/**
10
 *
11
 */
12 85ad3d82 Assos Assos
class LdapAuthenticationConfAdmin extends LdapAuthenticationConf {
13
14 32700c57 Assos Assos
  /**
15
   *
16
   */
17 85ad3d82 Assos Assos
  protected function setTranslatableProperties() {
18
19
    /**
20
     * 0.  Logon Options
21
     */
22
23 32700c57 Assos Assos
    $values['authenticationModeOptions'] = [
24 85ad3d82 Assos Assos
      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 32700c57 Assos Assos
    ];
31 85ad3d82 Assos Assos
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 32700c57 Assos Assos
    $values['ldapUserHelpLinkTextDescription'] = t('Text for above link e.g. Account Help or Campus Password Help Page');
50 85ad3d82 Assos Assos
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 32700c57 Assos Assos
    $values['emailOptionOptions'] = [
76 7547bb19 Assos Assos
      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 32700c57 Assos Assos
    ];
80 85ad3d82 Assos Assos
81 32700c57 Assos Assos
    $values['emailUpdateOptions'] = [
82 85ad3d82 Assos Assos
      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 32700c57 Assos Assos
    ];
86
    $values['emailTemplateHandlingOptions'] = [
87 dd54aff9 Assos Assos
      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 32700c57 Assos Assos
    ];
91 85ad3d82 Assos Assos
92
    /**
93
    * Password
94
    */
95
96 32700c57 Assos Assos
    $values['passwordUpdateOptions'] = [
97 85ad3d82 Assos Assos
      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 32700c57 Assos Assos
    ];
101 85ad3d82 Assos Assos
102
    /**
103
     *  Single Sign-On / Seamless Sign-On
104
     */
105
106 32700c57 Assos Assos
    $values['ldapImplementationOptions'] = [
107
      'mod_auth_sspi' => t('mod_auth_sspi'),
108
      'mod_auth_kerb' => t('mod_auth_kerb'),
109
    ];
110 85ad3d82 Assos Assos
111 32700c57 Assos Assos
    $values['cookieExpirePeriod'] = [-1 => t('Session'), 0 => t('Immediately')] +
112
        drupal_map_assoc([3600, 86400, 604800, 2592000, 31536000, 315360000, 630720000], 'format_interval');
113 85ad3d82 Assos Assos
114 32700c57 Assos Assos
    $values['ssoEnabledDescription'] = '<strong>' . t('Single Sign on is enabled.') .
115 85ad3d82 Assos Assos
        '</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 32700c57 Assos Assos
        'performed on the web server. Please review the readme file of the ' .
121 bc175c27 Assos Assos
        'ldap_sso module for more information.')
122 85ad3d82 Assos Assos
        . '</p>';
123
124 32700c57 Assos Assos
    $values['ssoExcludedPathsDescription'] = '<p>' .
125 85ad3d82 Assos Assos
        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 32700c57 Assos Assos
          ['%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>']);
128
    '</p>';
129 85ad3d82 Assos Assos
130 32700c57 Assos Assos
    $values['ssoExcludedHostsDescription'] = '<p>' .
131 85ad3d82 Assos Assos
        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 32700c57 Assos Assos
    '</p>';
135 85ad3d82 Assos Assos
136 32700c57 Assos Assos
    $values['ssoRemoteUserStripDomainNameDescription'] = t('Useful when the ' .
137 85ad3d82 Assos Assos
        '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 32700c57 Assos Assos
    $values['ssoNotifyAuthenticationDescription'] = t('This displays a message to the ' .
142 85ad3d82 Assos Assos
        'user after they have succesfully authenticated using single sign on');
143 32700c57 Assos Assos
    $values['seamlessLogInDescription'] = t('This requires that you ' .
144 85ad3d82 Assos Assos
        'have operational NTLM or Kerberos authentication turned on for at least ' .
145
        'the path user/login/sso, or for the whole domain.');
146 32700c57 Assos Assos
    $values['cookieExpireDescription'] = t('If using the automated/seamless login, a ' .
147 85ad3d82 Assos Assos
        'cookie is necessary to prevent automatic login after a user ' .
148
        'manually logs out. Select the lifetime of the cookie.');
149 32700c57 Assos Assos
    $values['ldapImplementationDescription'] = t('Select the type of ' .
150 85ad3d82 Assos Assos
        'authentication mechanism you are using.');
151
152 32700c57 Assos Assos
    foreach ($values as $property => $default_value) {
153
      $this->$property = $default_value;
154 85ad3d82 Assos Assos
    }
155 32700c57 Assos Assos
  }
156 85ad3d82 Assos Assos
157
  /**
158 32700c57 Assos Assos
   * 0.  Logon Options.
159 85ad3d82 Assos Assos
   */
160
  public $authenticationModeDefault = LDAP_AUTHENTICATION_MIXED;
161
  public $authenticationModeOptions;
162
163
  protected $authenticationServersDescription;
164 32700c57 Assos Assos
  protected $authenticationServersOptions = [];
165 85ad3d82 Assos Assos
166
  /**
167 32700c57 Assos Assos
   * 1.  User Login Interface.
168 85ad3d82 Assos Assos
   */
169
  protected $loginUIUsernameTxtDescription;
170
  protected $loginUIPasswordTxtDescription;
171
  protected $ldapUserHelpLinkUrlDescription;
172
  protected $ldapUserHelpLinkTextDescription;
173
174
175
  /**
176 32700c57 Assos Assos
   * 2.  LDAP User Restrictions.
177 85ad3d82 Assos Assos
   */
178
179
  protected $allowOnlyIfTextInDnDescription;
180
  protected $excludeIfTextInDnDescription;
181
  protected $allowTestPhpDescription;
182
183 32700c57 Assos Assos
  /**
184
   * 4. Email.
185 85ad3d82 Assos Assos
   */
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 32700c57 Assos Assos
193 dd54aff9 Assos Assos
  public $emailTemplateHandlingDefault = LDAP_AUTHENTICATION_EMAIL_TEMPLATE_DEFAULT;
194
  public $emailTemplateHandlingOptions;
195 32700c57 Assos Assos
196 dd54aff9 Assos Assos
  public $emailTemplateDefault = LDAP_AUTHENTICATION_DEFAULT_TEMPLATE;
197 32700c57 Assos Assos
198 dd54aff9 Assos Assos
  public $templateUsagePromptUserDefault = LDAP_AUTHENTICATION_TEMPLATE_USAGE_PROMPT_USER_DEFAULT;
199 32700c57 Assos Assos
200 dd54aff9 Assos Assos
  public $templateUsagePromptRegexDefault = LDAP_AUTHENTICATION_DEFAULT_TEMPLATE_REGEX;
201 32700c57 Assos Assos
202 dd54aff9 Assos Assos
  public $templateUsageNeverUpdateDefault = LDAP_AUTHENTICATION_TEMPLATE_USAGE_NEVER_UPDATE_DEFAULT;
203 85ad3d82 Assos Assos
204 32700c57 Assos Assos
  /**
205
   * 5. Single Sign-On / Seamless Sign-On.
206 85ad3d82 Assos Assos
   */
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 32700c57 Assos Assos
  /**
222
   *
223
   */
224 85ad3d82 Assos Assos
  public function clearError() {
225
    $this->hasError = FALSE;
226
    $this->errorMsg = NULL;
227
    $this->errorName = NULL;
228
  }
229
230 32700c57 Assos Assos
  /**
231
   *
232
   */
233 85ad3d82 Assos Assos
  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 32700c57 Assos Assos
  /**
242
   *
243
   */
244
  public static function getSaveableProperty($property) {
245
    $ldap_authentication_conf = variable_get('ldap_authentication_conf', []);
246 85ad3d82 Assos Assos
    return isset($ldap_authentication_conf[$property]) ? $ldap_authentication_conf[$property] : FALSE;
247
248
  }
249
250 32700c57 Assos Assos
  /**
251
   *
252
   */
253
  public static function uninstall() {
254 85ad3d82 Assos Assos
    variable_del('ldap_authentication_conf');
255
  }
256
257 32700c57 Assos Assos
  /**
258
   *
259
   */
260 85ad3d82 Assos Assos
  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 32700c57 Assos Assos
  /**
272
   *
273
   */
274 85ad3d82 Assos Assos
  public function drupalForm() {
275
276
    if (count($this->authenticationServersOptions) == 0) {
277
      $message = ldap_servers_no_enabled_servers_msg('configure LDAP Authentication');
278 32700c57 Assos Assos
      $form['intro'] = [
279 85ad3d82 Assos Assos
        '#type' => 'item',
280
        '#markup' => t('<h1>LDAP Authentication Settings</h1>') . $message,
281 32700c57 Assos Assos
      ];
282 85ad3d82 Assos Assos
      return $form;
283
    }
284
285 32700c57 Assos Assos
    // Not sure what the tokens would be for this form?
286
    $tokens = [];
287 85ad3d82 Assos Assos
288 32700c57 Assos Assos
    $form['intro'] = [
289
      '#type' => 'item',
290
      '#markup' => t('<h1>LDAP Authentication Settings</h1>'),
291
    ];
292 85ad3d82 Assos Assos
293 32700c57 Assos Assos
    $form['logon'] = [
294 85ad3d82 Assos Assos
      '#type' => 'fieldset',
295
      '#title' => t('Logon Options'),
296
      '#collapsible' => TRUE,
297
      '#collapsed' => FALSE,
298 32700c57 Assos Assos
    ];
299 85ad3d82 Assos Assos
300 32700c57 Assos Assos
    $form['logon']['authenticationMode'] = [
301 85ad3d82 Assos Assos
      '#type' => 'radios',
302
      '#title' => t('Allowable Authentications'),
303
      '#required' => 1,
304
      '#default_value' => $this->authenticationMode,
305
      '#options' => $this->authenticationModeOptions,
306 32700c57 Assos Assos
    ];
307 85ad3d82 Assos Assos
308 32700c57 Assos Assos
    $form['logon']['authenticationServers'] = [
309 85ad3d82 Assos Assos
      '#type' => 'checkboxes',
310
      '#title' => t('Authentication LDAP Server Configurations'),
311
      '#required' => FALSE,
312
      '#default_value' => $this->sids,
313
      '#options' => $this->authenticationServersOptions,
314 32700c57 Assos Assos
      '#description' => $this->authenticationServersDescription,
315
    ];
316 85ad3d82 Assos Assos
317 32700c57 Assos Assos
    $form['login_UI'] = [
318 85ad3d82 Assos Assos
      '#type' => 'fieldset',
319
      '#title' => t('User Login Interface'),
320
      '#collapsible' => TRUE,
321
      '#collapsed' => FALSE,
322 32700c57 Assos Assos
    ];
323 85ad3d82 Assos Assos
324 32700c57 Assos Assos
    $form['login_UI']['loginUIUsernameTxt'] = [
325 85ad3d82 Assos Assos
      '#type' => 'textfield',
326
      '#title' => t('Username Description Text'),
327
      '#required' => 0,
328
      '#default_value' => $this->loginUIUsernameTxt,
329
      '#description' => $this->loginUIUsernameTxtDescription,
330 32700c57 Assos Assos
    ];
331 85ad3d82 Assos Assos
332 32700c57 Assos Assos
    $form['login_UI']['loginUIPasswordTxt'] = [
333 85ad3d82 Assos Assos
      '#type' => 'textfield',
334
      '#title' => t('Password Description Text'),
335
      '#required' => 0,
336
      '#default_value' => $this->loginUIPasswordTxt,
337
      '#description' => $this->loginUIPasswordTxtDescription,
338 32700c57 Assos Assos
    ];
339 85ad3d82 Assos Assos
340 32700c57 Assos Assos
    $form['login_UI']['ldapUserHelpLinkUrl'] = [
341 85ad3d82 Assos Assos
      '#type' => 'textfield',
342
      '#title' => t('LDAP Account User Help URL'),
343
      '#required' => 0,
344
      '#default_value' => $this->ldapUserHelpLinkUrl,
345
      '#description' => $this->ldapUserHelpLinkUrlDescription,
346 32700c57 Assos Assos
    ];
347 85ad3d82 Assos Assos
348 32700c57 Assos Assos
    $form['login_UI']['ldapUserHelpLinkText'] = [
349 85ad3d82 Assos Assos
      '#type' => 'textfield',
350
      '#title' => t('LDAP Account User Help Link Text'),
351
      '#required' => 0,
352
      '#default_value' => $this->ldapUserHelpLinkText,
353
      '#description' => $this->ldapUserHelpLinkTextDescription,
354 32700c57 Assos Assos
    ];
355 85ad3d82 Assos Assos
356 32700c57 Assos Assos
    $form['restrictions'] = [
357 85ad3d82 Assos Assos
      '#type' => 'fieldset',
358
      '#title' => t('LDAP User "Whitelists" and Restrictions'),
359
      '#collapsible' => TRUE,
360
      '#collapsed' => FALSE,
361 32700c57 Assos Assos
    ];
362 85ad3d82 Assos Assos
363 32700c57 Assos Assos
    $form['restrictions']['allowOnlyIfTextInDn'] = [
364 85ad3d82 Assos Assos
      '#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 32700c57 Assos Assos
    ];
371 85ad3d82 Assos Assos
372 32700c57 Assos Assos
    $form['restrictions']['excludeIfTextInDn'] = [
373 85ad3d82 Assos Assos
      '#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 32700c57 Assos Assos
    ];
380 85ad3d82 Assos Assos
381 32700c57 Assos Assos
    $form['restrictions']['allowTestPhp'] = [
382 85ad3d82 Assos Assos
      '#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 32700c57 Assos Assos
      '#disabled' => (boolean) (!module_exists('php')),
389
    ];
390 85ad3d82 Assos Assos
391
    if (!module_exists('php')) {
392
      $form['restrictions']['allowTestPhp']['#title'] .= ' <em>' . t('php module currently disabled') . '</em>';
393
    }
394
395 32700c57 Assos Assos
    $form['restrictions']['excludeIfNoAuthorizations'] = [
396 85ad3d82 Assos Assos
      '#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 bc175c27 Assos Assos
      '#default_value' => $this->excludeIfNoAuthorizations,
401 85ad3d82 Assos Assos
      '#description' => t($this->excludeIfNoAuthorizationsDescription, $tokens),
402 32700c57 Assos Assos
      '#disabled' => (boolean) (!module_exists('ldap_authorization')),
403
    ];
404 85ad3d82 Assos Assos
405 32700c57 Assos Assos
    $form['email'] = [
406 85ad3d82 Assos Assos
      '#type' => 'fieldset',
407
      '#title' => t('Email'),
408
      '#collapsible' => TRUE,
409
      '#collapsed' => FALSE,
410 32700c57 Assos Assos
    ];
411 85ad3d82 Assos Assos
412 32700c57 Assos Assos
    $form['email']['emailOption'] = [
413 85ad3d82 Assos Assos
      '#type' => 'radios',
414
      '#title' => t('Email Behavior'),
415
      '#required' => 1,
416
      '#default_value' => $this->emailOption,
417
      '#options' => $this->emailOptionOptions,
418 32700c57 Assos Assos
    ];
419 85ad3d82 Assos Assos
420 32700c57 Assos Assos
    $form['email']['emailUpdate'] = [
421 85ad3d82 Assos Assos
      '#type' => 'radios',
422
      '#title' => t('Email Update'),
423
      '#required' => 1,
424
      '#default_value' => $this->emailUpdate,
425
      '#options' => $this->emailUpdateOptions,
426 32700c57 Assos Assos
    ];
427
428
    $form['email']['template'] = [
429 dd54aff9 Assos Assos
      '#type' => 'fieldset',
430
      '#collapsible' => TRUE,
431
      '#title' => t('Email Templates'),
432 32700c57 Assos Assos
    ];
433
434
    $form['email']['template']['emailTemplateHandling'] = [
435 dd54aff9 Assos Assos
      '#type' => 'radios',
436
      '#title' => t('Email Template Handling'),
437
      '#required' => 1,
438
      '#default_value' => $this->emailTemplateHandling,
439 32700c57 Assos Assos
      '#options' => $this->emailTemplateHandlingOptions,
440
    ];
441
442
    $form['email']['template']['emailTemplate'] = [
443 dd54aff9 Assos Assos
      '#type' => 'textfield',
444
      '#title' => t('Email Template'),
445
      '#required' => 0,
446
      '#default_value' => $this->emailTemplate,
447 32700c57 Assos Assos
    ];
448
449
    $form['email']['template']['templateUsageResolveConflict'] = [
450 dd54aff9 Assos Assos
      '#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 32700c57 Assos Assos
    ];
454
455
    $form['email']['template']['templateUsageNeverUpdate'] = [
456 dd54aff9 Assos Assos
      '#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 32700c57 Assos Assos
    ];
460
461
    $form['email']['prompts'] = [
462 dd54aff9 Assos Assos
      '#type' => 'fieldset',
463
      '#collapsible' => TRUE,
464
      '#title' => t('User Email Prompt'),
465 32700c57 Assos Assos
      '#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 dd54aff9 Assos Assos
      '#type' => 'checkbox',
470
      '#title' => t('Prompt user for email on every page load.'),
471
      '#default_value' => $this->templateUsagePromptUser,
472 32700c57 Assos Assos
    ];
473
474
    $form['email']['prompts']['templateUsageRedirectOnLogin'] = [
475 dd54aff9 Assos Assos
      '#type' => 'checkbox',
476
      '#title' => t('Redirect the user to the form after logging in.'),
477
      '#default_value' => $this->templateUsageRedirectOnLogin,
478 32700c57 Assos Assos
    ];
479
480
    $form['email']['prompts']['templateUsagePromptRegex'] = [
481 dd54aff9 Assos Assos
      '#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 32700c57 Assos Assos
    ];
486 85ad3d82 Assos Assos
487 32700c57 Assos Assos
    $form['password'] = [
488 85ad3d82 Assos Assos
      '#type' => 'fieldset',
489
      '#title' => t('Password'),
490
      '#collapsible' => TRUE,
491
      '#collapsed' => FALSE,
492 32700c57 Assos Assos
    ];
493
    $form['password']['passwordOption'] = [
494 85ad3d82 Assos Assos
      '#type' => 'radios',
495
      '#title' => t('Password Behavior'),
496
      '#required' => 1,
497
      '#default_value' => $this->passwordOption,
498
      '#options' => $this->passwordUpdateOptions,
499 32700c57 Assos Assos
    ];
500 85ad3d82 Assos Assos
501
    /**
502
     * Begin single sign-on settings
503
     */
504 32700c57 Assos Assos
    $form['sso'] = [
505 85ad3d82 Assos Assos
      '#type' => 'fieldset',
506
      '#title' => t('Single Sign-On'),
507
      '#collapsible' => TRUE,
508 32700c57 Assos Assos
      '#collapsed' => (boolean) (!$this->ssoEnabled),
509
    ];
510 85ad3d82 Assos Assos
511
    if ($this->ssoEnabled) {
512 32700c57 Assos Assos
      $form['sso']['enabled'] = [
513 85ad3d82 Assos Assos
        '#type' => 'markup',
514
        '#markup' => $this->ssoEnabledDescription,
515 32700c57 Assos Assos
      ];
516 85ad3d82 Assos Assos
    }
517
    else {
518 32700c57 Assos Assos
      $form['sso']['disabled'] = [
519 85ad3d82 Assos Assos
        '#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 32700c57 Assos Assos
      ];
524 85ad3d82 Assos Assos
    }
525
526 32700c57 Assos Assos
    $form['sso']['ssoRemoteUserStripDomainName'] = [
527 85ad3d82 Assos Assos
      '#type' => 'checkbox',
528
      '#title' => t('Strip REMOTE_USER domain name'),
529
      '#description' => t($this->ssoRemoteUserStripDomainNameDescription),
530
      '#default_value' => $this->ssoRemoteUserStripDomainName,
531 32700c57 Assos Assos
      '#disabled' => (boolean) (!$this->ssoEnabled),
532
    ];
533 85ad3d82 Assos Assos
534 32700c57 Assos Assos
    $form['sso']['seamlessLogin'] = [
535 85ad3d82 Assos Assos
      '#type' => 'checkbox',
536
      '#title' => t('Turn on automated/seamless single sign-on'),
537
      '#description' => t($this->seamlessLogInDescription),
538
      '#default_value' => $this->seamlessLogin,
539 32700c57 Assos Assos
      '#disabled' => (boolean) (!$this->ssoEnabled),
540
    ];
541 85ad3d82 Assos Assos
542 32700c57 Assos Assos
    $form['sso']['ssoNotifyAuthentication'] = [
543 85ad3d82 Assos Assos
      '#type' => 'checkbox',
544
      '#title' => t('Notify user of successful authentication'),
545
      '#description' => t($this->ssoNotifyAuthenticationDescription),
546
      '#default_value' => $this->ssoNotifyAuthentication,
547 32700c57 Assos Assos
      '#disabled' => (boolean) (!$this->ssoEnabled),
548
    ];
549 85ad3d82 Assos Assos
550 32700c57 Assos Assos
    $form['sso']['cookieExpire'] = [
551 85ad3d82 Assos Assos
      '#type' => 'select',
552
      '#title' => t('Cookie Lifetime'),
553
      '#description' => t($this->cookieExpireDescription),
554
      '#default_value' => $this->cookieExpire,
555
      '#options' => $this->cookieExpirePeriod,
556 32700c57 Assos Assos
      '#disabled' => (boolean) (!$this->ssoEnabled),
557
    ];
558 85ad3d82 Assos Assos
559 32700c57 Assos Assos
    $form['sso']['ldapImplementation'] = [
560 85ad3d82 Assos Assos
      '#type' => 'select',
561
      '#title' => t('Authentication Mechanism'),
562
      '#description' => t($this->ldapImplementationDescription),
563
      '#default_value' => $this->ldapImplementation,
564
      '#options' => $this->ldapImplementationOptions,
565 32700c57 Assos Assos
      '#disabled' => (boolean) (!$this->ssoEnabled),
566
    ];
567 85ad3d82 Assos Assos
568 32700c57 Assos Assos
    $form['sso']['ssoExcludedPaths'] = [
569 85ad3d82 Assos Assos
      '#type' => 'textarea',
570
      '#title' => t('SSO Excluded Paths'),
571
      '#description' => t($this->ssoExcludedPathsDescription),
572
      '#default_value' => $this->arrayToLines($this->ssoExcludedPaths),
573 32700c57 Assos Assos
      '#disabled' => (boolean) (!$this->ssoEnabled),
574
    ];
575 85ad3d82 Assos Assos
576 32700c57 Assos Assos
    $form['sso']['ssoExcludedHosts'] = [
577 85ad3d82 Assos Assos
      '#type' => 'textarea',
578
      '#title' => t('SSO Excluded Hosts'),
579
      '#description' => t($this->ssoExcludedHostsDescription),
580
      '#default_value' => $this->arrayToLines($this->ssoExcludedHosts),
581 32700c57 Assos Assos
      '#disabled' => (boolean) (!$this->ssoEnabled),
582
    ];
583 85ad3d82 Assos Assos
584 32700c57 Assos Assos
    $form['submit'] = [
585 85ad3d82 Assos Assos
      '#type' => 'submit',
586
      '#value' => 'Save',
587 32700c57 Assos Assos
    ];
588 85ad3d82 Assos Assos
589 32700c57 Assos Assos
    return $form;
590
  }
591 85ad3d82 Assos Assos
592 32700c57 Assos Assos
  /**
593
   * Validate form, not object.
594
   */
595
  public function drupalFormValidate($values) {
596 85ad3d82 Assos Assos
597
    $this->populateFromDrupalForm($values);
598
599
    $errors = $this->validate();
600
601
    return $errors;
602
  }
603
604 32700c57 Assos Assos
  /**
605
   * Validate object, not form.
606
   */
607 85ad3d82 Assos Assos
  public function validate() {
608 32700c57 Assos Assos
    $errors = [];
609 85ad3d82 Assos Assos
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 32700c57 Assos Assos
          $methods = [
615 85ad3d82 Assos Assos
            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 32700c57 Assos Assos
          ];
618
          $tokens = [
619 85ad3d82 Assos Assos
            '!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 32700c57 Assos Assos
          ];
623 85ad3d82 Assos Assos
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 32700c57 Assos Assos
  /**
632
   *
633
   */
634 85ad3d82 Assos Assos
  protected function populateFromDrupalForm($values) {
635
636 32700c57 Assos Assos
    $this->authenticationMode = ($values['authenticationMode']) ? (int) $values['authenticationMode'] : NULL;
637 85ad3d82 Assos Assos
    $this->sids = $values['authenticationServers'];
638
    $this->allowOnlyIfTextInDn = $this->linesToArray($values['allowOnlyIfTextInDn']);
639
    $this->excludeIfTextInDn = $this->linesToArray($values['excludeIfTextInDn']);
640
    $this->allowTestPhp = $values['allowTestPhp'];
641 32700c57 Assos Assos
    $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 85ad3d82 Assos Assos
    $this->ssoExcludedPaths = $this->linesToArray($values['ssoExcludedPaths']);
650
    $this->ssoExcludedHosts = $this->linesToArray($values['ssoExcludedHosts']);
651 32700c57 Assos Assos
    $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 dd54aff9 Assos Assos
    $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 85ad3d82 Assos Assos
  }
664
665 32700c57 Assos Assos
  /**
666
   *
667
   */
668 85ad3d82 Assos Assos
  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 32700c57 Assos Assos
  /**
683
   *
684
   */
685 85ad3d82 Assos Assos
  protected function arrayToLines($array) {
686 32700c57 Assos Assos
    $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 85ad3d82 Assos Assos
696 32700c57 Assos Assos
  /**
697
   *
698
   */
699 85ad3d82 Assos Assos
  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 32700c57 Assos Assos
      $array = [];
710 85ad3d82 Assos Assos
    }
711
    return $array;
712
  }
713
714
}