You are here

function simple_ldap_user_form_alter in Simple LDAP 7.2

Same name and namespace in other branches
  1. 7 simple_ldap_user/simple_ldap_user.module \simple_ldap_user_form_alter()

Implements hook_form_alter().

File

simple_ldap_user/simple_ldap_user.module, line 113
Main simple_ldap_user module file.

Code

function simple_ldap_user_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'user_login_block':

      // Remove the register and password reminder links.
      $server = SimpleLdapServer::singleton();
      if ($server->readonly) {
        unset($form['links']);
      }
    case 'user_login':
    case 'user_pass':

      // Insert simple_ldap_user's username validation.
      array_unshift($form['#validate'], 'simple_ldap_user_login_name_validate');
      break;
    case 'user_profile_form':

      // Disable mapped fields if LDAP Server is read-only.
      $server = SimpleLdapServer::singleton();
      $attribute_map = simple_ldap_user_variable_get('simple_ldap_user_attribute_map');
      $user_fields = simple_ldap_user_user_fields();

      // Name, Mail, and Password are special attributes.
      $form['account']['name']['#disabled'] = $server->readonly;
      $form['account']['mail']['#disabled'] = $server->readonly;
      $form['account']['pass']['#disabled'] = $server->readonly;

      // Active Directory has some additional restrictions.
      if ($server->type == 'Active Directory') {
        $form['account']['name']['#disabled'] = TRUE;
        $form['account']['pass']['#disabled'] = stripos($server->host, 'ldaps://') === FALSE;
      }

      // Other mapped fields.
      foreach ($attribute_map as $ldap_attr_name => $drupal_fields) {
        foreach ($drupal_fields as $index => $drupal_field_name) {

          // Skip #delimeter and other special entries
          if (!is_int($index)) {
            continue;
          }
          switch ($user_fields[$drupal_field_name]['module']) {
            case 'field':

              // Use Field API.
              $form[$drupal_field_name]['#disabled'] = $server->readonly;
              break;
            case 'user':

              // Use the user object directly.
              $form['account'][$drupal_field_name]['#disabled'] = $server->readonly;
              break;
            default:
              watchdog('Simple LDAP', 'Can\'t disable field from unknown module %module. (not supported yet)', array(
                '%module' => $field['module'],
              ), WATCHDOG_WARNING);
          }
        }
      }
      array_unshift($form['#validate'], 'simple_ldap_user_profile_form_validate');
      break;
    case 'user_register_form':
      array_unshift($form['#validate'], 'simple_ldap_user_register_form_validate');
      break;
    default:
  }
}