]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/runtime/org.argeo.security.ria/src/argeo-ria-lib/security/class/org/argeo/security/ria/components/UserEditor.js
add description field
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.ria / src / argeo-ria-lib / security / class / org / argeo / security / ria / components / UserEditor.js
1 /**
2 * A simple Hello World applet for documentation purpose.
3 * The only associated command is the "Close" command.
4 */
5 /* *************************************************
6 #asset(resource/org.argeo.security.ria/*)
7 ****************************************************/
8 qx.Class.define("org.argeo.security.ria.components.UserEditor",
9 {
10 extend : qx.ui.container.Composite,
11
12 construct : function(){
13 this.base(arguments);
14 this.setLayout(new qx.ui.layout.VBox(5));
15 },
16
17 events : {
18 "savedUser" : "qx.event.type.Data"
19 },
20
21 properties :
22 {
23 modified : {
24 init : false,
25 apply : "_applyDetailsModified"
26 },
27 naturesModified : {
28 init : false,
29 apply : "_applyNaturesModified"
30 },
31 rolesList : {
32
33 },
34 loaded : {
35 init : false
36 },
37 currentNatureTabs : {
38 },
39 availableNatures : {
40 },
41 selectedNatureTab : {
42 nullable : true
43 },
44 currentUser : {
45 }
46 },
47
48 members :
49 {
50 initGUI : function(ROLES_LIST){
51
52 this.naturesManager = new org.argeo.security.ria.components.NaturesManager();
53 var detectedNatures = this.naturesManager.getDetectedNatures();
54 this.setAvailableNatures(detectedNatures);
55
56 // TOOLBAR
57 this.buttonGB = new qx.ui.container.Composite(new qx.ui.layout.HBox(5, "right"));
58 this.add(this.buttonGB);
59
60 this.setCurrentNatureTabs([]);
61 this.naturesTab = new qx.ui.tabview.TabView("top");
62 this.naturesTab.addListener("changeSelection", function(e){
63 this.setSelectedNatureTab(e.getData()[0] || null);
64 }, this);
65
66 this.basicPage = new qx.ui.tabview.Page("Basic Information");
67 this.basicPage.setLayout(new qx.ui.layout.VBox(5));
68
69 // GROUPBOXES
70
71 this.basicGB = new qx.ui.groupbox.GroupBox("Base Informations");
72 var grid = new qx.ui.layout.Grid(5,5);
73 this.basicGB.setLayout(grid);
74 grid.setColumnFlex(0,1);
75 grid.setColumnAlign(0,"right", "middle");
76 grid.setColumnFlex(1,2);
77 this._initializeGroupBox(this.basicGB);
78
79 this.passGB = new qx.ui.groupbox.GroupBox("Set/Modify Password");
80 this.passGB.setLayout(new qx.ui.layout.VBox());
81 this._initializeGroupBox(this.passGB);
82
83 this.add(this.basicGB);
84 this.add(this.passGB);
85
86 // FIELDS
87 this.usernameField = new qx.ui.form.TextField();
88 this.basicGB.add(new qx.ui.basic.Label("Username"), {row:0,column:0});
89 this.basicGB.add(this.usernameField, {row:0,column:1});
90
91 this.rolesField = new org.argeo.ria.components.ui.MultipleComboBox();
92 if(ROLES_LIST){
93 this.rolesField.setChoiceValues(ROLES_LIST);
94 }
95 this.basicGB.add(new qx.ui.basic.Label("Roles"), {row:1,column:0});
96 this.basicGB.add(this.rolesField, {row:1,column:1});
97
98 this.passPane = new org.argeo.security.ria.components.PasswordCredentialImpl();
99 this.passGB.add(this.passPane.getContainer());
100
101 //this.naturesTab.add(this.basicPage);
102 this.natureButtonGB = new qx.ui.container.Composite(new qx.ui.layout.HBox(5, "right"));
103 this.natureButtonGB.setMarginTop(15);
104 this.add(this.natureButtonGB);
105
106
107 this.add(this.naturesTab, {flex:1});
108
109 this.naturesTab.setVisibility("excluded");
110 this.fakePane = new qx.ui.container.Composite(new qx.ui.layout.Canvas());
111 this.fakePane.setVisibility("visible");
112 this.fakePane.setDecorator("tabview-pane");
113 this.fakePane.setMarginTop(30);
114 this.add(this.fakePane, {flex:1});
115
116 title = new qx.ui.basic.Atom("User Details", "org.argeo.security.ria/preferences-users.png");
117 title.setFont(qx.bom.Font.fromString("16px sans-serif bold"));
118 this.buttonGB.add(title);
119 this.buttonGB.add(new qx.ui.core.Spacer(), {flex:1});
120
121 var title2 = new qx.ui.basic.Atom("User Natures", "org.argeo.security.ria/identity.png");
122 title2.setFont(qx.bom.Font.fromString("16px sans-serif bold"));
123 this.natureButtonGB.add(title2);
124 this.natureButtonGB.add(new qx.ui.core.Spacer(), {flex:1});
125
126
127 },
128
129 saveUser : function(){
130 var user = this.getCurrentUser();
131 if(this.basicGB.getVisibility()!= "excluded"){
132 user.setName(this.usernameField.getValue());
133 var roles = this.rolesField.getValue();
134 if(roles && roles != ""){
135 user.setRoles(roles.split(","));
136 }else{
137 user.setRoles([]);
138 }
139 }
140
141 // GO TO AND RETURN FROM SERVER
142 if(user.isCreate()){
143 if(!this.passPane.validate()){
144 this.error("Warning, passwords differ!");
145 return;
146 }
147 user.setPassword(this.passPane.getData());
148 var create = true;
149 var userExists = false;
150 var req = org.argeo.security.ria.SecurityAPI.getUserExistsService(user.getName());
151 req.addListener("completed", function(response){
152 userExists = response.getContent().value;
153 }, this);
154 req.setAsynchronous(false);
155 req.send();
156 if(userExists){
157 this.error("User already exists, choose another name!");
158 return;
159 }
160 }else{
161 var pass = this.passPane.getData();
162 if(pass != null && !this.passPane.validate()){
163 this.error("Warning, passwords differ!");
164 return;
165 }
166 }
167 this.passPane.clear();
168 var saveCompletedCallback = qx.lang.Function.bind(function(){
169 this._setGuiInCreateMode(false);
170 this.partialRefreshUser(user, ["details","natures"]);
171 this.setModified(false);
172 this.fireDataEvent("savedUser", user);
173 }, this);
174 var userService = user.getSaveService();
175 userService.addListener("completed", function(response){
176 if(response.getContent().status && response.getContent().status == "ERROR"){
177 return;
178 }
179 user.load(response.getContent(), "json");
180 if(pass!=null){
181 var passService = org.argeo.security.ria.SecurityAPI.getUpdateUserPassService(user.getName(), pass);
182 passService.addListener("completed", function(response){
183 if(response.getContent().status){
184 this.info(response.getContent().message);
185 }
186 saveCompletedCallback();
187 }, this);
188 passService.send();
189 }else{
190 saveCompletedCallback();
191 }
192 }, this);
193 userService.send();
194 },
195
196 _addNatureTab : function(natureClass, natureData, select){
197 var crtTabs = this.getCurrentNatureTabs();
198 if(qx.lang.Array.contains(crtTabs, natureClass.NATURE_TYPE)){
199 this.error("There can only be one instance of a given nature type!");
200 return null;
201 }
202 if(!this.naturesTab.isVisible()){
203 if(this.fakePane) this.fakePane.setVisibility("excluded");
204 this.naturesTab.setVisibility("visible");
205 }
206 var page = new qx.ui.tabview.Page("Nature : " + natureClass.NATURE_LABEL);
207 page.setLayout(new qx.ui.layout.Dock());
208 page.setUserData("NATURE_CLASS", natureClass);
209 var newClass = new natureClass();
210 page.add(newClass.getContainer(), {edge:"center"});
211
212 buttons = new qx.ui.container.Composite(new qx.ui.layout.HBox(5, "center"));
213 var editB = new qx.ui.form.Button("Edit this Nature", "org.argeo.security.ria/document-properties-22.png");
214 var saveB = new qx.ui.form.Button("Save", "org.argeo.security.ria/dialog-apply.png");
215 var cancelB = new qx.ui.form.Button("Cancel", "org.argeo.security.ria/dialog-cancel.png");
216 buttons.add(editB);
217 buttons.add(saveB);
218 buttons.add(cancelB);
219 page.add(buttons, {edge:"south"});
220 editB.addListener("execute", function(){
221 newClass.setEditMode(true);
222 editB.setVisibility("excluded");
223 saveB.setVisibility("visible");
224 cancelB.setVisibility("visible");
225 });
226 cancelB.addListener("execute", function(){
227 if(newClass.getIsNew()){
228 this._removeNatureTab(natureClass);
229 }
230 newClass.setEditMode(false);
231 editB.setVisibility("visible");
232 saveB.setVisibility("excluded");
233 cancelB.setVisibility("excluded");
234 }, this);
235 saveB.addListener("execute", function(){
236 // SAVE CURRENT NATURE
237 var data = newClass.getData();
238 if(newClass.getIsNew()){
239 this.getCurrentUser().addNature(data);
240 }else{
241 this.getCurrentUser().updateNature(data);
242 }
243 this.saveUser();
244 this.setNaturesModified(false);
245 newClass.setEditMode(false);
246 editB.setVisibility("visible");
247 saveB.setVisibility("excluded");
248 cancelB.setVisibility("excluded");
249 }, this);
250 if(natureData){
251 newClass.setData(natureData);
252 cancelB.execute();
253 }else{
254 newClass.setIsNew(true);
255 editB.execute();
256 }
257 this.naturesTab.add(page);
258 crtTabs.push(natureClass.NATURE_TYPE);
259 newClass.addListener("modified", function(){
260 this.setNaturesModified(true);
261 }, this);
262 if(select){
263 this.naturesTab.setSelection([page]);
264 }
265 return page;
266 },
267
268 _removeNatureTab : function(natureClass){
269 this.naturesTab.getChildren().forEach(function(el){
270 if(el.getUserData("NATURE_CLASS") == natureClass){
271 this.naturesTab.remove(el);
272 qx.lang.Array.remove(this.getCurrentNatureTabs(), natureClass.NATURE_TYPE);
273 }
274 }, this);
275 if(this.naturesTab.getChildren().length == 0){
276 this.naturesTab.setVisibility("excluded");
277 this.fakePane.setVisibility("visible");
278 }
279 },
280
281 removeSelectedTab : function(){
282 if(this.naturesTab.isSelectionEmpty()) return;
283 var selected = this.naturesTab.getSelection()[0];
284 var tabClass = selected.getUserData("NATURE_CLASS");
285 var user = this.getCurrentUser();
286 user.removeNature(tabClass.NATURE_TYPE);
287 this.saveUser();
288 this._removeNatureTab(tabClass);
289 },
290
291 removeAllTabs : function(){
292 while(!this.naturesTab.isSelectionEmpty()){
293 this._removeNatureTab(this.naturesTab.getSelection()[0].getUserData("NATURE_CLASS"));
294 }
295 },
296
297 _setGuiInCreateMode : function(bool){
298 if(bool){
299 if(!this.natureButtonGB.isVisible()) return;
300 this.natureButtonGB.hide();
301 this.fakePane.setVisibility("excluded");
302 }else{
303 if(this.natureButtonGB.isVisible()) return;
304 this.natureButtonGB.show();
305 this.fakePane.setVisibility("visible");
306 }
307 },
308
309 _attachListeners : function(){
310 this.usernameField.addListener("changeValue", function(){
311 this.setModified(true);
312 }, this);
313 this.rolesField.addListener("changeValue", function(){
314 this.setModified(true);
315 }, this);
316 this.passPane.addListener("modified", function(){
317 this.setModified(true);
318 }, this);
319 },
320
321 _initializeGroupBox: function(groupBox){
322 groupBox.setPadding(0);
323 groupBox.getChildrenContainer().setPadding(8);
324 },
325
326 loadUserData : function(userName){
327 var userDataService = org.argeo.security.ria.SecurityAPI.getUserDetailsService(userName);
328 userDataService.addListener("completed", function(response){
329 var user = new org.argeo.security.ria.model.User();
330 user.load(response.getContent(), "json");
331 this.setCurrentUser(user);
332 this.usernameField.setValue(user.getName());
333 this.usernameField.setReadOnly(true);
334 this.rolesField.setValue(user.getRoles());
335 var userNatureTabs = this.naturesManager.detectNaturesInData(user.getNatures());
336 if(userNatureTabs.length){
337 userNatureTabs.forEach(function(el){
338 this._addNatureTab(el.NATURE_CLASS, el.NATURE_DATA);
339 }, this);
340 }
341 this._attachListeners();
342 }, this);
343 userDataService.send();
344 },
345
346 clearUserData : function(){
347 this.usernameField.setValue("");
348 this.rolesField.setValue([]);
349 this.removeAllTabs();
350 },
351
352 partialRefreshUser : function(user, target){
353 if(!qx.lang.Type.isArray(target)) target = [target];
354
355 if(qx.lang.Array.contains(target,"natures")){
356 if(this.getSelectedNatureTab()){
357 var selectedTab = this.getSelectedNatureTab().getUserData("NATURE_CLASS");
358 }
359 this.removeAllTabs();
360 var userNatureTabs = this.naturesManager.detectNaturesInData(user.getNatures());
361 if(userNatureTabs.length){
362 userNatureTabs.forEach(function(el){
363 this._addNatureTab(el.NATURE_CLASS, el.NATURE_DATA, (selectedTab && selectedTab == el.NATURE_CLASS));
364 }, this);
365 }
366 }
367 if(qx.lang.Array.contains(target,"details")){
368 this.usernameField.setValue(user.getName());
369 this.rolesField.setValue(user.getRoles());
370 this.usernameField.setReadOnly(true);
371 this.fireEvent("saveUser");
372 }
373 },
374
375 _applyDetailsModified : function(value){},
376
377 _applyNaturesModified : function(value){}
378
379 }
380 });