[Kimchi-devel] [PATCH 2/2] Change guest edit permission logic

lvroyce at linux.vnet.ibm.com lvroyce at linux.vnet.ibm.com
Wed Nov 5 10:21:15 UTC 2014


From: Royce Lv <lvroyce at linux.vnet.ibm.com>

Update api for ldap user query,
also adopt guest edit permission, validate user when change permission,
add notice icon to indicate invalid user input.

Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
---
 ui/js/src/kimchi.api.js             |  13 +++-
 ui/js/src/kimchi.guest_edit_main.js | 146 ++++++++++++++++++++++++++++++------
 2 files changed, 131 insertions(+), 28 deletions(-)

diff --git a/ui/js/src/kimchi.api.js b/ui/js/src/kimchi.api.js
index ebf826b..1483b02 100644
--- a/ui/js/src/kimchi.api.js
+++ b/ui/js/src/kimchi.api.js
@@ -1087,14 +1087,21 @@ var kimchi = {
         });
     },
 
-    getHostUsers : function(suc, err) {
+    getHostUsers : function(usr_data, suc, err) {
+        var usr = usr_data ? '?_user_id='+usr_data.user_id : '';
+        var handler = function(data) {
+            if (usr_data) {
+                data['_kimchi_user'] = usr_data.user_id;
+            }
+            suc(data);
+        }
         kimchi.requestJSON({
-            url : kimchi.url + 'host/users',
+            url : kimchi.url + 'host/users' + usr,
             type : 'GET',
             contentType : 'application/json',
             dataType : 'json',
             resend : true,
-            success : suc,
+            success : handler,
             error : err ? err : function(data) {
                 kimchi.message.error(data.responseJSON.reason);
             }
diff --git a/ui/js/src/kimchi.guest_edit_main.js b/ui/js/src/kimchi.guest_edit_main.js
index ed8c689..701c101 100644
--- a/ui/js/src/kimchi.guest_edit_main.js
+++ b/ui/js/src/kimchi.guest_edit_main.js
@@ -42,7 +42,6 @@ kimchi.guest_edit_main = function() {
         }, function(storages) {
             var container = $('#form-guest-edit-storage .body');
             $(container).empty();
-
             $.each(storages, function(index, storage) {
                 storage['vm'] = kimchi.selectedGuest;
                 rowHTML = $('#' + storage['type'] + '-row-tmpl').html();
@@ -258,25 +257,108 @@ kimchi.guest_edit_main = function() {
     };
 
     var setupPermission = function() {
+       //set up for LDAP
+       $(".add", "#form-guest-edit-permission").button({
+            icons: { primary: "ui-icon-plusthick" },
+            text: false
+        }).click(function(evt){
+            evt.preventDefault();
+            addItem({
+                user: "",
+                freeze: false,
+                viewMode: "hide",
+                editMode: "",
+                checked: true
+            });
+        });
+        var addItem = function(data) {
+            var itemNode = $.parseHTML(kimchi.substitute($('#ldap-user-tmpl').html(),data));
+            $(".body", "#form-guest-edit-permission .ldap").append(itemNode);
+            $(".edit", itemNode).button({
+                disabled: data.freeze,
+                icons: { primary: "ui-icon-pencil" },
+                text: false
+            }).click(function(evt){
+                evt.preventDefault();
+                toggleEdit($(this).parent().parent(), true);
+            });
+            $(".delete", itemNode).button({
+                icons: { primary: "ui-icon-trash" },
+                text: false
+            }).click(function(evt){
+                evt.preventDefault();
+                var item = $(this).parent().parent();
+                item.remove();
+            });
+            $("input").focusout(function() {
+                var item = $(this).parent().parent();
+                var user= $(this).val();
+                item.prop("id", user);
+                $("label", item).text(user);
+                toggleEdit(item, false);
+            });
+            if (data.checked == true) {
+                $(".checked", itemNode).addClass("hide");
+            }
+            $(".cancel", itemNode).button({
+                icons: { primary: "ui-icon-arrowreturnthick-1-w" },
+                text: false
+            }).click(function(evt){
+                evt.preventDefault();
+                var item = $(this).parent().parent();
+                $("label", item).text()==="" ? item.remove() : toggleEdit(item, false);
+            });
+        };
+        var toggleEdit = function(item, on){
+            $("label", item).toggleClass("hide", on);
+            $("input", item).toggleClass("hide", !on);
+            $(".action-area", item).toggleClass("hide");
+        };
+        //set up for PAM
         var userNodes = {}, groupNodes = {};
-        kimchi.retrieveVM(kimchi.selectedGuest, function(vm){
-            kimchi.getHostUsers(function(users){
-                kimchi.getHostGroups(function(groups){
-                    var subArray = function(a1, a2){ //a1-a2
-                        for(var i=0; i<a2.length; i++){
-                            for(var j=0; j<a1.length; j++){
-                                if(a2[i] == a1[j]){
-                                    a1.splice(j, 1);
-                                    break;
+        var authType = 'ldap';
+        if (authType == 'pam') {
+            kimchi.retrieveVM(kimchi.selectedGuest, function(vm){
+                kimchi.getHostUsers(undefined, function(users){
+                    kimchi.getHostGroups(function(groups){
+                        var subArray = function(a1, a2){ //a1-a2
+                            for(var i=0; i<a2.length; i++){
+                                for(var j=0; j<a1.length; j++){
+                                    if(a2[i] == a1[j]){
+                                        a1.splice(j, 1);
+                                        break;
+                                    }
                                 }
                             }
-                        }
-                    };
-                    subArray(users, vm.users); subArray(groups, vm.groups);
-                    init(users, groups, vm.users, vm.groups);
+                        };
+                        subArray(users, vm.users); subArray(groups, vm.groups);
+                        init(users, groups, vm.users, vm.groups);
+                    });
                 });
             });
-        });
+        }
+        else if (authType == 'ldap') {
+            kimchi.retrieveVM(kimchi.selectedGuest, function(vm){
+                for (var i=0; i<vm.users.length; i++) {
+                    var user = {'user_id' :vm.users[i]};
+                    kimchi.getHostUsers(user, function(data) {
+                        addItem({
+                            user: data._kimchi_user,
+                            viewMode: "",
+                            freeze: true,
+                            editMode: "hide",
+                            checked: true});
+                    }, function (data) {
+                        addItem({
+                            user: data._kimchi_user,
+                            viewMode: "",
+                            freeze: true,
+                            editMode: "hide",
+                            checked: false});
+                    });
+                }
+            });
+        }
         var sortNodes = function(container, isUser){
             nodes = container.children();
             var keys = [];
@@ -296,7 +378,7 @@ kimchi.guest_edit_main = function() {
         var init = function(availUsers, availGroups, selUsers, selGroups){
             var initNode = function(key, isUserNode){
                 var nodeGroups = isUserNode ? userNodes : groupNodes;
-                nodeGroups[key] = $.parseHTML(kimchi.substitute($('#permission-item').html(), {
+                nodeGroups[key] = $.parseHTML(kimchi.substitute($('#permission-item-pam').html(), {
                     val: key,
                     class: isUserNode? "user-icon" : "group-icon"
                 }));
@@ -534,16 +616,30 @@ kimchi.guest_edit_main = function() {
     }
 
     var permissionSubmit = function(event) {
+        var authType = 'ldap';
         var content = { users: [], groups: [] };
-        $("#permission-sel-users").children().each(function(){
-            content.users.push($("label", this).text());
-        });
-        $("#permission-sel-groups").children().each(function(){
-            content.groups.push($("label", this).text());
-        });
-        kimchi.updateVM(kimchi.selectedGuest, content, function(){
-            kimchi.window.close();
-        });
+        if (authType == 'pam') {
+            $("#permission-sel-users").children().each(function(){
+                content.users.push($("label", this).text());
+            });
+            $("#permission-sel-groups").children().each(function(){
+                content.groups.push($("label", this).text());
+            });
+        }
+        else if (authType == 'ldap') {
+            $(".body","#form-guest-edit-permission .ldap").children().each(function () {
+                var user = {'user_id': $(this).attr("id")};
+                var tmp = $(this);
+                kimchi.getHostUsers(user, function(data) {
+                    }, function (data) {
+                        $(".checked", tmp).removeClass("hide");
+                });
+                content.users.push($(this).attr("id"));
+            });
+            kimchi.updateVM(kimchi.selectedGuest, content, function(){
+                kimchi.window.close();
+            });
+        }
     }
 
     // tap map, "general": 0, "storage": 1, "interface": 2, "permission": 3, "password": 4
-- 
1.8.3.2




More information about the Kimchi-devel mailing list