You are here

function cas_form_alter in CAS 6.2

Same name and namespace in other branches
  1. 5.4 cas.module \cas_form_alter()
  2. 5 cas.module \cas_form_alter()
  3. 5.3 cas.module \cas_form_alter()
  4. 6.3 cas.module \cas_form_alter()
  5. 6 cas.module \cas_form_alter()
  6. 7 cas.module \cas_form_alter()

implementation of hook_form_alter Overrides specific from settings based on user policy.

File

./cas.module, line 1088

Code

function cas_form_alter(&$form, $form_state, $form_id) {

  //drupal_set_message($form_id.'<pre>'.print_r($form,1).'</pre>');
  switch ($form_id) {
    case 'user_login':
    case 'user_login_block':
      if (variable_get('cas_login_form', CAS_NO_LINK) == CAS_NO_LINK) {
        break;
      }
      drupal_add_css(drupal_get_path('module', 'cas') . '/cas.css', 'module');
      drupal_add_js(drupal_get_path('module', 'cas') . '/cas.js');
      if ($form_state['post']['cas_identifier'] == 1) {
        $form['name']['#required'] = FALSE;
        $form['pass']['#required'] = FALSE;
        unset($form['#submit']);
        $form['#validate'] = array(
          'cas_login_validate',
        );
      }
      $items = array();
      $items[] = array(
        'data' => l(t(variable_get('cas_login_invite', CAS_LOGIN_INVITE_DEFAULT)), '', array(
          'fragment' => ' ',
          'external' => TRUE,
        )),
        'class' => 'cas-link',
      );
      $items[] = array(
        'data' => l(t(variable_get('cas_login_drupal_invite', CAS_LOGIN_DRUPAL_INVITE_DEFAULT)), '', array(
          'fragment' => ' ',
          'external' => TRUE,
        )),
        'class' => 'uncas-link',
      );
      $form['cas_links'] = array(
        '#value' => theme('item_list', $items),
        '#weight' => 1,
      );
      $form['links']['#weight'] = 2;
      $form['cas_login_redirection_message'] = array(
        '#value' => '<div class="form-item cas-login-redirection-message">' . t(variable_get('cas_login_redir_message', CAS_LOGIN_REDIR_MESSAGE)) . '</div>',
        '#weight' => -1,
      );
      $form['cas_identifier'] = array(
        '#type' => 'checkbox',
        '#title' => t(variable_get('cas_login_invite', CAS_LOGIN_INVITE_DEFAULT)),
        '#default_value' => variable_get('cas_login_form', CAS_NO_LINK) == CAS_ADD_LINK ? 0 : 1,
        '#weight' => -1,
        '#description' => t(variable_get('cas_login_redir_message', CAS_LOGIN_REDIR_MESSAGE)),
      );
      $form['cas.return_to'] = array(
        '#type' => 'hidden',
        '#value' => 'cas',
      );
      break;
    case 'user_profile_form':

      //make the email field hidden and force the value to the default.
      if (variable_get('cas_hide_email', 0)) {
        if (variable_get('cas_domain', '')) {
          $form['account']['mail']['#type'] = 'hidden';
          $form['account']['mail']['#value'] = $form['account']['mail']['#default_value'];
          if (!$form['account']['mail']['#default_value']) {
            $form['account']['mail']['#value'] = $form['account']['name']['#default_value'] . '@' . variable_get('cas_domain', '');
          }
        }

        /*
         ** LDAPAuth interfacing - BEGIN
         */
        if (variable_get('cas_useldap', '')) {
          global $ldapauth_ldap, $user;
          if ($ldap_config_name = _get_ldap_config_name($user->name)) {
            _ldapauth_init($ldap_config_name);
            _ldapauth_user_lookup($user->name);
            $cas_ldap_email_attribute = (string) variable_get('cas_ldap_email_attribute', 'mail');
            $ldap_entries = $ldapauth_ldap
              ->search($ldapauth_ldap
              ->getOption('basedn'), $ldapauth_ldap
              ->getOption('user_attr') . '=' . $user->name, array(
              $cas_ldap_email_attribute,
            ));
            if ($ldap_entries['count'] == 1 && isset($ldap_entries[0][$cas_ldap_email_attribute][0])) {
              if (trim($ldap_entries[0][$cas_ldap_email_attribute][0]) != '') {
                $form['account']['mail']['#type'] = 'hidden';
                $form['account']['mail']['#value'] = $ldap_entries[0][$cas_ldap_email_attribute][0];
              }
            }
          }
        }

        /*
         ** LDAPAuth interfacing - END
         */
      }

      //Remove the password fields from the form.
      if (variable_get('cas_hide_password', 0)) {
        unset($form['account']['pass']);
      }
      break;
    case 'user_pass':
      if (!user_access('administer users') && variable_get('cas_changePasswordURL', '') != '') {
        drupal_goto(variable_get('cas_changePasswordURL', ''));
      }
      break;
    case 'user_register':
      if (!user_access('administer users') && variable_get('cas_registerURL', '') != '') {
        drupal_goto(variable_get('cas_registerURL', ''));
      }
      break;
  }
}