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); $('#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 ------ */ function um_user_save() { var user = um_user_form_lesen(); var kww = $('#kennwortw').val(); if(user.pw != kww) { alert('Kennworte stimmen nicht ueberein.'); } else { um_apicall_create_user(self.serialisieren(user)); } } function um_del_user() { var userId = um_get_first_selected_user_id(); if(userId !== 'none') { var antwort = confirm("Wollen Sie den Nutzer " + userId + " loeschen?"); if (antwort === true) { var user = new User(userId, '-', '-', '-', '-'); um_apicall_del_user(self.serialisieren(user)); } } } /* ----------- role functions ------------ */ function um_new_role() { var roleIsNew = true; var userId = um_get_first_selected_user_id(); if(userId === 'none') { alert('Es wurde kein Benutzer ausgewaehlt.'); } else { var answer = prompt("Name der Rolle", ""); if (answer === null) { alert("Es wurde Cancel betaetigt."); } else if(answer !== "") { um_apicall_grant_role(userId, answer, roleIsNew); } else { alert("OK betaetigt, aber Keine Rolle angegeben."); } } } 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() { $('#rolleErteilen').prop( "disabled", false ); } function um_user_role_list_click() { $('#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 ------ */ function um_user_form_lesen() { var userId = $('#anmeldename').val(); var kw = $('#kennwort').val(); 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}} * ' * {{/List}} * {{/List}} */ function um_show_user_list(resp) { $("#nutzerliste").empty(); var template = $('#tpl-list').html(); Mustache.parse(template); // optional, speeds up future uses $("#nutzerliste").append( Mustache.render(template, resp) ); } function um_clear_user_form() { $('#anmeldename').val(''); $('#kennwort').val(''); $('#kennwortw').val(''); } function um_get_first_selected_user_id() { var userId = 'none'; var userIds = []; $('#nutzerliste :selected').each(function(i, selected){ userIds[i] = $(selected).text(); }); if(userIds.length > 0) { userId = userIds[0]; } return userId; } 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 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 template = $('#tpl-list').html(); Mustache.parse(template); // optional, speeds up future uses $("#rollen").append( Mustache.render(template, resp) ); $('#rolleErteilen').prop( "disabled", true ); } /* ---- api calls ----- */ function um_apicall_create_user(user) { var m = 'createUser'; var u = '../svc/' + m; $.ajax({ url: u, data: { p: user }, type: "POST", dataType : "html", success: function( resp ) { um_apicall_get_user_list(); um_back_from_new_user(); }, error: function( xhr, status, errorThrown ) { $('#fehler').html("Error: " + errorThrown + " Status: " + status); }, complete: function( xhr, status ) { //alert( "The request is complete!" ); } }); } function um_apicall_del_user(user) { var m = 'deleteUser'; var u = '../svc/' + m; $.ajax({ url: u, data: { p: user }, type: "POST", dataType : "html", success: function( resp ) { um_apicall_get_user_list(); }, 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_list() { var m = 'getUserNameList'; var u = '../svc/' + m; $.ajax({ url: u, type: "GET", dataType : "json", success: function( resp ) { um_show_user_list(resp); }, error: function( xhr, status, errorThrown ) { $('#fehler').html("Error: " + errorThrown + " Status: " + status); }, complete: function( xhr, status ) { //alert( "The request is complete!" ); } }); } // 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: { p: self.serialisieren(ur) }, type: "POST", dataType : "html", success: function( resp ) { um_apicall_get_user_roles(userId); if(isNewRole) { um_apicall_get_granted_roles(); } }, error: function( xhr, status, errorThrown ) { $('#fehler').html("Error: " + errorThrown + " Status: " + status); }, complete: function( xhr, status ) { //alert( "The request is complete!" ); } }); } 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; $.ajax({ url: u, type: "GET", dataType : "json", success: function( resp ) { um_show_user_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_granted_roles() { var m = 'getRoleNamesGranted'; var u = '../svc/' + m; $.ajax({ url: u, type: "GET", dataType : "json", success: function( resp ) { 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); }, complete: function( xhr, status ) { //alert( "The request is complete!" ); } }); } /* ----- Hilfsfunktionen ----- */ function serialisieren(obj) { return '{"' + obj.constructor.name + '":' + JSON.stringify(obj) + '}'; }; /* ----- Objekte ----- */ function User(i, p, fn, ln, em) { this.id = i; this.pw = p; this.firstName = fn; this.lastName = ln; this.email = em; } function UserRole(u, r) { this.userId = u; this.role = r; }