X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=security%2Fruntime%2Forg.argeo.security.ria%2Fsrc%2Fargeo-ria-lib%2Fsecurity%2Fclass%2Forg%2Fargeo%2Fsecurity%2Fria%2FUserEditorApplet.js;h=38ca9c85c5dce21b7841d7a691c4de40e922469a;hb=b9f0dc6d3192adcbd4e9de8053b087e36759820e;hp=2d51a172e3e7a17f39e7827cea1b58c25218ca43;hpb=98c2719d08b7d1791eb2080b4488bba810044608;p=lgpl%2Fargeo-commons.git diff --git a/security/runtime/org.argeo.security.ria/src/argeo-ria-lib/security/class/org/argeo/security/ria/UserEditorApplet.js b/security/runtime/org.argeo.security.ria/src/argeo-ria-lib/security/class/org/argeo/security/ria/UserEditorApplet.js index 2d51a172e..38ca9c85c 100644 --- a/security/runtime/org.argeo.security.ria/src/argeo-ria-lib/security/class/org/argeo/security/ria/UserEditorApplet.js +++ b/security/runtime/org.argeo.security.ria/src/argeo-ria-lib/security/class/org/argeo/security/ria/UserEditorApplet.js @@ -9,12 +9,16 @@ qx.Class.define("org.argeo.security.ria.UserEditorApplet", { extend : qx.ui.container.Composite, implement : [org.argeo.ria.components.IView], - + construct : function(){ this.base(arguments); this.setLayout(new qx.ui.layout.VBox(5)); }, - + + events : { + "savedUser" : "qx.event.type.Data" + }, + properties : { /** @@ -22,7 +26,7 @@ qx.Class.define("org.argeo.security.ria.UserEditorApplet", */ view : { init : null - }, + }, /** * Commands definition, see {@link org.argeo.ria.event.CommandsManager#definitions} */ @@ -159,13 +163,13 @@ qx.Class.define("org.argeo.security.ria.UserEditorApplet", * @param viewPane {org.argeo.ria.components.ViewPane} The viewPane. */ init : function(viewPane, data){ - if(!data){ + if(!data.USER){ var now = new Date(); this.setInstanceId(now.getTime()); this.setInstanceLabel("New User"); }else{ - this.setInstanceId(data); - this.setInstanceLabel("User " + data); + this.setInstanceId(data.USER); + this.setInstanceLabel("User " + data.USER); } this.setView(viewPane); this.setViewSelection(new org.argeo.ria.components.ViewSelection(viewPane.getViewId())); @@ -211,7 +215,7 @@ qx.Class.define("org.argeo.security.ria.UserEditorApplet", this.basicGB.add(this.usernameField, {row:0,column:1}); this.rolesField = new org.argeo.ria.components.ui.MultipleComboBox(); - this.rolesField.setChoiceValues(["ROLE_ADMIN", "ROLE_USER", "ROLE_USER1"]); + this.rolesField.setChoiceValues(data.ROLES_LIST); this.basicGB.add(new qx.ui.basic.Label("Roles"), {row:1,column:0}); this.basicGB.add(this.rolesField, {row:1,column:1}); @@ -250,17 +254,33 @@ qx.Class.define("org.argeo.security.ria.UserEditorApplet", var user = this.getCurrentUser(); user.setName(this.usernameField.getValue()); user.setRoles((this.rolesField.getValue()||"").split(",")); - // GO TO AND RETURN FROM SERVER - user.setNatures([ - { - "email" : "", - "firstName" : "", - "lastName" : "", - "type" : "org.argeo.security.nature.SimpleUserNature" - }] - ); - this.partialRefreshUser(user, ["details","natures"]); - this.setModified(false); + // GO TO AND RETURN FROM SERVER + if(user.isCreate()){ + var userExists = false; + var req = org.argeo.security.ria.SecurityAPI.getUserExistsService(user.getName()); + req.addListener("completed", function(response){ + userExists = response.getContent().value; + }, this); + req.setAsynchronous(false); + req.send(); + if(userExists){ + this.error("User already exists, choose another name!"); + return; + } + } + var userService = user.getSaveService(); + userService.send(); + userService.addListener("completed", function(response){ + if(response.getContent().status && response.getContent().status == "ERROR"){ + return; + } + this._setGuiInCreateMode(false); + user.load(response.getContent(), "json"); + this.partialRefreshUser(user, ["details","natures"]); + this.setModified(false); + this.getViewSelection().triggerEvent(); + this.fireDataEvent("savedUser", user); + }, this); }, _addNatureTab : function(natureClass, natureData, select){ @@ -294,13 +314,23 @@ qx.Class.define("org.argeo.security.ria.UserEditorApplet", cancelB.setVisibility("visible"); }); cancelB.addListener("execute", function(){ + if(newClass.getIsNew()){ + this._removeNatureTab(natureClass); + } newClass.setEditMode(false); editB.setVisibility("visible"); saveB.setVisibility("excluded"); cancelB.setVisibility("excluded"); - }); + }, this); saveB.addListener("execute", function(){ // SAVE CURRENT NATURE + var data = newClass.getData(); + if(newClass.getIsNew()){ + this.getCurrentUser().addNature(data); + }else{ + this.getCurrentUser().updateNature(data); + } + this.saveUser(); this.setNaturesModified(false); newClass.setEditMode(false); editB.setVisibility("visible"); @@ -308,9 +338,10 @@ qx.Class.define("org.argeo.security.ria.UserEditorApplet", cancelB.setVisibility("excluded"); }, this); if(natureData){ - newClass.setData(natureData); + newClass.setData(natureData); cancelB.execute(); }else{ + newClass.setIsNew(true); editB.execute(); } this.naturesTab.add(page); @@ -341,7 +372,11 @@ qx.Class.define("org.argeo.security.ria.UserEditorApplet", removeSelectedTab : function(){ var selected = this.naturesTab.getSelected(); - this._removeNatureTab(selected.getUserData("NATURE_CLASS")); + var tabClass = selected.getUserData("NATURE_CLASS"); + var user = this.getCurrentUser(); + user.removeNature(tabClass.NATURE_TYPE); + this.saveUser(); + this._removeNatureTab(tabClass); }, removeAllTabs : function(){ @@ -349,7 +384,19 @@ qx.Class.define("org.argeo.security.ria.UserEditorApplet", this._removeNatureTab(this.naturesTab.getSelected().getUserData("NATURE_CLASS")); } }, - + + _setGuiInCreateMode : function(bool){ + if(bool){ + if(!this.natureButtonGB.isVisible()) return; + this.natureButtonGB.hide(); + this.fakePane.setVisibility("excluded"); + }else{ + if(this.natureButtonGB.isVisible()) return; + this.natureButtonGB.show(); + this.fakePane.setVisibility("visible"); + } + }, + _attachListeners : function(){ this.usernameField.addListener("changeValue", function(){ this.setModified(true); @@ -375,30 +422,40 @@ qx.Class.define("org.argeo.security.ria.UserEditorApplet", if(value) this.getViewSelection().triggerEvent(); }, - loadUserData : function(user){ - this.setCurrentUser(user); - this.usernameField.setValue(user.getName()); - this.usernameField.setReadOnly(true); - this.rolesField.setValue(user.getRoles()); - var userNatureTabs = this.naturesManager.detectNaturesInData(user.getNatures()); - if(userNatureTabs.length){ - userNatureTabs.forEach(function(el){ - this._addNatureTab(el.NATURE_CLASS, el.NATURE_DATA); - }, this); - } + loadUserData : function(userName){ + var userDataService = org.argeo.security.ria.SecurityAPI.getUserDetailsService(userName); + userDataService.addListener("completed", function(response){ + var user = new org.argeo.security.ria.model.User(); + user.load(response.getContent(), "json"); + this.setCurrentUser(user); + this.usernameField.setValue(user.getName()); + this.usernameField.setReadOnly(true); + this.rolesField.setValue(user.getRoles()); + var userNatureTabs = this.naturesManager.detectNaturesInData(user.getNatures()); + if(userNatureTabs.length){ + userNatureTabs.forEach(function(el){ + this._addNatureTab(el.NATURE_CLASS, el.NATURE_DATA); + }, this); + } + this._attachListeners(); + }, this); + userDataService.send(); }, partialRefreshUser : function(user, target){ if(!qx.lang.Array.isArray(target)) target = [target]; if(qx.lang.Array.contains(target,"natures")){ + if(this.getSelectedNatureTab()){ + var selectedTab = this.getSelectedNatureTab().getUserData("NATURE_CLASS"); + } this.removeAllTabs(); var userNatureTabs = this.naturesManager.detectNaturesInData(user.getNatures()); if(userNatureTabs.length){ userNatureTabs.forEach(function(el){ - this._addNatureTab(el.NATURE_CLASS, el.NATURE_DATA); + this._addNatureTab(el.NATURE_CLASS, el.NATURE_DATA, (selectedTab && selectedTab == el.NATURE_CLASS)); }, this); - } + } } if(qx.lang.Array.contains(target,"details")){ this.setInstanceLabel("User "+user.getName()); @@ -412,12 +469,10 @@ qx.Class.define("org.argeo.security.ria.UserEditorApplet", * Load a given row : the data passed must be a simple data array. * @param data {Element} The text xml description. */ - load : function(user){ + load : function(userName){ if(this.getLoaded()){ return; - } - this.setRolesList(["ROLE_ADMIN", "ROLE_USER"]); - + } // MUST BE DONE AFTER COMMANDS ARE INITIALIZED! var commands = this.getCommands(); var saveButton = commands["save_user"].command.getFormButton(); @@ -442,11 +497,12 @@ qx.Class.define("org.argeo.security.ria.UserEditorApplet", this.natureButtonGB.add(natureButton); this.natureButtonGB.add(removeButton); - if(user){ - this.loadUserData(user); - this._attachListeners(); + if(userName){ + this.loadUserData(userName); + this._setGuiInCreateMode(false); }else{ this.setCurrentUser(new org.argeo.security.ria.model.User()); + this._setGuiInCreateMode(true); this._attachListeners(); this.setModified(true); }