You are here

function ldapdata_admin_edit in LDAP integration 6

Same name and namespace in other branches
  1. 5.2 ldapdata.module \ldapdata_admin_edit()
  2. 5 ldapdata.module \ldapdata_admin_edit()

Implements the LDAP server edit page.

Parameters

$form_state: A form state array.

$op: An operatin - edit or reset.

$sid: A LDAP server ID.

Return value

The form structure.

1 string reference to 'ldapdata_admin_edit'
ldapdata_menu in ./ldapdata.module
Implements hook_menu().

File

./ldapdata.admin.inc, line 107
Module admin page callbacks.

Code

function ldapdata_admin_edit(&$form_state, $op, $sid) {
  drupal_add_js(drupal_get_path('module', 'ldapdata') . '/ldapdata.admin.js');
  if ($op == "reset" && $sid) {
    $form['sid'] = array(
      '#type' => 'value',
      '#value' => $sid,
    );
    return confirm_form($form, t('Are you sure you want to reset the fields mapping to defaults ?'), 'admin/settings/ldap/ldapdata', t('<em>This action cannot be undone.</p>'), t('Reset'), t('Cancel'));
  }
  elseif ($op == "edit" && $sid) {
    $edit = db_fetch_array(db_query("SELECT * FROM {ldapauth} WHERE sid = %d", $sid));
    $ldapdata_mappings = $edit['ldapdata_mappings'] ? unserialize($edit['ldapdata_mappings']) : array();
    $ldapdata_roattrs = $edit['ldapdata_roattrs'] ? unserialize($edit['ldapdata_roattrs']) : array();
    $ldapdata_rwattrs = $edit['ldapdata_rwattrs'] ? unserialize($edit['ldapdata_rwattrs']) : array();
    $ldapdata_attrs = $edit['ldapdata_attrs'] ? unserialize($edit['ldapdata_attrs']) : array();
    $form['description'] = array(
      '#value' => t('Configure profile synchronization settings for %server.', array(
        '%server' => $edit['name'],
      )),
    );

    // Attribute mapping.
    $form['mapping'] = array(
      '#type' => 'fieldset',
      '#title' => t('Drupal-LDAP fields mapping'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['mapping']['ldapdata_mapping'] = array(
      '#type' => 'radios',
      '#title' => t('Drupal user profile field mapping'),
      '#default_value' => isset($ldapdata_mappings['access']) ? $ldapdata_mappings['access'] : LDAPDATA_MAP_ATTRIBUTES,
      '#options' => array(
        LDAPDATA_MAP_NOTHING => t('No mapping. (Clears any mappings defined below.)'),
        LDAPDATA_MAP_ATTRIBUTES_READ_ONLY => t('Read only: Drupal user profile fields have LDAP attributes.'),
        LDAPDATA_MAP_ATTRIBUTES => t('Read/write: Drupal user profile fields have LDAP attributes. LDAP attributes updated upon Drupal profile change.'),
      ),
    );
    $profile_fields = _ldapdata_retrieve_profile_fields();
    $standard_fields = _ldapdata_retrieve_standard_user_fields();
    $content_profile_fields = _ldapdata_retrieve_content_profile_fields();
    $drupal_fields = $profile_fields + $standard_fields;
    $form['mapping']['mapping_pre'] = array(
      '#value' => '<div class="form-item"><label>' . t('Specify mappings below if you selected the second or third option.') . ' </label><table><thead><tr><th> ' . t('Drupal field') . '</th><th>' . t('LDAP attribute') . '</th></tr></thead><tbody>',
    );
    $ldap_drupal_reverse_mappings = _ldapdata_reverse_mappings($sid);
    foreach ($drupal_fields as $key => $field) {
      $field_tmp = "ldap_amap-" . $key;
      $_prefix = "<tr><td><label for=\"edit[{$field_tmp}]\">{$field}</label></td><td>";
      $form['mapping'][$field_tmp] = array(
        '#type' => 'textfield',
        '#default_value' => isset($ldapdata_mappings[$field_tmp]) ? $ldapdata_mappings[$field_tmp] : NULL,
        '#size' => '20',
        '#prefix' => $_prefix,
        '#suffix' => '</td>',
      );
    }

    // Content profile mapping
    if ($content_profile_fields) {
      $form['mapping']['mapping_post'] = array(
        '#value' => '</tbody></table>',
      );
      $form['mapping']['mapping_cp'] = array(
        '#value' => t('<table><thead><tr><th> Content Profile field</th><th>LDAP attribute</th></tr></thead><tbody>'),
      );
      foreach ($content_profile_fields as $key => $field) {
        $field_tmp = "ldap_amap-" . $key;
        $_prefix = "<tr><td><label for=\"edit[{$field_tmp}]\">{$field}</label></td><td>";
        $form['mapping'][$field_tmp] = array(
          '#type' => 'textfield',
          '#default_value' => isset($ldapdata_mappings[$field_tmp]) ? $ldapdata_mappings[$field_tmp] : NULL,
          '#size' => '20',
          '#prefix' => $_prefix,
          '#suffix' => '</td>',
        );
      }
    }
    $form['mapping']['mapping_cp_post'] = array(
      '#value' => '</tbody></table></div>',
    );

    // Attribute access control.
    $form["attributes"] = array(
      '#type' => 'fieldset',
      '#title' => t('Attribute visibility & access control'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#description' => t('Allows users to view or edit their LDAP attributes.'),
      '#tree' => TRUE,
    );
    $attributes = '';
    foreach ($ldapdata_attrs as $attr => $data) {
      $attributes .= $attr . '|' . implode('|', $data) . "\n";
    }
    $form['attributes']['ldapdata_attrs'] = array(
      '#type' => 'textarea',
      '#title' => t('Attributes'),
      '#default_value' => $attributes,
      '#cols' => 25,
      '#rows' => 5,
      '#description' => t('A list of the LDAP attributes and corresponding form data. If configured, they will be listed in a table below for a more control. The element type may be \'text\' or \'url\', the form element should be \'textfield\'. Please look at the following examples:<br /><code>cn|text|textfield|Common Name|64|64</code><br /><code>homePage|url|textfield|Other web pages|64|64</code>'),
    );
    $fields = $rooptions = $rwoptions = $roattrs = $rwattrs = array();
    foreach ($ldapdata_attrs as $attr => $data) {
      $fields[$attr] = $data[2];
    }
    foreach ($fields as $attr => $attr_name) {
      $rooptions[$attr] = '';
      $rwoptions[$attr] = '';
      if (in_array($attr, $ldapdata_roattrs)) {
        $roattrs[] = $attr;
      }
      if (in_array($attr, $ldapdata_rwattrs)) {
        $rwattrs[] = $attr;
      }
      $form['attributes']['table'][$attr] = array(
        '#value' => $attr_name,
      );
    }
    $form['attributes']['ldapdata_roattrs'] = array(
      '#type' => 'checkboxes',
      '#options' => $rooptions,
      '#default_value' => $roattrs,
    );
    $form['attributes']['ldapdata_rwattrs'] = array(
      '#type' => 'checkboxes',
      '#options' => $rwoptions,
      '#default_value' => $rwattrs,
    );
    $form['attributes']['header'] = array(
      '#type' => 'value',
      '#value' => array(
        array(
          'data' => t('Attribute name'),
        ),
        array(
          'data' => t('Readable by user?'),
        ),
        array(
          'data' => t('Editable by user?'),
        ),
      ),
    );
    $form['attributes']['ldapdata_filter_php'] = array(
      '#type' => 'textarea',
      '#title' => t('PHP to filter attributes'),
      '#default_value' => $edit['ldapdata_filter_php'],
      '#cols' => 25,
      '#rows' => 5,
      '#description' => t('Enter PHP to filter LDAP attributes. Careful, bad PHP code here will break your site. If left empty, no filtering will be done. If filter is set, then attributes will be only readable. The LDAP atributes array <code>$attributes</code> is available in the code context. The code should return a filtered <code>$attributes</code> array as in example bellow:<br /><code>$attributes[\'mail\'][0] = preg_replace(\'/([^@]+@).*/\', \'$1mail.com\', $attributes[\'mail\'][0]);</code><br /><code>return $attributes;</code>'),
    );

    // Advanced configuration.
    $form['advanced'] = array(
      '#type' => 'fieldset',
      '#title' => t('Advanced configuration'),
      '#description' => t('<p>When reading/editing attributes, this module logs on to the LDAP directory using the user\'s DN/pass pair. However, many LDAP setups do not allow their users to edit attributes.</p><p>If this is your case, but still you want users to edit their LDAP attributes via Drupal, you should set up an special user on your directory, with special access to edit your users\' attributes. Then this module will use it to log on and edit data.</p>'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    if (!$edit['ldapdata_bindpw']) {
      $form['advanced']['ldapdata_binddn'] = array(
        '#type' => 'textfield',
        '#title' => t('DN for reading/editing attributes'),
        '#default_value' => $edit['ldapdata_binddn'],
        '#size' => 50,
        '#maxlength' => 255,
      );
      $form['advanced']['ldapdata_bindpw'] = array(
        '#type' => 'password',
        '#title' => t('Password for reading/editing attributes'),
        '#size' => 50,
        '#maxlength' => 255,
      );
    }
    else {
      $form['advanced']['ldapdata_binddn'] = array(
        '#type' => 'item',
        '#title' => t('DN for non-anonymous search'),
        '#value' => $edit['ldapdata_binddn'],
      );

      // Given an option to clear the password.
      $form['advanced']['ldapdata_bindpw_clear'] = array(
        '#type' => 'checkbox',
        '#default_value' => FALSE,
        '#title' => t('Clear current password and change DN'),
      );
    }
    $form['advanced']['test'] = array(
      '#type' => 'submit',
      '#value' => t('Test'),
      '#suffix' => '<div id="test-spinner" style="display: none;">' . theme_image(drupal_get_path('module', 'ldapdata') . '/images/spinner.gif') . '</div><div id="test-message" class="messages" style="display: none;"></div>',
    );
    $form['sid'] = array(
      '#type' => 'hidden',
      '#value' => $sid,
    );
    $form['buttons']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Update'),
    );
    return $form;
  }
  else {
    drupal_goto('admin/settings/ldap/ldapdata');
  }
}