From 3d5fe0c05ec31993c5cf85f18f7202719f5bda11 Mon Sep 17 00:00:00 2001
From: ulrich@undisclosed
Date: Sat, 09 May 2020 16:03:12 +0000
Subject: [PATCH] Dialog Neuer Nutzer begonnen

---
 web/ui/ui.js |  113 ++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 85 insertions(+), 28 deletions(-)

diff --git a/web/ui/ui.js b/web/ui/ui.js
index 5da03e6..0665ebf 100644
--- a/web/ui/ui.js
+++ b/web/ui/ui.js
@@ -17,9 +17,12 @@
   $('#neueRolle').click(um_new_role);
   $('#rollen').click(um_role_list_click);
   $('#nutzerrollen').click(um_user_role_list_click);
+  $('#userMenu').text('?');
+  $('#logout').click(um_apicall_logout);
   um_clear_user_form();
   um_apicall_get_user_list();
   um_apicall_get_granted_roles();
+  um_apicall_get_login();
 }
 
 /* ----- user functions ------ */
@@ -130,18 +133,32 @@
 function um_user_form_lesen() {
   var userId = $('#anmeldename').val();
   var kw = $('#kennwort').val();
-  var user = new User(userId, kw, '-', '-', '-');
+  var firstName = $('#vorname').val();
+  var lastName = $('#nachname').val();
+  var user = new User(userId, kw, firstName, lastName, '-');
   return user;
 }
 
+/*
+ * BaseLink liefert fuer einfache selects wie die der Nutzerverwaltung 
+ * eine simple Listenfunktion, die auf der JSON-Seite wie folgt 
+ * herauskommt.
+ * 
+ * {"List":[{"List":[{"String":"USER_NAME"},{"String":"admin"},{"String":"test"}]}]}
+ * 
+ * Fuer Mustache sieht ein Template dann z.B so aus:
+ * 
+ *    {{#List}}
+ *    {{#List}}
+ *      <option value="{{ String }}">{{ String }}</option>'
+ *    {{/List}}
+ *    {{/List}}
+ */
 function um_show_user_list(resp) {
   $("#nutzerliste").empty();
-  var users = resp.List[0];
-  for(var i = 1; i < users.List.length; i++) {
-    var user = users.List[i];
-    $("#nutzerliste").append('<option value=' + user.String + '>' + user.String + '</option>');
-  }
-  $('#nutzerLoeschen').prop( "disabled", true );
+  var template = $('#tpl-list').html();
+  Mustache.parse(template);   // optional, speeds up future uses
+  $("#nutzerliste").append( Mustache.render(template, resp) );
 }
 
 function um_clear_user_form() {
@@ -186,25 +203,24 @@
   return roleId;
 }
 
-// {"List":[{"List":[{"String":"ROLE_NAME"},{"String":"testrolle1"}]}]}
 function um_show_user_role_list(resp) {
   $("#nutzerrollen").empty();
-  var rollen = resp.List[0];
-  for(var i = 1; i < rollen.List.length; i++) {
-    var rolle = rollen.List[i];
-    $("#nutzerrollen").append('<option value=' + rolle.String + '>' + rolle.String + '</option>');
-  }
+  
+  var template = $('#tpl-list').html();
+  Mustache.parse(template);   // optional, speeds up future uses
+  $("#nutzerrollen").append( Mustache.render(template, resp) );
+  
   $('#nutzerRolleEntziehen').prop( "disabled", true );
+  $('#rolleErteilen').prop( "disabled", true );
 }
 
 function um_show_role_list(resp) {
   $("#rollen").empty();
-  var rollen = resp.List[0];
-  for(var i = 1; i < rollen.List.length; i++) {
-    var rolle = rollen.List[i];
-    $("#rollen").append('<option value=' + rolle.String + '>' + rolle.String + '</option>');
-  }
-  //$('#neueRolle').prop( "disabled", true );
+  
+  var template = $('#tpl-list').html();
+  Mustache.parse(template);   // optional, speeds up future uses
+  $("#rollen").append( Mustache.render(template, resp) );
+  
   $('#rolleErteilen').prop( "disabled", true );
 }
 
@@ -221,7 +237,6 @@
     type: "POST",
     dataType : "html",
     success: function( resp ) {
-      //$('#fehler').html('Antwort: ' + resp);
       um_apicall_get_user_list();
       um_back_from_new_user();
     },
@@ -275,15 +290,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",
@@ -303,13 +318,13 @@
 }
 
 function um_apicall_revoke_role(userId, roleName) {
+  var ur = new UserRole(userId, roleName);
   var m = 'revokeRole';
   var u = '../svc/' + m;
   $.ajax({
     url: u,
     data: {
-      p1: userId,
-      p2: roleName
+      p: self.serialisieren(ur)
     },
     type: "POST",
     dataType : "html",
@@ -325,7 +340,6 @@
   });
 }
 
-//public List getUserRoleNames(String userId)
 function um_apicall_get_user_roles(userId) {
   var m = 'getUserRoleNames';
   var u = '../svc/' + m + '?p=' + userId;
@@ -334,7 +348,6 @@
     type: "GET",
     dataType : "json",
     success: function( resp ) {
-      // Antwort in Liste anzeigen
       um_show_user_role_list(resp);
     },
     error: function( xhr, status, errorThrown ) {
@@ -354,8 +367,47 @@
     type: "GET",
     dataType : "json",
     success: function( resp ) {
-      // Antwort in Liste anzeigen
       um_show_role_list(resp);
+    },
+    error: function( xhr, status, errorThrown ) {
+      $('#fehler').html("Error: " + errorThrown + " Status: " + status);
+    },
+    complete: function( xhr, status ) {
+      //alert( "The request is complete!" );
+    }
+  });      
+}
+
+function um_apicall_get_login() {
+  var m = '?c=de.uhilger.um.pub.SessionManager&m=getSessionUser';
+  var u = '../pub' + m;
+  $.ajax({
+    url: u,
+    type: "GET",
+    dataType : "json",
+    success: function( resp ) {
+      $('#userMenu').text(resp.UserData.firstName);
+    },
+    error: function( xhr, status, errorThrown ) {
+      $('#fehler').html("Error: " + errorThrown + " Status: " + status);
+    },
+    complete: function( xhr, status ) {
+      //alert( "The request is complete!" );
+    }
+  });      
+  
+}
+
+function um_apicall_logout() {
+  var m = '?c=de.uhilger.um.pub.SessionManager&m=expireSession';
+  var u = '../pub' + m;
+  $.ajax({
+    url: u,
+    type: "GET",
+    dataType : "text",
+    success: function( resp ) {
+      $('#userMenu').text('nicht angemeldet');
+      window.location.href = '../logout.html';
     },
     error: function( xhr, status, errorThrown ) {
       $('#fehler').html("Error: " + errorThrown + " Status: " + status);
@@ -380,4 +432,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