Blob Blame History Raw
From 014aab243a4e7185ad5ebdc0a71e7de81553e501 Mon Sep 17 00:00:00 2001
From: Pavel Vomacka <pvomacka@redhat.com>
Date: Mon, 17 Oct 2016 14:33:07 +0200
Subject: [PATCH] WebUI: services without canonical name are shown correctly

There is a change introduced in 4.4 that new services have canonical name. The old ones
didn't have it, therefore these services were not correctly displayed in WebUI.

This patch adds support for this type of services. Service name is taken from
'krbprincipalname' attribute in case that 'krbcanonicalname' attribute is not present
in server response.

https://fedorahosted.org/freeipa/ticket/6397

Reviewed-By: Petr Vobornik <pvoborni@redhat.com>
---
 install/ui/src/freeipa/field.js   | 41 ++++++++++++++++++++++++++++++
 install/ui/src/freeipa/service.js | 52 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 92 insertions(+), 1 deletion(-)

diff --git a/install/ui/src/freeipa/field.js b/install/ui/src/freeipa/field.js
index d8b957f5ab28b5ee4bc4ebce2ae6f454083bc4fd..efa2fb6ef4d4b5384661e9023ace511730954153 100644
--- a/install/ui/src/freeipa/field.js
+++ b/install/ui/src/freeipa/field.js
@@ -1306,6 +1306,46 @@ field.ObjectAdapter = declare([field.Adapter], {
 
 
 /**
+ * Custom adapter for fields which handles situations when there is no value
+ * for attribute (name) of the field and we want to use alternative attribute
+ * from response. We can set the alternative attribute name to the 'alt_attr'
+ * attribute of the adapter.
+ * This adapter is used i.e. in table in search facet for services. Handles
+ * situations where older services don't have canonical name.
+ *
+ * @class
+ * @extends field.Adapter
+ */
+field.AlternateAttrFieldAdapter = declare([field.Adapter], {
+    /**
+     * In case that the value is not get using field name then use alternative
+     * name.
+     * @param {Object} data Object which contains the record or the record
+     * @param {string} [attribute] attribute name - overrides `context.param`
+     * @param {Mixed} [def_val] default value - overrides `context.default_value`
+     * @returns {Array} attribute value
+     */
+    load: function(data, attribute, def_val) {
+        var record = this.get_record(data);
+        var value = null;
+        var attr = attribute || this.context.param;
+        var def = def_val || this.context.default_value;
+        if (record) {
+            value = this.get_value(record, attr);
+            if (util.is_empty(value) && this.context.adapter.alt_attr) {
+                value = this.get_value(record, this.context.adapter.alt_attr);
+            }
+        }
+        if (util.is_empty(value) && !util.is_empty(def)) {
+            value = util.normalize_value(def);
+        }
+        value = rpc.extract_objects(value);
+        return value;
+    }
+});
+
+
+/**
  * Field for enabling/disabling entity
  *
  * - expects radio widget
@@ -1577,6 +1617,7 @@ field.register = function() {
 
     l.register('adapter', field.Adapter);
     l.register('object_adapter', field.ObjectAdapter);
+    l.register('alternate_attr_field_adapter', field.AlternateAttrFieldAdapter);
 };
 phases.on('registration', field.register);
 
diff --git a/install/ui/src/freeipa/service.js b/install/ui/src/freeipa/service.js
index 30e336c35b8eece2e5e3ef55629d0c98f097fbf5..a6607d22e83047fb2d0dcc7775891445df4910b7 100644
--- a/install/ui/src/freeipa/service.js
+++ b/install/ui/src/freeipa/service.js
@@ -58,7 +58,16 @@ return {
     facets: [
         {
             $type: 'search',
-            columns: [ 'krbcanonicalname' ]
+            $factory: IPA.service.search_facet,
+            columns: [
+                {
+                    name: 'krbcanonicalname',
+                    adapter: {
+                        $type: 'alternate_attr_field_adapter',
+                        alt_attr: 'krbprincipalname'
+                    }
+                }
+            ]
         },
         {
             $type: 'details',
@@ -403,6 +412,47 @@ return {
     }
 };};
 
+
+/**
+ * Custom search facet for services. It has alternative primary key, in case
+ * that the service doesn't have canonical name.
+ */
+IPA.service.search_facet = function(spec) {
+    spec = spec || {};
+
+    spec.alternative_pkey = spec.alternative_pkey || 'krbprincipalname';
+
+    var that = IPA.search_facet(spec);
+
+    that.alternative_pkey = spec.alternative_pkey;
+
+    that.get_records_map = function(data) {
+
+        var records_map = $.ordered_map();
+
+        var result = data.result.result;
+        var pkey_name = that.managed_entity.metadata.primary_key ||
+                                                        that.primary_key_name;
+        var adapter = builder.build('adapter', 'adapter', {context: that});
+
+        for (var i=0; i<result.length; i++) {
+            var record = result[i];
+            var pkey = adapter.load(record, pkey_name)[0];
+            if (pkey === undefined && that.alternative_pkey) {
+                pkey = adapter.load(record, that.alternative_pkey)[0];
+            }
+            if (that.filter_records(records_map, pkey, record)) {
+                records_map.put(pkey, record);
+            }
+        }
+
+        return records_map;
+    };
+
+    return that;
+};
+
+
 IPA.service.details_facet = function(spec, no_init) {
 
     var that = IPA.details_facet(spec, true);
-- 
2.7.4