URL: https://github.com/freeipa/freeipa/pull/549
Author: pvomacka
 Title: #549: WebUI: certmap match
Action: synchronized

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/549/head:pr549
git checkout pr549
From 230fcbb463266a957da60bffff28ee4251361027 Mon Sep 17 00:00:00 2001
From: Pavel Vomacka <pvoma...@redhat.com>
Date: Tue, 7 Mar 2017 21:28:32 +0100
Subject: [PATCH 1/4] WebUI: Add possibility to turn of autoload when
 details.load is called

When field on details facet has set 'autoload_value' to false, then it won't
be loaded using that.load method of details facet. That means that field
might stay unchanged even that loading of data was performed.

Part of: https://pagure.io/freeipa/issue/6601
---
 install/ui/src/freeipa/details.js | 3 ++-
 install/ui/src/freeipa/field.js   | 8 ++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/install/ui/src/freeipa/details.js b/install/ui/src/freeipa/details.js
index 9f0e632..87b355a 100644
--- a/install/ui/src/freeipa/details.js
+++ b/install/ui/src/freeipa/details.js
@@ -743,7 +743,8 @@ exp.details_facet = IPA.details_facet = function(spec, no_init) {
         var fields = that.fields.get_fields();
         for (var i=0; i<fields.length; i++) {
             var field = fields[i];
-            field.load(data);
+
+            if (field.autoload_value) field.load(data);
         }
         that.policies.post_load(data);
         that.post_load.notify([data], that);
diff --git a/install/ui/src/freeipa/field.js b/install/ui/src/freeipa/field.js
index 9f287dd..4a63242 100644
--- a/install/ui/src/freeipa/field.js
+++ b/install/ui/src/freeipa/field.js
@@ -196,6 +196,14 @@ field.field = IPA.field = function(spec) {
     that.required = spec.required;
 
     /**
+     * Turns off loading value from command output on details pages.
+     * Used in certmap_match.
+     * @property {boolean}
+     */
+    that.autoload_value = spec.autoload_value === undefined ? true :
+                                spec.autoload_value;
+
+    /**
      * read_only is set when widget is created
      * @readonly
      * @property {boolean}

From 82aae381d873a4fe3bebd50213f546276afe22ec Mon Sep 17 00:00:00 2001
From: Pavel Vomacka <pvoma...@redhat.com>
Date: Tue, 7 Mar 2017 21:30:00 +0100
Subject: [PATCH 2/4] WebUI: Possibility to choose object when API call returns
 list of objects

In case that API call returns array of objects which contains data, using
'object_index' attribute in adapter specification we can set which object
should be used.

It is possible to choose only one object specified by its index in array.

Part of: https://pagure.io/freeipa/issue/6601
---
 install/ui/src/freeipa/field.js | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/install/ui/src/freeipa/field.js b/install/ui/src/freeipa/field.js
index 4a63242..3b6b97b 100644
--- a/install/ui/src/freeipa/field.js
+++ b/install/ui/src/freeipa/field.js
@@ -819,6 +819,15 @@ field.Adapter = declare(null, {
     result_index: 0,
 
     /**
+     * When result of API call is an array of object this object index
+     * allows to specify exact object in array according to its position.
+     * Default value is null which means do not use object_index.
+     *
+     * @type {Number|null}
+     */
+     object_index: null,
+
+    /**
      * Name of the record which we want to extract from the result.
      * Used in dnslocations.
      * @type {String}
@@ -849,6 +858,10 @@ field.Adapter = declare(null, {
             else if (dr.results) {
                 var result = dr.results[this.result_index];
                 if (result) record = result[this.result_name];
+                var res_type = typeof record;
+                var obj_in_type = typeof this.object_index;
+                if (res_type === 'object' && obj_in_type === 'number')
+                    record = record[this.object_index];
             }
         }
         return record;

From 7d30594f88572970ee3428234af9a49a5397b10f Mon Sep 17 00:00:00 2001
From: Pavel Vomacka <pvoma...@redhat.com>
Date: Tue, 7 Mar 2017 21:30:45 +0100
Subject: [PATCH 3/4] WebUI: Add Adapter for certmap_match result table

Result of certmap_match command is in the following format:
[{domain: 'domain1', uid:[uid11,uid12,uid13]}, {domain: 'domain2',
uid:[uid21, uid22, uid23},...]

For correct displaying in table we need to reformat it to the following:
[{domain: 'domain1', uid: 'uid11'}, {domain: 'domain1', uid: 'uid12'},...

This can be done using this Adapter.

Part of: https://pagure.io/freeipa/issue/6601
---
 install/ui/src/freeipa/field.js | 79 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)

diff --git a/install/ui/src/freeipa/field.js b/install/ui/src/freeipa/field.js
index 3b6b97b..dde2837 100644
--- a/install/ui/src/freeipa/field.js
+++ b/install/ui/src/freeipa/field.js
@@ -1462,6 +1462,84 @@ field.AlternateAttrFieldAdapter = declare([field.Adapter], {
 
 
 /**
+ * Custom adapter specifically implemented for certmap_match where it
+ * transform items in format {domain: "xxx", uid: [arrayof_uids]} to
+ * {[{domain: "xxx", uid: "uid1"}, {domain: "xxx", uid: 'uid2'}, ...]}.
+ * This is necessary for possibility to correctly display table.
+ *
+ * @class
+ * @extends field.Adapter
+ */
+field.CertMatchTransformAdapter = declare([field.Adapter], {
+
+    /**
+    * @param {Array} record
+    */
+    transform_one_record: function(record) {
+        var domain = record.domain;
+        var uids = record.uid;
+        var results = [];
+
+        for (var i=0, l=uids.length; i<l; i++) {
+            results.push({
+                domain: domain,
+                uid: uids[i]
+            });
+        }
+
+        return results;
+    },
+
+    /**
+     * Transform record to array of arrays with objects in the following format:
+     * {domain: 'xxx', uid: 'uid1'}
+     *
+     * @param {Array|Object} record
+     */
+    transform_record: function(record) {
+        if (lang.isArray(record)) {
+            for (var i=0, l=record.length; i<l; i++) {
+                record[i] = this.transform_one_record(record[i]);
+            }
+        } else {
+            record = this.transform_one_record(record);
+        }
+    },
+
+    /**
+     * Merge array of arrays of object into array of objects.
+     *
+     * @param {Array} records
+     */
+    merge_object_into_array: function(records) {
+        if (!lang.isArray(records)) return records;
+
+        var merged = [];
+        for (var i=0, l=records.length; i<l; i++) {
+            merged = merged.concat(records[i]);
+        }
+
+        return merged;
+    },
+
+    /**
+     *
+     * @param {Object} data Object which contains the record or the record
+     * @returns {Array} attribute values
+     */
+    load: function(data) {
+        var record = this.get_record(data);
+
+        this.transform_record(record);
+
+        var values = this.merge_object_into_array(record);
+
+        return values;
+    }
+});
+
+
+/**
  * Field for enabling/disabling entity
  *
  * - expects radio widget
@@ -1735,6 +1813,7 @@ field.register = function() {
     l.register('adapter', field.Adapter);
     l.register('object_adapter', field.ObjectAdapter);
     l.register('alternate_attr_field_adapter', field.AlternateAttrFieldAdapter);
+    l.register('certmatch_transform', field.CertMatchTransformAdapter);
 };
 phases.on('registration', field.register);
 

From 08ce78818be0d65961c63744b4356206d7306966 Mon Sep 17 00:00:00 2001
From: Pavel Vomacka <pvoma...@redhat.com>
Date: Tue, 7 Mar 2017 21:31:22 +0100
Subject: [PATCH 4/4] WebUI: Add cermapmatch module

Add module which can show users which are mapped to the provided certificate.
Additionaly, the certificate is parsed and parsed information are
also displayed.

https://pagure.io/freeipa/issue/6601
---
 install/ui/src/freeipa/app.js                  |   1 +
 install/ui/src/freeipa/navigation/menu_spec.js |   4 +
 install/ui/src/freeipa/plugins/certmap.js      |   3 +-
 install/ui/src/freeipa/plugins/certmapmatch.js | 387 +++++++++++++++++++++++++
 ipaserver/plugins/internal.py                  |  12 +
 5 files changed, 406 insertions(+), 1 deletion(-)
 create mode 100644 install/ui/src/freeipa/plugins/certmapmatch.js

diff --git a/install/ui/src/freeipa/app.js b/install/ui/src/freeipa/app.js
index d262a64..5e70dba 100644
--- a/install/ui/src/freeipa/app.js
+++ b/install/ui/src/freeipa/app.js
@@ -33,6 +33,7 @@ define([
     './plugins/caacl',
     './plugins/certprofile',
     './plugins/certmap',
+    './plugins/certmapmatch',
     './dns',
     './group',
     './hbac',
diff --git a/install/ui/src/freeipa/navigation/menu_spec.js b/install/ui/src/freeipa/navigation/menu_spec.js
index 5f1d388..8d13da1 100644
--- a/install/ui/src/freeipa/navigation/menu_spec.js
+++ b/install/ui/src/freeipa/navigation/menu_spec.js
@@ -178,6 +178,10 @@ var nav = {};
                         {
                             entity: 'certmapconfig',
                             facet: 'details'
+                        },
+                        {
+                            label: '@i18n:objects.certmap_match.facet_label',
+                            entity: 'certmap_match'
                         }
                     ]
                 }
diff --git a/install/ui/src/freeipa/plugins/certmap.js b/install/ui/src/freeipa/plugins/certmap.js
index ddbc5a7..ecbe095 100644
--- a/install/ui/src/freeipa/plugins/certmap.js
+++ b/install/ui/src/freeipa/plugins/certmap.js
@@ -30,7 +30,8 @@ var certmap = IPA.certmap = {
     search_facet_group: {
         facets: {
             certmaprule_search: 'certmaprule_search',
-            certmapconfig: 'certmapconfig_details'
+            certmapconfig: 'certmapconfig_details',
+            certmapmatch: 'certmapmatch_details'
         }
     }
 };
diff --git a/install/ui/src/freeipa/plugins/certmapmatch.js b/install/ui/src/freeipa/plugins/certmapmatch.js
new file mode 100644
index 0000000..0791c53
--- /dev/null
+++ b/install/ui/src/freeipa/plugins/certmapmatch.js
@@ -0,0 +1,387 @@
+//
+// Copyright (C) 2017  FreeIPA Contributors see COPYING for license
+//
+
+define([
+        'dojo/_base/lang',
+        'dojo/_base/declare',
+        'dojo/Evented',
+        'dojo/on',
+        '../metadata',
+        '../ipa',
+        '../phases',
+        '../reg',
+        '../rpc',
+        '../widget',
+        '../util',
+        // plain imports
+        '../search',
+        '../entity'],
+            function(lang, declare, Evented, on, metadata_provider, IPA, phases,
+                    reg, rpc, widget_mod, util ) {
+
+var certmapmatch = IPA.certmapmatch = {};
+
+var make_certmap_spec = function() {
+return {
+    name: 'certmap_match',
+    facets: [
+        {
+
+            $factory: certmapmatch.details_certmapmatch_facet,
+            disable_breadcrumb: true,
+            no_update: true,
+            name: 'cert',
+            label: "@i18n:objects.certmap_match.facet_label",
+            actions: [
+                'match', 'clear'
+            ],
+            control_buttons: [
+                {
+                    name: 'match',
+                    title: '@i18n:buttons.match_title',
+                    label: '@i18n:buttons.match',
+                    icon: 'fa-gear'
+                },
+                {
+                    name: 'clear',
+                    title: '@i18n:buttons.clear_title',
+                    label: '@i18n:buttons.clear',
+                    icon: 'fa-refresh'
+                }
+            ],
+            sections: [
+                {
+                    name: 'cert_input',
+                    label: '@i18n:objects.certmap_match.cert_for_match',
+                    fields: [
+                        {
+                            $type: 'cert_textarea',
+                            name: 'cert_textarea',
+                            label: '@i18n:objects.cert.certificate',
+                            autoload_value: false,
+                            undo: false,
+                            rows: 20,
+                            cols: 70
+                        }
+                    ]
+                },
+                {
+                    name: 'parsed_cert',
+                    label: '@i18n:objects.certmap_match.cert_data',
+                    fields: [
+                        {
+                            name: 'issuer',
+                            label: '@i18n:objects.cert.issued_by',
+                            adapter: {
+                                object_index: 0,
+                                result_index: 1
+                            },
+                            read_only: true
+                        },
+                        {
+                            name: 'subject',
+                            label: '@i18n:objects.cert.issued_to',
+                            adapter: {
+                                object_index: 0,
+                                result_index: 1
+                            },
+                            read_only: true
+                        },
+                        {
+                            name: 'serial_number',
+                            label: '@i18n:objects.cert.serial_number',
+                            adapter: {
+                                object_index: 0,
+                                result_index: 1
+                            },
+                            read_only: true
+                        },
+                        {
+                            name: 'serial_number_hex',
+                            label: '@i18n:objects.cert.serial_number_hex',
+                            adapter: {
+                                object_index: 0,
+                                result_index: 1
+                            },
+                            read_only: true
+                        },
+                        {
+                            name: 'valid_not_before',
+                            label: '@i18n:objects.cert.valid_from',
+                            adapter: {
+                                object_index: 0,
+                                result_index: 1
+                            },
+                            read_only: true
+                        },
+                        {
+                            name: 'valid_not_after',
+                            label: '@i18n:objects.cert.valid_to',
+                            adapter: {
+                                object_index: 0,
+                                result_index: 1
+                            },
+                            read_only: true
+                        },
+                        {
+                            name: 'sha1_fingerprint',
+                            label: '@i18n:objects.cert.sha1_fingerprint',
+                            adapter: {
+                                object_index: 0,
+                                result_index: 1
+                            },
+                            read_only: true
+                        },
+                        {
+                            name: 'sha256_fingerprint',
+                            label: '@i18n:objects.cert.sha256_fingerprint',
+                            adapter: {
+                                object_index: 0,
+                                result_index: 1
+                            },
+                            read_only: true
+                        }
+                    ]
+                },
+                {
+                    $factory: IPA.section,
+                    name: 'divider',
+                    layout_css_class: 'col-sm-12 col-sm-12',
+                    fields: []
+                },
+                {
+                    name: 'user_result_table',
+                    label: '@i18n:objects.certmap_match.matched_users',
+                    layout: {
+                        $factory: widget_mod.fluid_layout,
+                        widget_cls: "col-sm-12 col-sm-12",
+                        label_cls: "hide"
+                    },
+                    layout_css_class: 'col-md-12 col-sm-12',
+                    fields: [
+                        {
+                            $type: 'association_table',
+                            name: 'result_table',
+                            read_only: true,
+                            selectable: false,
+                            other_entity: 'user',
+                            adapter: {
+                                $type: 'certmatch_transform'
+                            },
+                            columns: [
+                                {
+                                    name: 'uid',
+                                    label: '@i18n:objects.certmap_match.userlogin'
+                                },
+                                {
+                                    name: 'domain',
+                                    label: '@i18n:objects.certmap_match.domain'
+                                }
+                            ]
+                        }
+                    ]
+                }
+            ]
+        }
+    ]
+};};
+
+
+/**
+ * Artificial entity created from command which does not have its own entity
+ *
+ * @class certmapmatch.certmapmatch_entity
+ * @extends IPA.entity
+ */
+certmapmatch.certmapmatch_entity = function(spec) {
+    var that = IPA.entity(spec);
+
+    that.get_default_metadata = function() {
+        return metadata_provider.get('@mc:'+that.name);
+    };
+
+    return that;
+};
+
+/**
+ * Custom facet which is used for showing certmap match information
+ *
+ * @class certmapmatch.details_certmapmatch_facet
+ * @extends IPA.details_facet
+ */
+certmapmatch.details_certmapmatch_facet = function(spec) {
+
+    spec = spec || {};
+
+    var that = IPA.details_facet(spec);
+
+    that.refresh = function() {};
+
+    // always not dirty
+    that.is_dirty = function() {
+        return false;
+    };
+
+    that.get_result_table_widget = function() {
+        return that.widgets.get_widget("user_result_table.result_table");
+    };
+
+    that.update_result_table_summary = function(summary) {
+        var result_w = that.get_result_table_widget();
+        result_w.summary.text(summary);
+    };
+
+    that.clean_result = function() {
+        var result_w = that.get_result_table_widget();
+        result_w.empty();
+        that.update_result_table_summary('');
+    };
+
+    that.clean_cert_info = function() {
+        var widgets = that.widgets.get_widget('parsed_cert').widgets.get_widgets();
+
+        for (var i=0, l=widgets.length; i<l; i++) {
+            var widget = widgets[i];
+
+            widget.update();
+        }
+    };
+
+    that.obtain_cert = function() {
+        var cert_w = that.widgets.get_widget('cert_input.cert_textarea');
+
+        return cert_w.save();
+    };
+
+    that.on_cert_match = function(data) {
+        that.clean_result();
+        that.clean_cert_info();
+        var cert = that.obtain_cert();
+
+        if (util.is_empty(cert)) return;
+
+        var batch_command = rpc.batch_command({
+            name: 'certmap-match-batch',
+            show_error: false
+        });
+
+        var command = rpc.command({
+            method: 'certmap_match',
+            args: cert
+        });
+
+        batch_command.add_command(command);
+
+        command = rpc.command({
+            entity: 'cert',
+            method: 'find',
+            options: {
+                certificate: cert[0],
+                all: true
+            }
+        });
+
+        batch_command.add_command(command);
+
+        batch_command.on_success = function(data, text_status, xhr) {
+            // Error handling needs to be here because cert-find never fails,
+            // therefore batch_command always calls on_success method.
+            var certmatch_r = data.result.results[0];
+            if (certmatch_r.error === null) {
+                //no error
+                that.load(data);
+                that.update_result_table_summary(certmatch_r.summary);
+                IPA.notify_success(certmatch_r.summary);
+            } else {
+                that.update_result_table_summary(certmatch_r.error);
+                IPA.notify(certmatch_r.error, 'error');
+            }
+        };
+
+        batch_command.execute();
+    };
+
+    that.on_clear_facet = function() {
+        that.reset();
+        that.clean_result();
+        that.clean_cert_info();
+    };
+
+    that.init = function() {
+        on(that, 'cert-match', that.on_cert_match);
+        on(that, 'clear-facet', that.on_clear_facet);
+    };
+
+    return that;
+};
+
+/**
+ * Action which run certmap match.
+ *
+ * @class certmapmatch.match_action
+ * @extends IPA.object_action
+ */
+certmapmatch.match_action = function(spec) {
+    spec = spec || {};
+    spec.name = spec.name || 'match';
+
+    var that = IPA.object_action(spec);
+
+    that.execute_action = function(facet) {
+        facet.emit('cert-match');
+    };
+
+    return that;
+};
+
+
+/**
+ * Action which allows to clean whole facet.
+ *
+ * @class certmapmatch.clean_action
+ * @extends IPA.object_action
+ */
+certmapmatch.clear_action = function(spec) {
+    spec = spec || {};
+    spec.name = spec.name || 'clear';
+
+    var that = IPA.object_action(spec);
+
+    that.execute_action = function(facet) {
+        facet.emit('clear-facet');
+    };
+
+    return that;
+};
+
+/**
+ * Certificate Mapping Configuration entity specification object
+ * @member certmap
+ */
+certmapmatch.certmap_spec = make_certmap_spec();
+
+
+/**
+ * Register entity
+ * @member cermap
+ */
+certmapmatch.register = function() {
+    var e = reg.entity;
+    var f = reg.field;
+    var a = reg.action;
+
+    a.register('match', certmapmatch.match_action);
+    a.register('clear', certmapmatch.clear_action);
+    f.register('cert_textarea', certmapmatch.cert_textarea_field);
+    e.register({
+        type: 'certmap_match',
+        spec: certmapmatch.certmap_spec,
+        factory: certmapmatch.certmapmatch_entity
+    });
+};
+
+phases.on('registration', certmapmatch.register);
+
+return certmapmatch;
+});
diff --git a/ipaserver/plugins/internal.py b/ipaserver/plugins/internal.py
index acd417b..36d17ca 100644
--- a/ipaserver/plugins/internal.py
+++ b/ipaserver/plugins/internal.py
@@ -219,6 +219,8 @@ class i18n_messages(Command):
             "apply": _("Apply"),
             "back": _("Back"),
             "cancel": _("Cancel"),
+            "clear": _("Clear"),
+            "clear_title": _("Clear all fields on the page."),
             "close": _("Close"),
             "disable": _("Disable"),
             "download": _("Download"),
@@ -230,6 +232,8 @@ class i18n_messages(Command):
             "get": _("Get"),
             "hide": _("Hide"),
             "issue": _("Issue"),
+            "match": _("Match"),
+            "match_title": _("Match users according to certificate."),
             "ok": _("OK"),
             "refresh": _("Refresh"),
             "refresh_title": _("Reload current settings from the server."),
@@ -464,6 +468,14 @@ class i18n_messages(Command):
                 "view_certificate": _("Certificate for ${entity} ${primary_key}"),
                 "view_certificate_btn": _("View Certificate"),
             },
+            "certmap_match": {
+                "cert_data": _("Certificate Data"),
+                "cert_for_match": _("Certificate For Match"),
+                "facet_label": _("Certificate Mapping Match"),
+                "domain": _("Domain"),
+                "matched_users": _("Matched Users"),
+                "userlogin": _("User Login"),
+            },
             "certmap": {
                 "adder_title": _("Add Certificate Mapping Data"),
                 "data_label": _("Certificate mapping data"),
-- 
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