Hello,

I made a patch for the https://fedorahosted.org/freeipa/ticket/5432 ticket. All Issue new certificate dialogs now validates input data.


--
Pavel Vomacka
>From 2d323f8cf997af594de01405b9d242360f625b86 Mon Sep 17 00:00:00 2001
From: Pavel Vomacka <pvoma...@redhat.com>
Date: Thu, 28 Jan 2016 12:07:26 +0100
Subject: [PATCH] Add validation to Issue new certificate dialog

'Issue new certificate' dialog now validates whether user fills 'principal' and 'csr' field.
In case that one of these fields is empty then it does not allow to submit the dialog.

https://fedorahosted.org/freeipa/ticket/5432
---
 install/ui/src/freeipa/certificate.js | 95 +++++++++++++++++++++++++++++------
 install/ui/src/freeipa/details.js     |  5 ++
 2 files changed, 84 insertions(+), 16 deletions(-)

diff --git a/install/ui/src/freeipa/certificate.js b/install/ui/src/freeipa/certificate.js
index 93f3cfc68a95bfb8014aaf96d1b571568ac605dc..dbdad98e07e239339cab28548d30493c996f8da6 100755
--- a/install/ui/src/freeipa/certificate.js
+++ b/install/ui/src/freeipa/certificate.js
@@ -30,10 +30,11 @@ define([
     './reg',
     './rpc',
     './text',
+    './widget',
     './dialog'],
     function(
         lang, builder, metadata_provider, IPA, $, menu,
-        phases, reg, rpc, text) {
+        phases, reg, rpc, text, widget_mod) {
 
 var exp = IPA.cert = {};
 
@@ -399,11 +400,49 @@ IPA.cert.request_dialog = function(spec) {
     spec = spec || {};
 
     spec.sections = spec.sections || [];
-    var section = { fields: [] };
-    spec.sections.push(section);
+    var section0 = { fields: [] };
+    var section_message = {
+        show_header: false,
+        fields: [
+            {
+                field: false, // the section does not contain any field.
+                $type: 'html',
+                name: 'message_html_widget',
+                html: spec.message
+            }
+        ],
+        layout:
+        {
+            $factory: widget_mod.fluid_layout,
+            widget_cls : "col-sm-12 controls",
+            label_cls : "hide"
+        }
+    };
+
+    var section_csr = {
+        show_header: false,
+        fields:
+        [
+            {
+                $type: 'textarea',
+                name: 'textarea_cert',
+                required: true
+            }
+        ],
+        layout:
+        {
+            $factory: widget_mod.fluid_layout,
+            widget_cls : "col-sm-12 controls",
+            label_cls : "hide"
+        }
+    };
+
+    spec.sections.push(section0);
+    spec.sections.push(section_message);
+    spec.sections.push(section_csr);
 
     if (spec.show_principal) {
-        section.fields.push(
+        section0.fields.push(
             {
                 $type: 'text',
                 name: 'principal',
@@ -418,7 +457,7 @@ IPA.cert.request_dialog = function(spec) {
             }
         );
     }
-    section.fields.push(
+    section0.fields.push(
         {
             $type: 'entity_select',
             name: 'profile_id',
@@ -443,7 +482,26 @@ IPA.cert.request_dialog = function(spec) {
         click: function() {
             var values = {};
             that.save(values);
-            var request = $.trim(that.textarea.val());
+            var csr_textarea_format = that.get_field('textarea_cert');
+
+            // check requested fields
+            if (!that.validate()) {
+                var dialog_fields = that.fields.get_fields();
+
+                for (var i=0; i<dialog_fields.length; i++) {
+                    var current_field = dialog_fields[i];
+
+                    if (!current_field.valid) {
+                        current_field.widget.focus_input();
+                        break;
+                    }
+                }
+
+                return;
+            }
+
+            // get csr from textarea
+            var request = $.trim(that.get_field('textarea_cert').get_value());
             values.request = IPA.cert.pem_csr_format(request);
 
             if (that.request) {
@@ -462,15 +520,20 @@ IPA.cert.request_dialog = function(spec) {
     });
 
     that.create_content = function() {
-        that.dialog_create_content();
-        var node = $("<div/>", {
-            'class': 'col-sm-12'
-        });
-        node.append(that.message);
-        that.textarea = $('<textarea/>', {
-            'class': 'certificate'
-        }).appendTo(node);
-        that.body_node.append(node);
+        that.body_node.empty();
+        var widgets = that.widgets.get_widgets();
+
+        for(var i=0; i<widgets.length; i++) {
+            var widget = widgets[i];
+
+            var div = $('<div/>', {
+                name: widget.name,
+                'class': 'dialog-section'
+            }).appendTo(that.body_node);
+
+            widget.create(div);
+        }
+
         return that.body_node;
     };
 
@@ -1519,4 +1582,4 @@ phases.on('post-metadata', exp.create_cert_metadata);
 phases.on('profile', exp.remove_menu_item, 20);
 
 return exp;
-});
\ No newline at end of file
+});
diff --git a/install/ui/src/freeipa/details.js b/install/ui/src/freeipa/details.js
index c708a878b7d6d29815535b7508da0c4cc30a3b5c..36dbb99fca3440b29ba280283c6c9e098f67d6d6 100644
--- a/install/ui/src/freeipa/details.js
+++ b/install/ui/src/freeipa/details.js
@@ -295,6 +295,11 @@ exp.section_builder = IPA.section_builder = function(spec) {
 
         var widget = that.widget_builder.build_widget(field_spec, section.widgets);
 
+        if (field_spec.field === false) {
+            // widget doesn't have field, skip
+            return;
+        }
+
         if (section.$field_adapter && !field_spec.adapter) {
             field_spec.adapter = section.$field_adapter;
         }
-- 
2.5.0

-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to