From 54bbae3b0cdf11bb776ba7360fba2f690d6cfd50 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Tue, 31 Jan 2017 08:27:16 +0000
Subject: [PATCH] Datenbankzugriff in der Herstellungsbeschreibung ergänzt

---
 web/ui/ui.js |  123 ++++++++++++++++++++++++++++++++++++----
 1 files changed, 109 insertions(+), 14 deletions(-)

diff --git a/web/ui/ui.js b/web/ui/ui.js
index c6e9098..0e7b427 100644
--- a/web/ui/ui.js
+++ b/web/ui/ui.js
@@ -1,12 +1,19 @@
 
 function um_init() {
+  $('#user-form').hide();
+  $('#nav').hide();
+  $('#role-form').hide();
+  $('#user-role-form').hide();
   $('.user-save-btn').click(um_user_save);
+  $('.user-cancel-btn').click(um_back_from_new_user);
   $('#nutzerliste').click(um_user_list_click);
   $('#nutzerLoeschen').click(um_del_user);
   $('#nutzerNeu').click(um_new_user);
   $('#nutzerLoeschen').prop( "disabled", true );
   $('#nutzerRolleEntziehen').prop( "disabled", true );
   $('#rolleErteilen').prop( "disabled", true );
+  $('#rolleErteilen').click(um_grant_role);
+  $('#nutzerRolleEntziehen').click(um_revoke_role);
   $('#neueRolle').click(um_new_role);
   $('#rollen').click(um_role_list_click);
   $('#nutzerrollen').click(um_user_role_list_click);
@@ -38,10 +45,6 @@
   }
 }
 
-function um_new_user() {
-  alert('show form to enter new user');
-}
-
 /* ----------- role functions ------------ */
 
 function um_new_role() {
@@ -61,12 +64,43 @@
   }
 }
 
+function um_grant_role() {
+  var roleIsNew = false;
+  var userId = um_get_first_selected_user_id();
+  var roleId = um_get_first_selected_role_id();
+  if(roleId !== 'none' && userId !== 'none') {
+    um_apicall_grant_role(userId, roleId, roleIsNew);
+  }
+}
+
+function um_revoke_role() {
+  var userId = um_get_first_selected_user_id();
+  var roleId = um_get_first_selected_userrole_id();
+  if(roleId !== 'none' && userId !== 'none') {
+    um_apicall_revoke_role(userId, roleId);
+  }
+}
+
 /* ----------- ui interaction ------------ */
 
 function um_user_list_click() {
+  $('#nav-back-btn').click(um_back_from_roles);
   $('#nutzerLoeschen').prop( "disabled", false );
+  $('#rollen :selected').prop("selected", false);
   var userId = um_get_first_selected_user_id();
+  $('#userid').html(userId);
   um_apicall_get_user_roles(userId);
+  $('#user-list-form').hide();
+  $('#nav').show();
+  $('#role-form').show();
+  $('#user-role-form').show();
+}
+
+function um_new_user() {
+  $('#nav-back-btn').click(um_back_from_new_user);
+  $('#user-list-form').hide();
+  $('#user-form').show();
+  $('#nav').show();
 }
 
 function um_role_list_click() {
@@ -77,6 +111,19 @@
   $('#nutzerRolleEntziehen').prop( "disabled", false );
 }
 
+function um_back_from_roles() {
+  $('#user-list-form').show();
+  $('#nav').hide();
+  $('#role-form').hide();
+  $('#user-role-form').hide();
+}
+
+function um_back_from_new_user() {
+  $('#user-list-form').show();
+  $('#nav').hide();
+  $('#user-form').hide();
+  um_clear_user_form();
+}
 
 /* ------- ui i/o ------ */
 
@@ -115,7 +162,30 @@
   return userId;
 }
 
-// {"List":[{"List":[{"String":"ROLE_NAME"},{"String":"testrolle1"}]}]}
+function um_get_first_selected_role_id() {
+  var roleId = 'none';
+  var roleIds = [];
+  $('#rollen :selected').each(function(i, selected){
+    roleIds[i] = $(selected).text();
+  });
+  if(roleIds.length > 0) {
+    roleId = roleIds[0];
+  }
+  return roleId;
+}
+
+function um_get_first_selected_userrole_id() {
+  var roleId = 'none';
+  var roleIds = [];
+  $('#nutzerrollen :selected').each(function(i, selected){
+    roleIds[i] = $(selected).text();
+  });
+  if(roleIds.length > 0) {
+    roleId = roleIds[0];
+  }
+  return roleId;
+}
+
 function um_show_user_role_list(resp) {
   $("#nutzerrollen").empty();
   var rollen = resp.List[0];
@@ -124,6 +194,7 @@
     $("#nutzerrollen").append('<option value=' + rolle.String + '>' + rolle.String + '</option>');
   }
   $('#nutzerRolleEntziehen').prop( "disabled", true );
+  $('#rolleErteilen').prop( "disabled", true );
 }
 
 function um_show_role_list(resp) {
@@ -133,9 +204,9 @@
     var rolle = rollen.List[i];
     $("#rollen").append('<option value=' + rolle.String + '>' + rolle.String + '</option>');
   }
-  //$('#neueRolle').prop( "disabled", true );
   $('#rolleErteilen').prop( "disabled", true );
 }
+
 /* ---- api calls ----- */
 
 function um_apicall_create_user(user) {
@@ -149,9 +220,8 @@
     type: "POST",
     dataType : "html",
     success: function( resp ) {
-      //$('#fehler').html('Antwort: ' + resp);
       um_apicall_get_user_list();
-      um_clear_user_form();
+      um_back_from_new_user();
     },
     error: function( xhr, status, errorThrown ) {
       $('#fehler').html("Error: " + errorThrown + " Status: " + status);
@@ -203,15 +273,15 @@
   });      
 }
 
-// public UserRole grantRole(String userId, String roleName) 
+// http]://example.com/um/api?c=de.uhilger.um.api.UserMgr&m=testmethode
 function um_apicall_grant_role(userId, roleName, isNewRole) {
+  var ur = new UserRole(userId, roleName);
   var m = 'grantRole';
   var u = '../svc/' + m;
   $.ajax({
     url: u,
     data: {
-      p1: userId,
-      p2: roleName
+      p: self.serialisieren(ur)
     },
     type: "POST",
     dataType : "html",
@@ -230,7 +300,29 @@
   });
 }
 
-//public List getUserRoleNames(String userId)
+function um_apicall_revoke_role(userId, roleName) {
+  var ur = new UserRole(userId, roleName);
+  var m = 'revokeRole';
+  var u = '../svc/' + m;
+  $.ajax({
+    url: u,
+    data: {
+      p: self.serialisieren(ur)
+    },
+    type: "POST",
+    dataType : "html",
+    success: function( resp ) {
+      um_apicall_get_user_roles(userId);
+    },
+    error: function( xhr, status, errorThrown ) {
+      $('#fehler').html("Error: " + errorThrown + " Status: " + status);
+    },
+    complete: function( xhr, status ) {
+      //alert( "The request is complete!" );
+    }
+  });
+}
+
 function um_apicall_get_user_roles(userId) {
   var m = 'getUserRoleNames';
   var u = '../svc/' + m + '?p=' + userId;
@@ -239,7 +331,6 @@
     type: "GET",
     dataType : "json",
     success: function( resp ) {
-      // Antwort in Liste anzeigen
       um_show_user_role_list(resp);
     },
     error: function( xhr, status, errorThrown ) {
@@ -259,7 +350,6 @@
     type: "GET",
     dataType : "json",
     success: function( resp ) {
-      // Antwort in Liste anzeigen
       um_show_role_list(resp);
     },
     error: function( xhr, status, errorThrown ) {
@@ -285,4 +375,9 @@
   this.firstName = fn;
   this.lastName = ln;
   this.email = em;
+}
+
+function UserRole(u, r) {
+  this.userId = u;
+  this.role = r;
 }
\ No newline at end of file

--
Gitblit v1.9.3