You are here

function ldapauth_admin_form_submit in LDAP integration 6

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

Submit hook for the LDAP server form.

File

./ldapauth.admin.inc, line 456
Module admin page callbacks.

Code

function ldapauth_admin_form_submit($form, &$form_state) {
  $op = $form_state['clicked_button']['#value'];
  $values = $form_state['values'];
  switch ($op) {
    case t('Save configuration'):
      $server = array(
        'name' => $values['name'],
        'machine_name' => $values['machine_name'],
        'status' => 1,
        'server' => $values['server'],
        'port' => $values['port'],
        'tls' => $values['tls'],
        'enc_type' => $values['enc_type'],
        'basedn' => $values['basedn'],
        'user_attr' => trim($values['user_attr']),
        'mail_attr' => trim($values['mail_attr']),
        'puid_attr' => trim($values['puid_attr']),
        'binary_puid' => trim($values['binary_puid']),
        'binddn' => $values['binddn'],
        'bindpw' => $values['bindpw'],
        'login_php' => trim($values['login_php']),
        'filter_php' => trim($values['filter_php']),
      );
      if (isset($values['sid'])) {
        $old_server = $values['original_server'];
        $server['sid'] = $values['sid'];

        // If machine name has changed, update ldapauth_users values.
        if ($old_server->machine_name != $server['machine_name']) {
          db_query("UPDATE {ldapauth_users SET machine_name='%s' WHERE sid = %d", $server['machine_name'], $server['sid']);
        }

        // If puid attr has changed, clear out old values.
        if ($old_server->puid_attr != $server['puid_attr']) {
          ldapauth_userinfo_delete_by_sid($server['sid']);
          drupal_set_message(t("NOTICE: The PUID info for current users were cleared because PUID Attribute changed.  You can either let this information be rebuild as users log in or use ldapsync to rebuild."), 'warning');
        }
      }
      if (!empty($values['bindpw_clear'])) {
        $server['bindpw'] = '';
      }
      ldapauth_server_save($server, FALSE, TRUE);
      $form_state['redirect'] = 'admin/settings/ldap/ldapauth/list';
      break;
    case t('Test'):
      global $_ldapauth_ldap;
      if (isset($values['sid']) && _ldapauth_init($values['sid'])) {

        // Try to authenticate.
        $dn = $_ldapauth_ldap
          ->getOption('binddn');
        $pass = $_ldapauth_ldap
          ->getOption('bindpw');
        if (!$_ldapauth_ldap
          ->connect($dn, $pass)) {
          drupal_set_message(t('Authentication with the LDAP server for the dn %dn and saved password failed.', array(
            '%dn' => $dn,
          )), 'error');
        }
        else {
          drupal_set_message(t('Authentication with the LDAP server for the dn %dn and saved password succeeded.', array(
            '%dn' => $dn,
          )));
        }
      }
      else {
        drupal_set_message(t('Cannot load server settings. Please save configuration first.'), 'error');
      }
      break;
  }
}