From f412516c3e1c60535c7ae1e26090d82153b63397 Mon Sep 17 00:00:00 2001
From: Bradley M. Froehle <brad.froehle@gmail.com>
Date: Tue, 12 Apr 2011 18:21:13 -0700
Subject: [PATCH] Issue #1125512: Remove admin page clutter, part II.

---
 cas.admin.inc |  172 +++++++++++++++++++++++++++++----------------------------
 cas.install   |    9 +++-
 cas.module    |   15 ++---
 3 files changed, 101 insertions(+), 95 deletions(-)

diff --git a/cas.admin.inc b/cas.admin.inc
index 8b6e37e..04abd07 100644
--- a/cas.admin.inc
+++ b/cas.admin.inc
@@ -81,23 +81,6 @@ function cas_admin_settings() {
     '#description' => t('If CAS is not at the root of the host, include a URI (e.g., /cas).'),
   );
 
-  $form['server']['cas_check_first'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Check with the CAS server to see if the user is already logged in?'),
-    '#default_value' => variable_get('cas_check_first', 0),
-    '#description' => t('This implements the <a href="@cas-gateway">Gateway feature</a> of the CAS Protocol. The check is only performed the first time a user visits your site, so that the local drupal logout is still useful for site admins.', array('@cas-gateway' => 'https://wiki.jasig.org/display/CAS/gateway')),
-  );
-
-  $form['server']['cas_cert_verify'] = array(
-    '#type' => 'radios',
-    '#title' => t('CAS CA PEM certificate verification'),
-    '#default_value' => variable_get('cas_cert_verify', CAS_NO_VERIFY),
-    '#options' => array(
-      CAS_NO_VERIFY => t('Do not verify the certificate'),
-      CAS_CA_VERIFY => t('Verify the Certificate Authority using PEM certificate'),
-    ),
-  );
-
   $form['server']['cas_cert'] = array(
     '#type' => 'textfield',
     '#title' => t('CAS CA PEM Certificate'),
@@ -106,34 +89,56 @@ function cas_admin_settings() {
     '#description' => t('The PEM certificate of the Certificate Authority that issued the certificate of the CAS server.'),
   );
 
-  $form['server']['cas_proxy'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Initialize CAS as proxy'),
-    '#default_value' => variable_get('cas_proxy', 0),
-    '#description' => t('When using a proxy it is required to set the option "Require CAS login for:" to "specific pages" with "node/*" as a specific page or "all pages except specific pages" on the "Redirection settings".')
+  $form['login'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Login form settings'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
   );
 
-  $form['server']['cas_pgtformat'] = array(
+  $form['login']['cas_login_form'] = array(
     '#type' => 'radios',
-    '#title' => t('CAS PGT storage file format'),
-    '#default_value' => variable_get('cas_pgtformat', 'plain'),
-    '#options' => array('plain' => t('Plain Text'), 'xml' => t('XML')),
+    '#title' => t('Add CAS link to login forms'),
+    '#default_value' => variable_get('cas_login_form', CAS_NO_LINK),
+    '#options' => array(
+      CAS_NO_LINK => t('Do not add link to login forms'),
+      CAS_ADD_LINK => t('Add link to login forms'),
+      CAS_MAKE_DEFAULT => t('Make CAS login default on login forms')),
   );
 
-  $form['server']['cas_pgtpath'] = array(
+  $form['login']['cas_login_invite'] = array(
     '#type' => 'textfield',
-    '#title' => t('CAS PGT storage path'),
-    '#default_value' => variable_get('cas_pgtpath', ''),
-    '#size' => 30,
-    '#description' => t("Only needed if 'Use CAS proxy initializer' is configured. Leave empty for default."),
+    '#title' => t('CAS Login invitation'),
+    '#default_value' => variable_get('cas_login_invite', CAS_LOGIN_INVITE_DEFAULT),
+    '#size' => 55,
+    '#maxlength' => 80,
+    '#description' => t('Message users will see to invite them to log in with cas credentials .'),
   );
 
-  $form['server']['cas_debugfile'] = array(
+  $form['login']['cas_login_drupal_invite'] = array(
     '#type' => 'textfield',
-    '#title' => t('CAS debugging output filename'),
-    '#default_value' => variable_get('cas_debugfile', ''),
-    '#size' => 30,
-    '#description' => t("Leave empty if you don't want debugging output."),
+    '#title' => t('Drupal login invitation'),
+    '#default_value' => variable_get('cas_login_drupal_invite', CAS_LOGIN_DRUPAL_INVITE_DEFAULT),
+    '#size' => 55,
+    '#maxlength' => 80,
+    '#description' => t('Message users will see to invite them to log in with drupal credentials.'),
+  );
+
+  $form['login']['cas_login_redir_message'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Redirection notification message'),
+    '#default_value' => variable_get('cas_login_redir_message', CAS_LOGIN_REDIR_MESSAGE),
+    '#size' => 55,
+    '#maxlength' => 80,
+    '#description' => t('Message users see at the top of the cas login form to warn them that they are being redirected.'),
+  );
+
+  // Setting for message displayed to user upon successfull login
+  $form['login']['cas_login_message'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Successful login message'),
+    '#default_value' => variable_get('cas_login_message', 'Logged in via CAS as %cas_username.'),
+    '#description' => t('The message displayed to a user when he successfully logs in via CAS. You may specify \'%cas_username\', the username of the user.'),
   );
 
 
@@ -210,6 +215,13 @@ function cas_admin_settings() {
     '#collapsed' => TRUE,
   );
 
+  $form['pages']['cas_check_first'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Check with the CAS server to see if the user is already logged in?'),
+    '#default_value' => variable_get('cas_check_first', 0),
+    '#description' => t('This implements the <a href="@cas-gateway">Gateway feature</a> of the CAS Protocol. The check is only performed the first time a user visits your site, so that the local drupal logout is still useful for site admins.', array('@cas-gateway' => 'https://wiki.jasig.org/display/CAS/gateway')),
+  );
+
   $form['pages']['cas_access'] = array(
     '#type' => 'radios',
     '#title' => t('Require CAS login for'),
@@ -236,8 +248,15 @@ function cas_admin_settings() {
   );
 
 
+  $form['misc'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Miscellaneous settings'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+
   // Settings for redirection upon first login
-  $form['pages']['cas_first_login_destination'] = array(
+  $form['misc']['cas_first_login_destination'] = array(
     '#type' => 'textfield',
     '#title' => t('Initial login destination'),
     '#default_value' => variable_get('cas_first_login_destination', ''),
@@ -246,16 +265,8 @@ function cas_admin_settings() {
     '#description' => t("Drupal path or URL. Enter a destination if you want the user to be redirected to this page on their first CAS login. An example path is <em>blog</em> for the blog page, <em>&lt;front&gt;</em> for the front page, or <em>user</em> for the user's page."),
   );
 
-  // Setting for message displayed to user upon successfull login
-  $form['pages']['cas_login_message'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Successful login message'),
-    '#default_value' => variable_get('cas_login_message', 'Logged in via CAS as %cas_username.'),
-    '#description' => t('The message displayed to a user when he successfully logs in via CAS. You may specify \'%cas_username\', the username of the user.'),
-  );
-
   // Setting for page to return to after a CAS logout
-  $form['pages']['cas_logout_destination'] = array(
+  $form['misc']['cas_logout_destination'] = array(
     '#type' => 'textfield',
     '#title' => t('Logout destination'),
     '#default_value' => variable_get('cas_logout_destination', ''),
@@ -264,14 +275,6 @@ function cas_admin_settings() {
     '#description' => t("Drupal path or URL. Enter a destination if you want a user to be directed to this page after logging out of CAS, or leave blank to direct users back to the previous page. An example path is <em>blog</em> for the blog page or <em>&lt;front&gt;</em> for the front page."),
   );
 
-
-  $form['misc'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Miscellaneous settings'),
-    '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
-  );
-
   $form['misc']['cas_changePasswordURL'] = array(
     '#type' => 'textfield',
     '#title' => t('Change password URL'),
@@ -286,48 +289,49 @@ function cas_admin_settings() {
     '#description' => t('The URL users should use for changing registering.  Leave blank to use the standard Drupal page.'),
   );
 
-  $form['login'] = array(
+
+  $form['advanced'] = array(
     '#type' => 'fieldset',
-    '#title' => t('Login form settings'),
+    '#title' => t('Advanced 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 => t('Do not add link to login forms'),
-      CAS_ADD_LINK => t('Add link to login forms'),
-      CAS_MAKE_DEFAULT => t('Make CAS login default on login forms')),
+  $form['advanced']['cas_proxy'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Initialize CAS as proxy'),
+    '#default_value' => variable_get('cas_proxy', 0),
+    '#description' => t('When using a proxy it is required to set the option "Require CAS login for:" to "specific pages" with "node/*" as a specific page or "all pages except specific pages" on the "Redirection settings".')
   );
 
-  $form['login']['cas_login_invite'] = array(
-    '#type' => 'textfield',
-    '#title' => t('CAS Login invitation'),
-    '#default_value' => variable_get('cas_login_invite', CAS_LOGIN_INVITE_DEFAULT),
-    '#size' => 55,
-    '#maxlength' => 80,
-    '#description' => t('Message users will see to invite them to log in with cas credentials .'),
+  $form['advanced']['cas_proxy_settings'] = array(
+    '#type' => 'container',
+    '#states' => array(
+      'invisible' => array(
+        'input[name="cas_proxy"]' => array('checked' => FALSE),
+      ),
+    ),
+  );
+  $form['advanced']['cas_proxy_settings']['cas_pgtformat'] = array(
+    '#type' => 'radios',
+    '#title' => t('CAS PGT storage file format'),
+    '#default_value' => variable_get('cas_pgtformat', 'plain'),
+    '#options' => array('plain' => t('Plain Text'), 'xml' => t('XML')),
   );
 
-  $form['login']['cas_login_drupal_invite'] = array(
+  $form['advanced']['cas_proxy_settings']['cas_pgtpath'] = array(
     '#type' => 'textfield',
-    '#title' => t('Drupal login invitation'),
-    '#default_value' => variable_get('cas_login_drupal_invite', CAS_LOGIN_DRUPAL_INVITE_DEFAULT),
-    '#size' => 55,
-    '#maxlength' => 80,
-    '#description' => t('Message users will see to invite them to log in with drupal credentials.'),
+    '#title' => t('CAS PGT storage path'),
+    '#default_value' => variable_get('cas_pgtpath', ''),
+    '#size' => 30,
+    '#description' => t("Only needed if 'Use CAS proxy initializer' is configured. Leave empty for default."),
   );
 
-  $form['login']['cas_login_redir_message'] = array(
+  $form['advanced']['cas_debugfile'] = array(
     '#type' => 'textfield',
-    '#title' => t('Redirection notification message'),
-    '#default_value' => variable_get('cas_login_redir_message', CAS_LOGIN_REDIR_MESSAGE),
-    '#size' => 55,
-    '#maxlength' => 80,
-    '#description' => t('Message users see at the top of the cas login form to warn them that they are being redirected.'),
+    '#title' => t('CAS debugging output filename'),
+    '#default_value' => variable_get('cas_debugfile', ''),
+    '#size' => 30,
+    '#description' => t("Leave empty if you don't want debugging output."),
   );
 
   return system_settings_form($form);
diff --git a/cas.install b/cas.install
index 1e2ae33..b3d7af7 100644
--- a/cas.install
+++ b/cas.install
@@ -79,7 +79,6 @@ function cas_uninstall() {
   variable_del('cas_authmap');
   variable_del('cas_auto_assigned_role');
   variable_del('cas_cert');
-  variable_del('cas_cert_verify');
   variable_del('cas_changePasswordURL');
   variable_del('cas_check_first');
   variable_del('cas_debugfile');
@@ -107,6 +106,7 @@ function cas_uninstall() {
   variable_del('cas_version');
 
   // And old (un-used) variables.
+  variable_del('cas_cert_verify');
   variable_del('cas_first_login');
   variable_del('cas_hijack_user');
   variable_del('cas_ldap_email_attribute');
@@ -308,6 +308,13 @@ function cas_update_6303() {
   }
   variable_del('cas_logout_redirect');
 
+  // If the Certicate Authority is not being verified, ensure that the
+  // certificate field is empty.
+  if (variable_get('cas_cert_verify', 'none') == 'none') {
+    variable_set('cas_cert', '');
+  }
+  variable_del('cas_cert_verify');
+
   // Also remove the variable controlling CAS Single Sign-Out which is now
   // always enabled.
   variable_del('cas_signout');
diff --git a/cas.module b/cas.module
index e5b2a5c..feb42ed 100644
--- a/cas.module
+++ b/cas.module
@@ -7,8 +7,6 @@
  * create user accounts automatically.
  */
 
-define('CAS_NO_VERIFY', 'none');
-define('CAS_CA_VERIFY', 'ca_verify');
 define('CAS_NO_LINK', 0);
 define('CAS_ADD_LINK', 1);
 define('CAS_MAKE_DEFAULT', 2);
@@ -232,7 +230,6 @@ function cas_phpcas_init() {
   $server_cas_server = (string)variable_get('cas_server', 'sso-cas.univ-rennes1.fr');
   $server_port       = (int)variable_get('cas_port', '443');
   $server_uri        = (string)variable_get('cas_uri', '');
-  $cas_cert_verify   = (string)variable_get('cas_cert_verify', CAS_NO_VERIFY);
   $cas_cert          = (string)variable_get('cas_cert', '');
   if ( ($debugFile = variable_get("cas_debugfile", "")) != "" ) {
     phpCAS::setDebug($debugFile);
@@ -250,13 +247,11 @@ function cas_phpcas_init() {
     phpCAS::client($server_version, $server_cas_server, $server_port, $server_uri, $start_session);
   }
   // force CAS authentication
-  switch ($cas_cert_verify) {
-    case CAS_NO_VERIFY:
-      phpCAS::setNoCasServerValidation();
-      break;
-    case CAS_CA_VERIFY:
-      phpCAS::setCasServerCACert($cas_cert);
-      break;
+  if ($cas_cert = variable_get('cas_cert', '')) {
+    phpCAS::setCasServerCACert($cas_cert);
+  }
+  else {
+    phpCAS::setNoCasServerValidation();
   }
 
   $service = isset($_GET['q']) ? $_GET['q'] : 'cas';
-- 
1.7.4.1

