--- cas.module	Wed Aug 26 13:42:29 2009
+++ cas.module	Fri Sep 11 20:28:06 2009
@@ -11,6 +11,9 @@
 define('CAS_NO_VERIFY', 'none');
 define('CAS_VERIFY', 'verify');
 define('CAS_CA_VERIFY', 'ca_verify');
+define('CAS_NO_LINK',0);
+define('CAS_ADD_LINK',1);
+define('CAS_MAKE_DEFAULT',2);
 
 /**
  * Invokes hook_auth_transform() in every module.
@@ -697,6 +700,30 @@
     '#default_value' => variable_get('cas_useldap_groups', 0),
     '#description' => t('Activate this option if you want to extract the user groups from an LDAP directory. <strong>Ldapgroups module must be enabled and configured</strong>.'),
   );
+
+  $form['login'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Login form settings'),
+    '#collapsible' => true,
+    '#collapsed' => true,
+  );
+
+  $form['login']['cas_login_form'] = array(
+    '#type' => 'radios',
+    '#title' => t('Add CAS link to login forms'),
+    '#default_value' => variable_get('cas_login_form', CAS_NO_LINK),
+    '#options' => array(CAS_NO_LINK => 'Do not add link to login forms', CAS_ADD_LINK => 'Add link to login forms',
+                        CAS_MAKE_DEFAULT => 'Make CAS login default on login forms'),
+  );
+
+  $form['login']['cas_login_id'] = array(
+    '#type' => 'textfield',
+    '#title' => t('CAS username identifier'),
+    '#default_value' => variable_get('cas_login_id', 'CAS username'),
+    '#size' => 30,
+    '#maxlength' => 55,
+    '#description' => t('What you call your CAS credentials.'),
+  );
   
   return system_settings_form($form);
 }
@@ -896,6 +923,53 @@
 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;
+      }
+      else if (variable_get('cas_login_form', CAS_NO_LINK) == CAS_ADD_LINK) {
+        drupal_add_css(drupal_get_path('module', 'cas') .'/cas_link.css', 'module');
+        drupal_add_js(drupal_get_path('module', 'cas') .'/cas_link.js');
+      }
+      else {
+        drupal_add_css(drupal_get_path('module', 'cas') .'/cas_default.css', 'module');
+        drupal_add_js(drupal_get_path('module', 'cas') .'/cas_default.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('Log in using your @cas_login_id', array('@cas_login_id' => variable_get('cas_login_id', 'CAS username'))), '#'),
+        'class' => 'cas-link',
+      );
+      $items[] = array(
+        'data' => l(t('Users without a @cas_login_id click here', array('@cas_login_id' => variable_get('cas_login_id', 'CAS username'))), '#'),
+        'class' => 'uncas-link',
+      );
+      
+      $form['cas_links'] = array(
+        '#value' => theme('item_list', $items),
+        '#weight' => 1,
+      );
+
+      $form['links']['#weight'] = 2;
+
+      $form['cas_identifier'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Log in using NetID'),
+        '#default_value' => 0,
+        '#weight' => -1,
+        '#description' => t("When you click 'Log in' you will be redirected to a page where you can securely enter your %cas_login_id and password", array('%cas_login_id' => variable_get('cas_login_id', 'CAS username'))),
+      );
+      $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)) {
@@ -947,6 +1021,18 @@
       break;
   }
 }
+
+/**
+ * Login form _validate hook
+ */
+function cas_login_validate($form, &$form_state) {
+  if($form_state['values']['persistent_login'] == 1) {
+    $_SESSION['cas_remember'] = 1;
+  }
+  $_REQUEST['destination'] = 'cas';
+  drupal_redirect_form($form, 'cas');
+}
+
 /*
 ** LDAPAuth interfacing - BEGIN
 */
