From 44364f3b6cdd6b02218bad12f97b99c11172e76d Mon Sep 17 00:00:00 2001
From: Kyle Browning <kylebrowning@me.com>
Date: Wed, 30 Mar 2011 16:58:03 -0700
Subject: [PATCH] FIxes authentication removal

---
 .../export_ui/services_ctools_export_ui.class.php  |   31 +++++++++---------
 plugins/export_ui/services_ctools_export_ui.inc    |   34 +++++++++----------
 services.runtime.inc                               |    6 ++-
 3 files changed, 36 insertions(+), 35 deletions(-)

diff --git a/plugins/export_ui/services_ctools_export_ui.class.php b/plugins/export_ui/services_ctools_export_ui.class.php
index 680ef8a..2e961c2 100644
--- a/plugins/export_ui/services_ctools_export_ui.class.php
+++ b/plugins/export_ui/services_ctools_export_ui.class.php
@@ -47,24 +47,23 @@ function services_edit_form_endpoint_authentication($form_state) {
         'Drupal session based security will be used.'),
     );
   }
-  elseif (empty($endpoint->authentication)) {
-    $form['message'] = array(
-      '#type'          => 'item',
-      '#title'         => t('No enabled authentication modules'),
-      '#value'   => t('No authentication modules are enabled, standard ' .
-        'Drupal session based security will be used.'),
-    );
-  }
   else {
     // Add configuration fieldsets for the authentication modules
     foreach ($endpoint->authentication as $module => $settings) {
       $info = services_authentication_info($module);
-      if ($info) {
-        $form[$module] = array(
-          '#type' => 'fieldset',
-          '#title' => $info['title'],
-          '#tree' => TRUE,
-        ) + services_auth_invoke($module, 'security_settings', $settings);
+      $form[$module] = array(
+        '#type' => 'fieldset',
+        '#title' => $info['title'],
+        '#tree' => TRUE,
+      );
+      if ($info && $settings) {
+        $form[$module] += services_auth_invoke($module, 'security_settings', $settings);
+      }
+      if(!$settings) {
+        $form[$module]['message'] = array(
+          '#type'          => 'item',
+          '#value'         => t('@module is not enabled for this endpoint', array('@module' => $module)),
+        );
       }
     }
   }
@@ -80,7 +79,9 @@ function services_edit_form_endpoint_authentication($form_state) {
 function services_edit_form_endpoint_authentication_submit($form, $form_state) {
   $endpoint = $form_state['values']['endpoint_object'];
   foreach (array_keys($endpoint->authentication) as $module) {
-    $endpoint->authentication[$module] = $form_state['values'][$module];
+    if(isset($form_state['values'][$module])) {
+      $endpoint->authentication[$module] = $form_state['values'][$module];
+    }
   }
 
   drupal_set_message(t('Your authentication options have been saved.'));
diff --git a/plugins/export_ui/services_ctools_export_ui.inc b/plugins/export_ui/services_ctools_export_ui.inc
index 748a7ee..ab350a0 100644
--- a/plugins/export_ui/services_ctools_export_ui.inc
+++ b/plugins/export_ui/services_ctools_export_ui.inc
@@ -121,10 +121,16 @@ function services_ctools_export_ui_form(&$form, &$form_state) {
       $auth_options[$module] = $info['title'];
     }
 
+    $default_values = array();
+    foreach($endpoint->authentication as $auth_module => $enabled) {
+      if($auth_module == $enabled) {
+        $default_values[] = $enabled;
+      }
+    }
     $form['authentication'] = array(
       '#type'          => 'checkboxes',
       '#options'       => $auth_options,
-      '#default_value' => array_keys($endpoint->authentication),
+      '#default_value' => $default_values,
       '#title'         => t('Authentication'),
       '#description'   => t('Choose which authentication schemes that should ' .
         'be used with your endpoint. If no authentication method is selected ' .
@@ -183,25 +189,17 @@ function services_ctools_export_ui_form_submit(&$form, &$form_state) {
   $auth = array();
   if (isset($form_state['values']['authentication'])) {
     foreach (array_keys($form_state['values']['authentication']) as $module) {
-      if (isset($endpoint->authentication[$module])) {
-        $auth[$module] = $endpoint->authentication[$module];
-      }
-      else {
-        $auth[$module] = array();
+      //if module's checkbox is checked, add to empty
+      if ($module == $form_state['values']['authentication'][$module]) {
+        if ($form_state['values']['authentication'][$module] != 0) {
+          $auth[$module] = $endpoint->authentication[$module];
+        } 
+        else{
+          unset($auth[$module]);
+        }
       }
     }
   }
   $endpoint->authentication = $auth;
-
-/*
-  if (empty($endpoint->eid)) {
-    drupal_set_message(t('Your new endpoint %title has been saved.', array('%title' => $endpoint->title)));
-    services_endpoint_save($endpoint);
-    $form_state['values']['eid'] = $endpoint->eid;
-  }
-  else {
-    drupal_set_message(t('Your changes have been saved.'));
-    services_endpoint_save($endpoint);
-  }
-*/
+  services_endpoint_save($endpoint);
 }
\ No newline at end of file
diff --git a/services.runtime.inc b/services.runtime.inc
index 7762ba5..e8e5992 100644
--- a/services.runtime.inc
+++ b/services.runtime.inc
@@ -108,8 +108,10 @@ function services_controller_execute($controller, $args = array(), $auth_args =
     $endpoint = services_endpoint_load($endpoint_name);
 
     foreach ($endpoint->authentication as $auth_module => $settings) {
-      if ($auth_error = services_auth_invoke($auth_module, 'authenticate_call', $settings, $controller, $args, $auth_args)) {
-        return services_error($auth_error, 401);
+      if($settings) {
+        if ($auth_error = services_auth_invoke($auth_module, 'authenticate_call', $settings, $controller, $args, $auth_args)) {
+          return services_error($auth_error, 401);
+        }
       }
     }
   }
-- 
1.7.3.4

