From 2d106f75e8e7d0b37c74f995f4c6101f6e590424 Mon Sep 17 00:00:00 2001
From: Yuriy Gerasimov <yuri.gerasimov@gmail.com>
Date: Fri, 1 Apr 2011 22:44:12 +0300
Subject: [PATCH] Issue #1036758 by obstacle,kylebrowning,ygerasimov: Check/uncheck authentication methods on endpoint edit form.

---
 .../export_ui/services_ctools_export_ui.class.php  |   47 +++++++++++--------
 plugins/export_ui/services_ctools_export_ui.inc    |   43 +++++++-----------
 services.module                                    |   14 +++---
 services.runtime.inc                               |    2 +-
 4 files changed, 51 insertions(+), 55 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..3248c9e 100644
--- a/plugins/export_ui/services_ctools_export_ui.class.php
+++ b/plugins/export_ui/services_ctools_export_ui.class.php
@@ -22,6 +22,9 @@ class services_ctools_export_ui extends ctools_export_ui {
     drupal_set_title($this->get_page_title('authentication', $item));
     return drupal_get_form('services_edit_form_endpoint_authentication', $item);
   }
+
+  // Avoid standard submit of edit form by ctools.
+  function edit_save_form($form_state) { }
 }
 
 /**
@@ -42,30 +45,32 @@ function services_edit_form_endpoint_authentication($form_state) {
   if (empty($auth_modules)) {
     $form['message'] = array(
       '#type'          => 'item',
-      '#title'         => t('No installed authentication modules'),
+      '#title'         => t('Authentication'),
       '#description'   => t('No authentication modules are installed, standard ' .
         'Drupal session based security will be used.'),
     );
+    return $form;
   }
-  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.'),
+
+  // Add configuration fieldsets for the authentication modules
+  foreach ($endpoint->authentication as $module => $settings) {
+    $info = services_authentication_info($module);
+    if (empty($info)) {
+      continue;
+    }
+    $form[$module] = array(
+      '#type' => 'fieldset',
+      '#title' => isset($info['title']) ? $info['title'] : $module,
+      '#tree' => TRUE,
     );
-  }
-  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);
-      }
+    if ($settings) {
+      $form[$module] += services_auth_invoke($module, 'security_settings', $settings);
+    }
+    else {
+      $form[$module]['message'] = array(
+        '#type'   => 'item',
+        '#value'  => t('@module is not enabled for this endpoint', array('@module' => $module)),
+      );
     }
   }
 
@@ -80,7 +85,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..9f3d1bb 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 => $settings) {
+      if (!empty($settings)) {
+        $default_values[] = $auth_module;
+      }
+    }
     $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 ' .
@@ -172,36 +178,21 @@ function services_ctools_export_ui_form_validate(&$form, &$form_state) {
 function services_ctools_export_ui_form_submit(&$form, &$form_state) {
   $endpoint = $form_state['values']['endpoint_object'];
 
-  $endpoint->name           = $form_state['values']['name'];
-  $endpoint->title          = $form_state['values']['title'];
-  $endpoint->server         = $form_state['values']['server'];
-  $endpoint->path           = $form_state['values']['path'];
-  $endpoint->debug          = $form_state['values']['debug'];
+  $endpoint->name   = $form_state['values']['name'];
+  $endpoint->title  = $form_state['values']['title'];
+  $endpoint->server = $form_state['values']['server'];
+  $endpoint->path   = $form_state['values']['path'];
+  $endpoint->debug  = $form_state['values']['debug'];
 
   // Set the authentication modules, and preserve the settings for modules
   // that already exist.
   $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();
-      }
+    // Save settings of enabled authentication methods.
+    foreach (array_filter($form_state['values']['authentication']) as $module) {
+      $auth[$module] = $endpoint->authentication[$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);
-  }
-*/
-}
\ No newline at end of file
+  services_endpoint_save($endpoint);
+}
diff --git a/services.module b/services.module
index d9854ad..ca6a804 100644
--- a/services.module
+++ b/services.module
@@ -4,7 +4,7 @@
  * @file
  *  Provides a generic but powerful API for web services.
  */
- 
+
 /**
  * Minimum CTools version needed.
  */
@@ -199,12 +199,10 @@ function services_endpoint_load($name) {
   if (isset($result[$name])) {
     return $result[$name];
   }
-  else {
-    $result = db_query("SELECT * FROM {services_endpoint} WHERE name = '%s'", $name);
-    $matches = array();
-    while ($endpoint = db_fetch_object($result)) {
-      return $endpoint;
-    }
+  $result = db_query("SELECT * FROM {services_endpoint} WHERE name = '%s'", $name);
+  $matches = array();
+  while ($endpoint = db_fetch_object($result)) {
+    return $endpoint;
   }
   return FALSE;
 }
@@ -234,7 +232,7 @@ function services_endpoint_load_all() {
  * @return void
  */
 function services_endpoint_save($endpoint) {
-  if(is_array($endpoint) && isset($endpoint['build_info'])) {
+  if (is_array($endpoint) && isset($endpoint['build_info'])) {
     $endpoint = $endpoint['build_info']['args'][0];
   }
   ctools_export_crud_save('services_endpoint', $endpoint);
diff --git a/services.runtime.inc b/services.runtime.inc
index 7762ba5..57d1fa3 100644
--- a/services.runtime.inc
+++ b/services.runtime.inc
@@ -108,7 +108,7 @@ 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)) {
+      if ($settings && $auth_error = services_auth_invoke($auth_module, 'authenticate_call', $settings, $controller, $args, $auth_args)) {
         return services_error($auth_error, 401);
       }
     }
-- 
1.7.0.4

