Roles management working good
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.ria / src / argeo-ria-lib / security / class / org / argeo / security / ria / RolesApplet.js
index 6cd0b43995d850f8105841880830d933e8001704..65b5427b30259fbe5dc1a968744151cbad0d73f1 100644 (file)
@@ -37,6 +37,15 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
                                toolbar         : null,
                                callback        : function(e){
                                        // Prompt for new name
+                                       var modal = new org.argeo.ria.components.Modal();
+                                       modal.makePromptForm("Please enter a role name", function(roleName){
+                                               var service = org.argeo.security.ria.SecurityAPI.getCreateRoleService(roleName);
+                                               service.addListener("completed", function(response){
+                                                       this.loadRolesList();
+                                               }, this);
+                                               service.send();
+                                       }, this);
+                                       modal.attachAndShow();
                                },
                                command         : null
                        },      
@@ -49,9 +58,13 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
                                toolbar         : null,
                                callback        : function(e){
                                        // Call service to delete
-                                       var crtUsers = this.getViewSelection().getNodes();
-                                       for(var i=0;i<crtUsers.length;i++){
-                                               alert("Delete " + crtUsers[i]);
+                                       var roles = this.getViewSelection().getNodes();
+                                       for(var i=0;i<roles.length;i++){
+                                               var service = org.argeo.security.ria.SecurityAPI.getDeleteRoleService(roles[i]);
+                                               service.addListener("completed", function(response){
+                                                       this.loadRolesList();
+                                               }, this);
+                                               service.send();
                                        }
                                },
                                selectionChange : function(viewName, data){
@@ -86,6 +99,10 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
        guiMode : {
                apply : "_applyGuiMode"
        },
+       rolesList : {
+               check : "Array",
+               event : "changeRolesList"
+       },      
        chooserOriginalSelection : {},
        chooserSelectionModified : {
                init:false,
@@ -126,7 +143,7 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
                        this._selectionToValues(this.table.getSelectionModel(), this.getViewSelection());
                }, this);
                
-               this.rolesUsersStub = {"ROLE_ADMIN":["root","mbaudier"],"ROLE_USER":["mbaudier","cdujeu"]};
+               this.rolesUsersStub = {"ROLE_ADMIN":["gandalf"],"ROLE_USER":["demo","frodo","gandalf"]};
                                        
                this.toggleButton = new qx.ui.form.ToggleButton("Filter", "org.argeo.security.ria/go-next.png");
                this.toggleButton.set({
@@ -157,8 +174,14 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
                });
                
                this.saveButton.addListener("execute", function(){
-                       alert("Saving changes...");
-                       this.setGuiMode(this.initialState);
+                       if(!this.usersAppletReference){
+                               this.setGuiMode(this.initialState);
+                               return;
+                       }
+                       var newSelection = this.usersAppletReference.getViewSelection().getNodes();
+                       var diff = this._selectionDiff(this.getChooserOriginalSelection(), newSelection);
+                       this.saveRoleModifications(diff.deltaPlus, diff.deltaMinus);
+                       this.setGuiMode(this.initialState);
                }, this);
                this.cancelButton.addListener("execute", function(){
                        if(!this.getChooserSelectionModified()){
@@ -176,6 +199,11 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
                this.table.addListener("cellDblclick", function(cellEvent){
                        this.setGuiMode("edit");
                }, this);
+               this.addListener("changeRolesList", function(event){
+                       var data = [];
+                       event.getData().forEach(function(el){data.push([el]);});
+                       this.tableModel.setData(data);                                                  
+               }, this);
                
                this.setGuiMode("clear");
        },
@@ -224,13 +252,43 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
                }
        },
        
+       saveRoleModifications : function(deltaPlus, deltaMinus){
+               //console.log(deltaPlus);
+               //console.log(deltaMinus);
+               // LOAD CONCERNED USERS
+               var selectionModel = this.table.getSelectionModel();
+               if(!selectionModel.getSelectedCount()){
+                       return;
+               }
+               var roleValue = this._selectionToValues(selectionModel)[0];
+               
+               var users = deltaPlus.concat(deltaMinus);
+               for(var i=0;i<users.length;i++){
+                       var user = users[i];
+                       var userDetailService = org.argeo.security.ria.SecurityAPI.getUserDetailsService(users[i]);
+                       userDetailService.addListener("completed", function(response){
+                               var userRoles = response.getContent().roles;
+                               if(qx.lang.Array.contains(deltaPlus, user)){
+                                       userRoles.push(roleValue);
+                               }else if(qx.lang.Array.contains(deltaMinus, user)){
+                                       qx.lang.Array.remove(userRoles, roleValue);
+                               }
+                               var userSaveService = org.argeo.security.ria.SecurityAPI.getUpdateUserService(response.getContent());
+                               userSaveService.addListener("completed", function(e){
+                                       this.fireDataEvent("changeRolesList", this.getRolesList());
+                               }, this);
+                               userSaveService.send();
+                       }, this);
+                       userDetailService.send();
+               }
+       },
+       
        monitorChooserSelectionChanges : function(event){
                if(!this.usersAppletReference || this.getChooserSelectionModified()) return;
                var initialSelection = this.getChooserOriginalSelection();
                var crtSelection = event.getTarget().getNodes();
                if(!qx.lang.Array.equals(initialSelection.sort(), crtSelection.sort())){
-                       this.setChooserSelectionModified(true);
-                       console.log("Changed!");
+                       this.setChooserSelectionModified(true);                         
                        this.saveButton.setEnabled(true);
                }
        },
@@ -253,10 +311,11 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
                        return;
                }
                var uniqueValue = this._selectionToValues(selectionModel)[0];
-               var initSelection = this.rolesUsersStub[uniqueValue];
+               //var initSelection = this.rolesUsersStub[uniqueValue];
+               this.usersAppletReference.applySelection(uniqueValue, "roles");
+               var initSelection = this.usersAppletReference.getViewSelection().getNodes(); 
                this.setChooserOriginalSelection(initSelection);
                this.setChooserSelectionModified(false);
-               this.usersAppletReference.applySelection(initSelection, "username");
                this.saveButton.setEnabled(false);
        },
        
@@ -282,13 +341,19 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
                return values;
        },
        
+       _selectionDiff : function(initialSelection, modifiedSelection){
+               var deltaMinus = qx.lang.Array.clone(initialSelection);
+               var deltaPlus = qx.lang.Array.clone(modifiedSelection);
+               qx.lang.Array.exclude(deltaPlus, initialSelection);
+               qx.lang.Array.exclude(deltaMinus, modifiedSelection);
+               return {deltaPlus : deltaPlus, deltaMinus : deltaMinus};
+       },
+       
        /**
         * Load a given row : the data passed must be a simple data array.
         * @param data {Element} The text xml description. 
         */
-       load : function(){              
-               var data = [["ROLE_ADMIN"],["ROLE_USER"]];
-               this.tableModel.setData(data);
+       load : function(){
                
                var commands = this.getCommands();
                this.toolBarPart.add(commands["new_role"].command.getToolbarButton());
@@ -300,6 +365,17 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
                this.toolBar.add(this.cancelButton);                            
                this.toolBar.setShow("icon");
                
+               this.loadRolesList();
+               
+       },
+       
+       loadRolesList : function(){
+               this.setRolesList([]);
+               var service = org.argeo.security.ria.SecurityAPI.getListRolesService();
+               service.addListener("completed", function(response){
+                       this.setRolesList(response.getContent());
+               }, this);
+               service.send();                 
        },
                 
        addScroll : function(){