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