You are here

function cas_form_alter in CAS 7

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. 6.2 cas.module \cas_form_alter()

Implements hook_form_alter().

Overrides specific from settings based on user policy.

File

./cas.module, line 928
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) {

  // Special handling of the user login page when the CAS login form is set to
  // redirect.
  if ($form_id == 'user_login' && variable_get('cas_login_form', CAS_NO_LINK) == CAS_REDIRECT) {
    drupal_goto('cas');
  }
  switch ($form_id) {
    case 'user_login':
    case 'user_login_block':
      if (variable_get('cas_login_form', CAS_NO_LINK) != CAS_NO_LINK) {
        $form['#attached']['css'][] = drupal_get_path('module', 'cas') . '/cas.css';
        $form['#attached']['js'][] = drupal_get_path('module', 'cas') . '/cas.js';
        if (!empty($form_state['input']['cas_identifier'])) {
          $form['name']['#required'] = FALSE;
          $form['pass']['#required'] = FALSE;
          unset($form['#validate']);
          $form['#submit'] = array(
            'cas_login_submit',
          );
        }
        $items = array();
        $items[] = array(
          'data' => l(t(variable_get('cas_login_invite', CAS_LOGIN_INVITE_DEFAULT)), '#'),
          'class' => array(
            'cas-link',
          ),
        );
        $items[] = array(
          'data' => l(t(variable_get('cas_login_drupal_invite', CAS_LOGIN_DRUPAL_INVITE_DEFAULT)), '#'),
          'class' => array(
            'uncas-link',
          ),
        );
        $form['cas_links'] = array(
          '#theme' => 'item_list',
          '#items' => $items,
          '#attributes' => array(
            'class' => array(
              'cas-links',
            ),
          ),
          '#weight' => 101,
        );
        $form['links']['#weight'] = 2;
        $form['cas_login_redirection_message'] = array(
          '#type' => 'item',
          '#markup' => t(variable_get('cas_login_redir_message', CAS_LOGIN_REDIR_MESSAGE)),
          '#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' => user_login_destination(),
        );
      }
      break;
    case 'user_profile_form':
      $account = $form['#user'];
      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',
          ),
          '#weight' => -9,
        );

        // 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),
          ));
        }
        $form['account']['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)) {
          $form['account']['mail']['#access'] = FALSE;
        }
        if (variable_get('cas_hide_password', 0)) {
          $form['account']['pass']['#access'] = FALSE;
        }
      }
      if (cas_is_external_user($account) && variable_get('cas_hide_password', 0)) {

        // Also remove requirement to validate your current password before
        // changing your e-mail address.
        $form['account']['current_pass']['#access'] = FALSE;
        $form['account']['current_pass_required_values']['#access'] = FALSE;
        $form['account']['current_pass_required_values']['#value'] = array();
        $form['#validate'] = array_diff($form['#validate'], array(
          'user_validate_current_pass',
        ));
      }
      break;
    case 'user_pass':
      if (!user_access('administer users') && variable_get('cas_changePasswordURL', '') != '') {
        drupal_goto(variable_get('cas_changePasswordURL', ''));
      }
      break;
    case 'user_register_form':
      if (user_access('administer users')) {
        $form['account']['cas_name'] = 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',
          ),
          '#weight' => -9,
        );
      }
      elseif (variable_get('cas_registerURL', '') != '') {
        drupal_goto(variable_get('cas_registerURL', ''));
      }
      break;
    case 'user_admin_account':

      // Insert the CAS username into the second column.
      _cas_array_insert($form['accounts']['#header'], 1, array(
        'cas' => array(
          'data' => 'CAS usernames',
        ),
      ));
      foreach ($form['accounts']['#options'] as $uid => &$row) {
        $cas_usernames = db_query('SELECT cas_name FROM {cas_user} WHERE uid = :uid', array(
          ':uid' => $uid,
        ))
          ->fetchCol();
        $row['cas'] = theme('item_list', array(
          'items' => $cas_usernames,
        ));
      }
      break;
  }
}