URL: https://github.com/freeipa/freeipa/pull/166
Author: pvomacka
 Title: #166: WebUI: services without canonical name are shown correctly
Action: synchronized

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/166/head:pr166
git checkout pr166
From ffaa0458ecc872bf8d5a82499bb7820e304cb935 Mon Sep 17 00:00:00 2001
From: Pavel Vomacka <pvoma...@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
---
 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 3088e22..d70a778 100644
--- a/install/ui/src/freeipa/field.js
+++ b/install/ui/src/freeipa/field.js
@@ -1361,6 +1361,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
@@ -1632,6 +1672,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 30e336c..a6607d2 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);
-- 
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