Password impl, various enhancements
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.ria / src / argeo-ria-lib / security / class / org / argeo / security / ria / RolesApplet.js
index 3803968a9a9a797f33d730eb131dd3f1a4d0dbfb..429460b3700d2258910ad6b3ed3f57bed0da3de7 100644 (file)
@@ -28,13 +28,75 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
         */
        commands : {
                init : {
+                       "reload" : {
+                               label           : "Reload Data",
+                               icon            : "org.argeo.security.ria/view-refresh.png",
+                               shortcut        : "Control+h",
+                               enabled         : true,
+                               menu            : "Roles",
+                               toolbar         : "roles",
+                               callback        : function(e){
+                                       this.loadRolesList();
+                               },
+                               command         : null
+                       },                      
+                       "new_role" : {
+                               label           : "Create Role", 
+                               icon            : "org.argeo.security.ria/list-add.png",
+                               shortcut        : null,
+                               enabled         : true,
+                               menu            : "Roles",
+                               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
+                       },      
+                       "delete_role" : {
+                               label           : "Delete Role", 
+                               icon            : "org.argeo.security.ria/list-remove.png",
+                               shortcut        : null,
+                               enabled         : true,
+                               menu            : "Roles",
+                               toolbar         : null,
+                               callback        : function(e){
+                                       // Call service to delete
+                                       var roles = this.getViewSelection().getNodes();
+                                       var modal = new org.argeo.ria.components.Modal("Delete");                                       
+                                       modal.addConfirm("Are you sure you want to delete the selected roles?");
+                                       modal.addListener("ok", function(e){
+                                               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();
+                                               }
+                                       }, this);
+                                       modal.attachAndShow();                                  
+                               },
+                               selectionChange : function(viewName, data){
+                                       if(viewName != "roles") return;
+                                       this.setEnabled(!(data == null || !data.length));                                       
+                               },
+                               command         : null
+                       },                                              
                        "edit_role" : {
                                label           : "Edit Role", 
-                               icon            : "ria/window-close.png",
+                               icon            : "org.argeo.security.ria/document-properties.png",
                                shortcut        : "Control+r",
                                enabled         : true,
                                menu            : "Roles",
-                               toolbar         : "role",
+                               toolbar         : null,
                                callback        : function(e){
                                        // Call service to delete
                                        this.setGuiMode("edit");
@@ -54,6 +116,10 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
        guiMode : {
                apply : "_applyGuiMode"
        },
+       rolesList : {
+               check : "Array",
+               event : "changeRolesList"
+       },      
        chooserOriginalSelection : {},
        chooserSelectionModified : {
                init:false,
@@ -71,7 +137,13 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
         */
        init : function(viewPane){
                this.setView(viewPane);
-               this.setViewSelection(new org.argeo.ria.components.ViewSelection(viewPane.getViewId()));                
+               this.setViewSelection(new org.argeo.ria.components.ViewSelection(viewPane.getViewId()));
+               
+               this.toolBar = new qx.ui.toolbar.ToolBar();
+               this.toolBarPart = new qx.ui.toolbar.Part();
+               this.toolBar.add(this.toolBarPart);             
+               viewPane.add(this.toolBar);
+               
                this.tableModel = new qx.ui.table.model.Simple();
                this.tableModel.setColumns(["Role Name"]);
                this.table = new qx.ui.table.Table(this.tableModel, {
@@ -88,54 +160,67 @@ 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"]};
-                               
-               var buttonPane = new qx.ui.container.Composite(new qx.ui.layout.HBox(2, "right"));
-               this.getView().header.setPadding(0);
-               if(this.getView().headerLabel) this.getView().headerLabel.setMargin(8);
-               this.getView().header.add(buttonPane, {edge:"east"});
-       
-               this.toggleButton = new qx.ui.form.ToggleButton("Filter", "ria/go-right.png");
+               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({
                        show:"icon",
                        margin:2,
                        toolTip :new qx.ui.tooltip.ToolTip("Apply automatic filtering on Users list")                   
                });
-               buttonPane.add(this.toggleButton);
                                
                // TOGGLE THE GUI MODES                 
                this.toggleButton.addListener("changeChecked", function(event){
                        this.setGuiMode(event.getData()?"filter":"clear");
                }, this);
                
-               this.saveButton = new qx.ui.form.Button("Save");
+               this.saveButton = new qx.ui.form.Button("Save", "org.argeo.security.ria/document-save.png");
                this.saveButton.set({
-                       show:"label",
+                       show:"icon",
                        margin:2,
                        toolTip :new qx.ui.tooltip.ToolTip("Save changes"),
                        visibility : "excluded"
                });
-               buttonPane.add(this.saveButton);                
                
-               this.cancelButton = new qx.ui.form.Button("Cancel");
+               this.cancelButton = new qx.ui.form.Button("Cancel", "org.argeo.security.ria/window-close.png");
                this.cancelButton.set({
-                       show:"label",
+                       show:"icon",
                        margin:2,
                        toolTip :new qx.ui.tooltip.ToolTip("Cancel changes"),
                        visibility : "excluded"                         
                });
-               buttonPane.add(this.cancelButton);              
                
-               var listener = function(){
-                       this.setGuiMode(this.initialState);
-               };
-               this.saveButton.addListener("execute", listener, this);
-               this.cancelButton.addListener("execute", listener, this);
+               this.saveButton.addListener("execute", function(){
+                       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()){
+                               this.setGuiMode(this.initialState);
+                               return;
+                       }
+                       var modal = new org.argeo.ria.components.Modal("Warning");
+                       modal.addConfirm("There are unsaved changes!\n Are you sure you want to close?");
+                       modal.addListener("ok", function(){
+                               this.setGuiMode(this.initialState);
+                       }, this);
+                       modal.attachAndShow();                  
+               }, this);
                
                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");
        },
@@ -159,7 +244,13 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
                                viewSel.removeListener("changeSelection", this.monitorChooserSelectionChanges, this);                   
                        }
                        selectionModel.addListener("changeSelection", this.selectionToFilter, this);
+                       if(selectionModel.getSelectedCount()){
+                               var orig = selectionModel.getSelectedRanges()[0].minIndex;
+                       }
                        selectionModel.setSelectionMode(qx.ui.table.selection.Model.MULTIPLE_INTERVAL_SELECTION_TOGGLE);
+                       if(orig){
+                               selectionModel.addSelectionInterval(orig, orig);
+                       }
                        this.selectionToFilter();                       
                }else if(guiMode == "edit"){
                        if(!this.usersAppletReference) return;
@@ -180,17 +271,55 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
                        }
                        this.table.setEnabled(true);
                        selectionModel.removeListener("changeSelection", this.selectionToFilter, this);
+                       if(selectionModel.getSelectedCount()){
+                               var orig = selectionModel.getSelectedRanges()[0].minIndex;
+                       }
                        selectionModel.setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);                          
+                       if(orig){
+                               selectionModel.addSelectionInterval(orig, orig);
+                       }
                }
        },
        
+       saveRoleModifications : function(deltaPlus, deltaMinus){
+               // LOAD CONCERNED USERS
+               var selectionModel = this.table.getSelectionModel();
+               if(!selectionModel.getSelectedCount()){
+                       return;
+               }
+               var roleValue = this._selectionToValues(selectionModel)[0];
+               
+               var users = deltaPlus.concat(deltaMinus);
+               var modal = new org.argeo.ria.components.Modal("Batch Update", "", "Please wait, updating roles for selected users");
+               modal.attachAndShow();
+               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.setAsynchronous(false);
+                               userSaveService.send();
+                       }, this);
+                       userDetailService.setAsynchronous(false);
+                       userDetailService.send();
+               }
+               this.fireDataEvent("changeRolesList", this.getRolesList());
+               modal.hide();
+               modal.destroy();
+       },
+       
        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);
                }
        },
@@ -213,10 +342,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);
        },
        
@@ -242,13 +372,41 @@ 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());
+               this.toolBarPart.add(commands["delete_role"].command.getToolbarButton());
+               this.toolBarPart.add(commands["edit_role"].command.getToolbarButton());                 
+               this.toolBar.addSpacer();
+               this.toolBar.add(this.toggleButton);
+               this.toolBar.add(this.saveButton);              
+               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(){