You are here

function cas_form_alter in CAS 6.3

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 cas.module \cas_form_alter()
  5. 6.2 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 837
Enables users to authenticate via a Central Authentication Service (CAS) Cas will currently work if the auto registration is turned on and will create user accounts automatically.

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) {
        drupal_add_css(drupal_get_path('module', 'cas') . '/cas.css', 'module');
        drupal_add_js(drupal_get_path('module', 'cas') . '/cas.js');
        if (!empty($form_state['post']['cas_identifier'])) {
          $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_MAKE_DEFAULT,
          '#weight' => -1,
          '#description' => t(variable_get('cas_login_redir_message', CAS_LOGIN_REDIR_MESSAGE)),
        );
        $form['cas.return_to'] = array(
          '#type' => 'hidden',
          '#value' => cas_login_destination(),
        );
      }
      break;
    case 'user_profile_form':

      // We alter the 'account' tab only, not any other tab provided by the
      // profile module.
      if ($form['_category']['#value'] == 'account') {
        $account = user_load($form['#uid']);
        if (user_access('administer users')) {

          // The user is an administrator, so add fields to allow changing the
          // CAS username(s) associated with the account.
          $cas_names = $account->cas_names;
          $aids = array_keys($cas_names);
          $element = array(
            '#type' => 'textfield',
            '#title' => t('CAS username'),
            '#default_value' => array_shift($cas_names),
            '#cas_user_aid' => array_shift($aids),
            '#description' => t('<a href="@url">Create, edit or delete</a> additional CAS usernames associated with this account.', array(
              '@url' => url('user/' . $account->uid . '/cas'),
            )),
            '#element_validate' => array(
              '_cas_name_element_validate',
            ),
          );

          // See if any additional CAS usernames exist.
          if (!empty($cas_names)) {
            $element['#description'] .= ' <br />' . t('Other CAS usernames: %cas_names.', array(
              '%cas_names' => implode(', ', $cas_names),
            ));
          }

          // Position the element directly below the 'name' field.
          $position = array_search('name', array_keys($form['account'])) + 1;
          _cas_array_insert($form['account'], $position, array(
            'cas_name' => $element,
          ));
        }
        elseif (cas_is_external_user($account)) {

          // The user is not an administrator, so selectively remove the e-mail
          // and password fields.
          if (variable_get('cas_hide_email', 0)) {

            // We cannot just set #access to FALSE, as the form throws an error if the current
            // value would not pass validation.
            $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']['#value']->name . '@' . variable_get('cas_domain', '');
            }
          }
          if (variable_get('cas_hide_password', 0)) {
            $form['account']['pass']['#access'] = FALSE;
          }
        }
      }
      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')) {
        $element = array(
          '#type' => 'textfield',
          '#title' => t('CAS username'),
          '#default_value' => '',
          '#description' => t('If necessary, additional CAS usernames can be added after the account is created.'),
          '#element_validate' => array(
            '_cas_name_element_validate',
          ),
        );

        // Position the element directly below the 'name' field.
        $position = array_search('name', array_keys($form)) + 1;
        _cas_array_insert($form, $position, array(
          'cas_name' => $element,
        ));
      }
      elseif (variable_get('cas_registerURL', '') != '') {
        drupal_goto(variable_get('cas_registerURL', ''));
      }
      break;
    case 'user_admin_account':
      foreach (array_keys($form['name']) as $uid) {
        $result = db_query('SELECT cas_name FROM {cas_user} WHERE uid = %d', $uid);
        $cas_names = array();
        while ($record = db_fetch_object($result)) {
          $cas_names[] = $record->cas_name;
        }
        if ($cas_names) {
          $form['name'][$uid]['#value'] .= ' ' . check_plain('(' . implode(', ', $cas_names) . ')');
        }
      }
      break;
  }
}