Improve security UI
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 20 Jan 2011 22:21:03 +0000 (22:21 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 20 Jan 2011 22:21:03 +0000 (22:21 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@4060 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

16 files changed:
security/eclipse/plugins/org.argeo.security.ui/META-INF/spring/commands.xml
security/eclipse/plugins/org.argeo.security.ui/META-INF/spring/views.xml
security/eclipse/plugins/org.argeo.security.ui/icons/add.gif [new file with mode: 0644]
security/eclipse/plugins/org.argeo.security.ui/icons/remove.gif [new file with mode: 0644]
security/eclipse/plugins/org.argeo.security.ui/plugin.xml
security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/SecurityPerspective.java
security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/SecurityUiPlugin.java
security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/commands/AddRole.java [new file with mode: 0644]
security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/commands/OpenArgeoUserEditor.java
security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/commands/SaveArgeoUser.java [new file with mode: 0644]
security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/dialogs/DefaultLoginDialog.java
security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/editors/ArgeoUserEditor.java
security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/editors/DefaultUserMainPage.java
security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/views/RolesView.java [new file with mode: 0644]
security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/views/UsersView.java
security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/ArgeoSecurityDao.java

index 4cee31dbae34e777254fa1cceb85ef5771e85643..121eb3dc90ea1f7ffb9172bfa816c40db561557d 100644 (file)
@@ -5,6 +5,11 @@
         http://www.springframework.org/schema/beans/spring-beans.xsd">
 
        <bean id="org.argeo.security.ui.openArgeoUserEditor" class="org.argeo.security.ui.commands.OpenArgeoUserEditor"
+               scope="prototype" />
+       <bean id="org.argeo.security.ui.saveArgeoUser" class="org.argeo.security.ui.commands.SaveArgeoUser"
+               scope="prototype" />
+       <bean id="org.argeo.security.ui.addRole" class="org.argeo.security.ui.commands.AddRole"
                scope="prototype">
+               <property name="securityService" ref="securityService" />
        </bean>
 </beans>
index 2cd8d033f899b63a0ab3e3567fc61fe2c84cb550..7ae3b62e360cafd59c6c9c5e3c3e54e3c27c745c 100644 (file)
@@ -8,6 +8,10 @@
                scope="prototype">
                <property name="securityService" ref="securityService" />
        </bean>
+       <bean id="org.argeo.security.ui.rolesView" class="org.argeo.security.ui.views.RolesView"
+               scope="prototype">
+               <property name="securityService" ref="securityService" />
+       </bean>
        <bean id="org.argeo.security.ui.currentUserView" class="org.argeo.security.ui.views.CurrentUserView"
                scope="prototype">
        </bean>
diff --git a/security/eclipse/plugins/org.argeo.security.ui/icons/add.gif b/security/eclipse/plugins/org.argeo.security.ui/icons/add.gif
new file mode 100644 (file)
index 0000000..252d7eb
Binary files /dev/null and b/security/eclipse/plugins/org.argeo.security.ui/icons/add.gif differ
diff --git a/security/eclipse/plugins/org.argeo.security.ui/icons/remove.gif b/security/eclipse/plugins/org.argeo.security.ui/icons/remove.gif
new file mode 100644 (file)
index 0000000..0ae6dec
Binary files /dev/null and b/security/eclipse/plugins/org.argeo.security.ui/icons/remove.gif differ
index cadb18f2628225bb86f77ea8f9cd7d52db173ec8..fbb88b0bb168b76da27bc4e84015fb047a2cf263 100644 (file)
             name="Current User"
             restorable="false">
       </view>
+      <view
+            class="org.argeo.eclipse.spring.SpringExtensionFactory"
+            icon="icons/role.gif"
+            id="org.argeo.security.ui.rolesView"
+            name="Roles"
+            restorable="false">
+      </view>
    </extension>
    
    <!-- Security -->
                        name="Username">
                        </commandParameter>
       </command>
+      <command
+            defaultHandler="org.argeo.eclipse.spring.SpringCommandHandler"
+            id="org.argeo.security.ui.saveArgeoUser"
+            name="SaveArgeoUser">
+      </command>
+      <command
+            defaultHandler="org.argeo.eclipse.spring.SpringCommandHandler"
+            id="org.argeo.security.ui.addRole"
+            name="AddRole">
+      </command>
     </extension>
+     <extension
+         id="menu:org.eclipse.ui.main.menu"
+         point="org.eclipse.ui.menus">
+       <menuContribution
+            locationURI="toolbar:org.argeo.security.ui.rolesView">
+            <command
+                  commandId="org.argeo.security.ui.addRole"
+                  icon="icons/add.gif"
+                  label="Add Role"
+                  tooltip="Add new role">
+            </command>
+        </menuContribution>
+        <menuContribution
+                allPopups="false"
+                locationURI="toolbar:org.eclipse.ui.main.toolbar">
+             <toolbar
+                   id="org.argeo.security.ui.mainToolbar">
+                <command
+                      commandId="org.argeo.security.ui.saveArgeoUser"
+                      disabledIcon="icons/user.gif"
+                      icon="icons/user.gif"
+                      label="Save user"
+                      style="push"
+                      tooltip="Save user">
+                </command>
+             </toolbar>
+          </menuContribution>
+  </extension>
  </plugin>
index f15c4e7d95bdf436eb8f96abe6f7d066e50d4fde..63e327eee5bbfcf8d526e542c2b3b264b4fc1c95 100644 (file)
@@ -12,9 +12,10 @@ public class SecurityPerspective implements IPerspectiveFactory {
                layout.setFixed(false);
 
                IFolderLayout left = layout.createFolder("left", IPageLayout.LEFT,
-                               0.3f, editorArea);
+                               0.4f, editorArea);
 
                left.addView("org.argeo.security.ui.usersView");
+               left.addView("org.argeo.security.ui.rolesView");
                left.addView("org.argeo.security.ui.currentUserView");
        }
 
index 5e165ad4f4726a21fd8793ab1ccfaec3631e2f60..971affa4b62366a42fa2c86447e44fa3e019dedb 100644 (file)
@@ -1,5 +1,10 @@
 package org.argeo.security.ui;
 
+import org.argeo.ArgeoException;
+import org.argeo.security.ArgeoUser;
+import org.argeo.security.UserNature;
+import org.argeo.security.nature.SimpleUserNature;
+import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.ui.plugin.AbstractUIPlugin;
 import org.osgi.framework.BundleContext;
 
@@ -13,7 +18,7 @@ public class SecurityUiPlugin extends AbstractUIPlugin {
 
        // The shared instance
        private static SecurityUiPlugin plugin;
-       
+
        /**
         * The constructor
         */
@@ -22,7 +27,10 @@ public class SecurityUiPlugin extends AbstractUIPlugin {
 
        /*
         * (non-Javadoc)
-        * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
+        * 
+        * @see
+        * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
+        * )
         */
        public void start(BundleContext context) throws Exception {
                super.start(context);
@@ -31,7 +39,10 @@ public class SecurityUiPlugin extends AbstractUIPlugin {
 
        /*
         * (non-Javadoc)
-        * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
+        * 
+        * @see
+        * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
+        * )
         */
        public void stop(BundleContext context) throws Exception {
                plugin = null;
@@ -40,11 +51,33 @@ public class SecurityUiPlugin extends AbstractUIPlugin {
 
        /**
         * Returns the shared instance
-        *
+        * 
         * @return the shared instance
         */
        public static SecurityUiPlugin getDefault() {
                return plugin;
        }
 
+       public static ImageDescriptor getImageDescriptor(String path) {
+               return imageDescriptorFromPlugin(PLUGIN_ID, path);
+       }
+
+       /*
+        * SECURITY UTILITIES
+        */
+       public final static SimpleUserNature findSimpleUserNature(ArgeoUser user,
+                       String simpleNatureType) {
+               SimpleUserNature simpleNature = null;
+               if (simpleNatureType != null)
+                       simpleNature = (SimpleUserNature) user.getUserNatures().get(
+                                       simpleNatureType);
+               else
+                       for (UserNature userNature : user.getUserNatures().values())
+                               if (userNature instanceof SimpleUserNature)
+                                       simpleNature = (SimpleUserNature) userNature;
+
+               if (simpleNature == null)
+                       throw new ArgeoException("No simple user nature in user " + user);
+               return simpleNature;
+       }
 }
diff --git a/security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/commands/AddRole.java b/security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/commands/AddRole.java
new file mode 100644 (file)
index 0000000..4581e82
--- /dev/null
@@ -0,0 +1,40 @@
+package org.argeo.security.ui.commands;
+
+import org.argeo.ArgeoException;
+import org.argeo.security.ArgeoSecurityService;
+import org.argeo.security.ui.views.RolesView;
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.ui.handlers.HandlerUtil;
+
+/** Add a new role. */
+public class AddRole extends AbstractHandler {
+       public final static String COMMAND_ID = "org.argeo.security.ui.addRole";
+       private ArgeoSecurityService securityService;
+       private String rolePrefix = "ROLE_";
+
+       public Object execute(ExecutionEvent event) throws ExecutionException {
+               RolesView rolesView = (RolesView) HandlerUtil
+                               .getActiveWorkbenchWindow(event).getActivePage()
+                               .findView(RolesView.ID);
+               String role = rolesView.getNewRole();
+               if (role.trim().equals(""))
+                       return null;
+               if (role.equals(rolesView.getAddNewRoleText()))
+                       return null;
+               role = role.trim().toUpperCase();
+               if (!role.startsWith(rolePrefix))
+                       role = rolePrefix + role;
+               if (securityService.getSecurityDao().listEditableRoles().contains(role))
+                       throw new ArgeoException("Role " + role + " already exists");
+               securityService.newRole(role);
+               rolesView.refresh();
+               return null;
+       }
+
+       public void setSecurityService(ArgeoSecurityService securityService) {
+               this.securityService = securityService;
+       }
+
+}
index 747d21a7e647de20d19d47fe50c531d184d7a8f7..c9bd25f548e5ccb55d447fd1f3a3c9d71bebbcc7 100644 (file)
@@ -21,7 +21,7 @@ public class OpenArgeoUserEditor extends AbstractHandler {
                                        event).getActivePage();
                        activePage.openEditor(editorInput, ArgeoUserEditor.ID);
                } catch (Exception e) {
-                       e.printStackTrace();
+                       throw new ExecutionException("Cannot open editor", e);
                }
                return null;
        }
diff --git a/security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/commands/SaveArgeoUser.java b/security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/commands/SaveArgeoUser.java
new file mode 100644 (file)
index 0000000..9756872
--- /dev/null
@@ -0,0 +1,28 @@
+package org.argeo.security.ui.commands;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.handlers.HandlerUtil;
+
+/** Save the currently edited Argeo user. */
+public class SaveArgeoUser extends AbstractHandler {
+       public final static String COMMAND_ID = "org.argeo.security.ui.saveArgeoUser";
+
+       public Object execute(ExecutionEvent event) throws ExecutionException {
+               try {
+                       IWorkbenchPart iwp = HandlerUtil.getActiveWorkbenchWindow(event)
+                                       .getActivePage().getActivePart();
+
+                       if (!(iwp instanceof IEditorPart))
+                               return null;
+                       IEditorPart editor = (IEditorPart) iwp;
+                       editor.doSave(null);
+               } catch (Exception e) {
+                       throw new ExecutionException("Cannot save user", e);
+               }
+               return null;
+       }
+}
index b0a7e05073d93bdbf6006af717b4a9c360681e01..d5d60d0c7efb514024e38680a791a10446c4d0aa 100644 (file)
@@ -30,14 +30,15 @@ public class DefaultLoginDialog extends AbstractLoginDialog {
        }
 
        protected Point getInitialSize() {
-               return new Point(300, 280);
+               return new Point(300, 200);
        }
 
        protected Control createDialogArea(Composite parent) {
                Composite dialogarea = (Composite) super.createDialogArea(parent);
-               dialogarea.setLayoutData(new GridData(GridData.FILL_BOTH));
+               dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
                Composite composite = new Composite(dialogarea, SWT.NONE);
                composite.setLayout(new GridLayout(2, false));
+               composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
                createCallbackHandlers(composite);
                return composite;
        }
@@ -63,7 +64,8 @@ public class DefaultLoginDialog extends AbstractLoginDialog {
                label.setText(callback.getPrompt());
                final Text passwordText = new Text(composite, SWT.SINGLE | SWT.LEAD
                                | SWT.PASSWORD | SWT.BORDER);
-               //passwordText.setLayoutData(new GridData(GridData.FILL_BOTH));
+               passwordText
+                               .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
                passwordText.addModifyListener(new ModifyListener() {
 
                        public void modifyText(ModifyEvent event) {
@@ -78,7 +80,7 @@ public class DefaultLoginDialog extends AbstractLoginDialog {
                label.setText(callback.getPrompt());
                final Text text = new Text(composite, SWT.SINGLE | SWT.LEAD
                                | SWT.BORDER);
-               //text.setLayoutData(new GridData(GridData.FILL_BOTH));
+               text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
                text.addModifyListener(new ModifyListener() {
 
                        public void modifyText(ModifyEvent event) {
index 9e258a65f8dbcf2cf471adcb2422c3efc9e7051f..256016f85b8fcc201010f30c7a9b9de9e545f013 100644 (file)
@@ -1,17 +1,23 @@
 package org.argeo.security.ui.editors;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.argeo.ArgeoException;
 import org.argeo.security.ArgeoSecurityService;
 import org.argeo.security.ArgeoUser;
-import org.argeo.security.UserNature;
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorSite;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.forms.editor.FormEditor;
+import org.eclipse.ui.forms.widgets.FormToolkit;
 
 /** Editor for an Argeo user. */
 public class ArgeoUserEditor extends FormEditor {
+       private final static Log log = LogFactory.getLog(ArgeoUserEditor.class);
+
        public final static String ID = "org.argeo.security.ui.argeoUserEditor";
 
        private ArgeoUser user;
@@ -28,8 +34,8 @@ public class ArgeoUserEditor extends FormEditor {
 
        protected void addPages() {
                try {
-                       addPage(new DefaultUserMainPage(this, user));
-                               
+                       addPage(new DefaultUserMainPage(this, securityService, user));
+
                } catch (PartInitException e) {
                        throw new ArgeoException("Not able to add page ", e);
                }
@@ -37,24 +43,30 @@ public class ArgeoUserEditor extends FormEditor {
 
        @Override
        public void doSave(IProgressMonitor monitor) {
-               // TODO Auto-generated method stub
-
+               log.debug("doSave called");
+               securityService.updateUser(user);
        }
 
        @Override
        public void doSaveAs() {
-               // TODO Auto-generated method stub
-
        }
 
        @Override
        public boolean isSaveAsAllowed() {
-               // TODO Auto-generated method stub
                return false;
        }
 
        public void setSecurityService(ArgeoSecurityService securityService) {
                this.securityService = securityService;
        }
+       
+       
+       private class DirtyListener implements ModifyListener {
+
+               public void modifyText(ModifyEvent e) {
+                       
+               }
+               
+       }
 
 }
index 5495d18d27f95e6ea1a98fb1ce80b36e3ef498bb..7b3ce5c95245bb5e97bb992c45a650a1090b5a33 100644 (file)
@@ -1,16 +1,28 @@
 package org.argeo.security.ui.editors;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.argeo.ArgeoException;
+import org.argeo.security.ArgeoSecurityService;
 import org.argeo.security.ArgeoUser;
-import org.argeo.security.UserNature;
 import org.argeo.security.nature.SimpleUserNature;
+import org.argeo.security.ui.SecurityUiPlugin;
+import org.eclipse.jface.viewers.CellEditor;
+import org.eclipse.jface.viewers.CheckboxCellEditor;
+import org.eclipse.jface.viewers.ColumnLabelProvider;
+import org.eclipse.jface.viewers.EditingSupport;
+import org.eclipse.jface.viewers.IStructuredContentProvider;
+import org.eclipse.jface.viewers.ITableLabelProvider;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.TableViewerColumn;
+import org.eclipse.jface.viewers.Viewer;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.graphics.Image;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Combo;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Table;
+import org.eclipse.swt.widgets.TableColumn;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.forms.IManagedForm;
 import org.eclipse.ui.forms.editor.FormEditor;
@@ -19,82 +31,215 @@ import org.eclipse.ui.forms.widgets.ColumnLayout;
 import org.eclipse.ui.forms.widgets.FormToolkit;
 import org.eclipse.ui.forms.widgets.ScrolledForm;
 
-/**
- * 
- * @author bsinou
- * 
- *         This page display main info of a specified Ebi. Roles enable the
- */
 public class DefaultUserMainPage extends FormPage {
-       private final static Log log = LogFactory.getLog(DefaultUserMainPage.class);
+       // We use icons
+       private static final Image CHECKED = SecurityUiPlugin.getImageDescriptor(
+                       "icons/security.gif").createImage();
 
        private ArgeoUser user;
        private SimpleUserNature simpleNature;
 
        private String simpleNatureType;
 
+       private Text firstName;
+       private Text lastName;
        private Text email;
        private Text description;
+       private TableViewer rolesViewer;
 
-       public DefaultUserMainPage(FormEditor editor, ArgeoUser user) {
+       private ArgeoSecurityService securityService;
+
+       public DefaultUserMainPage(FormEditor editor,
+                       ArgeoSecurityService securityService, ArgeoUser user) {
                super(editor, "argeoUserEditor.mainPage", "Main");
+               this.securityService = securityService;
                this.user = user;
+               this.simpleNature = SecurityUiPlugin.findSimpleUserNature(user,
+                               simpleNatureType);
+       }
 
-               if (simpleNatureType != null)
-                       simpleNature = (SimpleUserNature) user.getUserNatures().get(
-                                       simpleNatureType);
-               else
-                       for (UserNature userNature : user.getUserNatures().values())
-                               if (userNature instanceof SimpleUserNature)
-                                       simpleNature = (SimpleUserNature) userNature;
+       protected void createFormContent(final IManagedForm mf) {
+               ScrolledForm form = mf.getForm();
+
+               // Set the title of the current form
+               form.setText(simpleNature.getFirstName() + " "
+                               + simpleNature.getLastName());
+
+               ColumnLayout mainLayout = new ColumnLayout();
+               mainLayout.minNumColumns = 1;
+               mainLayout.maxNumColumns = 4;
+
+               mainLayout.topMargin = 0;
+               mainLayout.bottomMargin = 5;
+               mainLayout.leftMargin = mainLayout.rightMargin = mainLayout.horizontalSpacing = mainLayout.verticalSpacing = 10;
+               form.getBody().setLayout(mainLayout);
+
+               FormToolkit tk = mf.getToolkit();
+
+               Composite body = tk.createComposite(form.getBody());
+               GridLayout layout = new GridLayout();
+               layout.marginWidth = layout.marginHeight = 0;
+               layout.numColumns = 2;
+               body.setLayout(layout);
+
+               // Comments
+               tk.createLabel(body, "Username");
+               tk.createLabel(body, user.getUsername());
+
+               firstName = createLT(mf, body, "First name",
+                               simpleNature.getFirstName());
+               lastName = createLT(mf, body, "Last name", simpleNature.getLastName());
+               email = createLT(mf, body, "Email", simpleNature.getEmail());
+               description = createLT(mf, body, "Description",
+                               simpleNature.getDescription());
+
+               // Roles table
+               tk.createLabel(body, "Roles");
+               Table table = new Table(body, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
+               GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
+               gridData.verticalSpan = 20;
+               table.setLayoutData(gridData);
+               table.setLinesVisible(true);
+               table.setHeaderVisible(false);
+               rolesViewer = new TableViewer(table);
+
+               // check column
+               TableViewerColumn column = createTableViewerColumn("checked", 20);
+               column.setLabelProvider(new ColumnLabelProvider() {
+                       public String getText(Object element) {
+                               return null;
+                       }
+
+                       public Image getImage(Object element) {
+                               String role = element.toString();
+                               if (user.getRoles().contains(role)) {
+                                       return CHECKED;
+                               } else {
+                                       return null;
+                               }
+                       }
+               });
+               column.setEditingSupport(new RoleEditingSupport(rolesViewer));
+
+               // role column
+               column = createTableViewerColumn("Role", 200);
+               column.setLabelProvider(new ColumnLabelProvider() {
+                       public String getText(Object element) {
+                               return element.toString();
+                       }
+
+                       public Image getImage(Object element) {
+                               return null;
+                       }
+               });
+               rolesViewer.setContentProvider(new RolesContentProvider());
+               rolesViewer.setInput(getEditorSite());
 
-               if (simpleNature == null)
-                       throw new ArgeoException("No simple user nature in user " + user);
        }
 
-       protected void createFormContent(IManagedForm managedForm) {
-               try {
-                       ScrolledForm form = managedForm.getForm();
-
-                       // Set the title of the current form
-                       form.setText(simpleNature.getFirstName() + " "
-                                       + simpleNature.getLastName());
-
-                       ColumnLayout mainLayout = new ColumnLayout();
-                       mainLayout.minNumColumns = 1;
-                       mainLayout.maxNumColumns = 4;
-
-                       mainLayout.topMargin = 0;
-                       mainLayout.bottomMargin = 5;
-                       mainLayout.leftMargin = mainLayout.rightMargin = mainLayout.horizontalSpacing = mainLayout.verticalSpacing = 10;
-                       form.getBody().setLayout(mainLayout);
-
-                       FormToolkit toolkit = managedForm.getToolkit();
-
-                       Composite body = toolkit.createComposite(form.getBody());
-                       GridLayout layout = new GridLayout();
-                       layout.marginWidth = layout.marginHeight = 0;
-                       layout.numColumns = 2;
-                       body.setLayout(layout);
-
-                       // Comments
-                       toolkit.createLabel(body, "Username");
-                       toolkit.createLabel(body, user.getUsername());
-                       toolkit.createLabel(body, "Email");
-                       email = toolkit.createText(body, simpleNature.getEmail(), SWT.WRAP
-                                       | SWT.BORDER);
-                       toolkit.createLabel(body, "Description");
-                       description = toolkit.createText(body,
-                                       simpleNature.getDescription(), SWT.MULTI | SWT.WRAP
-                                                       | SWT.BORDER);
-
-               } catch (Exception e) {
-                       e.printStackTrace();
-               }
+       protected TableViewerColumn createTableViewerColumn(String title, int bound) {
+               final TableViewerColumn viewerColumn = new TableViewerColumn(
+                               rolesViewer, SWT.NONE);
+               final TableColumn column = viewerColumn.getColumn();
+               column.setText(title);
+               column.setWidth(bound);
+               column.setResizable(true);
+               column.setMoveable(true);
+               return viewerColumn;
+
+       }
+
+       /** Creates label and text. */
+       protected Text createLT(final IManagedForm managedForm, Composite body,
+                       String label, String value) {
+               FormToolkit toolkit = managedForm.getToolkit();
+               toolkit.createLabel(body, label);
+               Text text = toolkit.createText(body, value, SWT.BORDER);
+               text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+               text.addModifyListener(new ModifyListener() {
+                       public void modifyText(ModifyEvent e) {
+                               managedForm.dirtyStateChanged();
+                       }
+               });
+               return text;
        }
 
        public void setSimpleNatureType(String simpleNatureType) {
                this.simpleNatureType = simpleNatureType;
        }
 
+       private class RolesContentProvider implements IStructuredContentProvider {
+               public Object[] getElements(Object inputElement) {
+                       return securityService.getSecurityDao().listEditableRoles()
+                                       .toArray();
+               }
+
+               public void dispose() {
+               }
+
+               public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+               }
+       }
+
+       private class RolesLabelProvider extends LabelProvider implements
+                       ITableLabelProvider {
+               public String getColumnText(Object element, int columnIndex) {
+                       // Principal argeoUser = (Principal) element;
+                       // switch (columnIndex) {
+                       // case 0:
+                       // return argeoUser.getName();
+                       // case 1:
+                       // return argeoUser.toString();
+                       // default:
+                       // throw new ArgeoException("Unmanaged column " + columnIndex);
+                       // }
+                       return element.toString();
+               }
+
+               public Image getColumnImage(Object element, int columnIndex) {
+                       // TODO Auto-generated method stub
+                       return null;
+               }
+
+       }
+
+       class RoleEditingSupport extends EditingSupport {
+
+               private final TableViewer viewer;
+
+               public RoleEditingSupport(TableViewer viewer) {
+                       super(viewer);
+                       this.viewer = viewer;
+               }
+
+               @Override
+               protected CellEditor getCellEditor(Object element) {
+                       return new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
+
+               }
+
+               @Override
+               protected boolean canEdit(Object element) {
+                       return true;
+               }
+
+               @Override
+               protected Object getValue(Object element) {
+                       String role = element.toString();
+                       return user.getRoles().contains(role);
+
+               }
+
+               @Override
+               protected void setValue(Object element, Object value) {
+                       Boolean inRole = (Boolean) value;
+                       String role = element.toString();
+                       if (inRole && !user.getRoles().contains(role))
+                               user.getRoles().add(role);
+                       else if (!inRole && user.getRoles().contains(role))
+                               user.getRoles().remove(role);
+                       viewer.refresh();
+               }
+       }
+
 }
diff --git a/security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/views/RolesView.java b/security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/views/RolesView.java
new file mode 100644 (file)
index 0000000..635525c
--- /dev/null
@@ -0,0 +1,178 @@
+package org.argeo.security.ui.views;
+
+import java.util.ArrayList;
+
+import org.argeo.ArgeoException;
+import org.argeo.security.ArgeoSecurityService;
+import org.argeo.security.ArgeoUser;
+import org.argeo.security.ui.SecurityUiPlugin;
+import org.argeo.security.ui.commands.AddRole;
+import org.argeo.security.ui.commands.OpenArgeoUserEditor;
+import org.eclipse.core.commands.Command;
+import org.eclipse.core.commands.IParameter;
+import org.eclipse.core.commands.Parameterization;
+import org.eclipse.core.commands.ParameterizedCommand;
+import org.eclipse.jface.viewers.DoubleClickEvent;
+import org.eclipse.jface.viewers.IDoubleClickListener;
+import org.eclipse.jface.viewers.IStructuredContentProvider;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.ITableLabelProvider;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Table;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.commands.ICommandService;
+import org.eclipse.ui.handlers.IHandlerService;
+import org.eclipse.ui.part.ViewPart;
+
+/** List all roles. */
+public class RolesView extends ViewPart {
+       public final static String ID = "org.argeo.security.ui.rolesView";
+
+       private Text newRole;
+
+       private TableViewer viewer;
+       private ArgeoSecurityService securityService;
+
+       private String addNewRoleText = "<add new role here>";
+
+       @Override
+       public void createPartControl(Composite parent) {
+               parent.setLayout(new GridLayout(1, false));
+
+               // new role text field
+               newRole = new Text(parent, SWT.BORDER);
+               newRole.setText(addNewRoleText);
+               newRole.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
+               // default action is add role
+               newRole.addListener(SWT.DefaultSelection, new Listener() {
+                       public void handleEvent(Event evt) {
+                               IWorkbench iw = SecurityUiPlugin.getDefault().getWorkbench();
+                               IHandlerService handlerService = (IHandlerService) iw
+                                               .getService(IHandlerService.class);
+                               try {
+                                       handlerService.executeCommand(AddRole.COMMAND_ID, evt);
+                               } catch (Exception e) {
+                                       throw new ArgeoException("Cannot execute add role command",
+                                                       e);
+                               }
+                       }
+               });
+               // select all on focus
+               newRole.addListener(SWT.FocusIn, new Listener() {
+                       public void handleEvent(Event e) {
+                               newRole.selectAll();
+                       }
+               });
+
+               // roles table
+               Table table = new Table(parent, SWT.V_SCROLL | SWT.BORDER);
+               table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+               table.setLinesVisible(false);
+               table.setHeaderVisible(false);
+               viewer = new TableViewer(table);
+               viewer.setContentProvider(new RolesContentProvider());
+               viewer.setLabelProvider(new UsersLabelProvider());
+               viewer.setInput(getViewSite());
+               viewer.addDoubleClickListener(new ViewDoubleClickListener());
+       }
+
+       @Override
+       public void setFocus() {
+               viewer.getTable().setFocus();
+       }
+
+       public void setSecurityService(ArgeoSecurityService securityService) {
+               this.securityService = securityService;
+       }
+
+       public String getAddNewRoleText() {
+               return addNewRoleText;
+       }
+
+       private class RolesContentProvider implements IStructuredContentProvider {
+
+               public Object[] getElements(Object inputElement) {
+                       return securityService.getSecurityDao().listEditableRoles()
+                                       .toArray();
+               }
+
+               public void dispose() {
+               }
+
+               public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+               }
+
+       }
+
+       private class UsersLabelProvider extends LabelProvider implements
+                       ITableLabelProvider {
+               public String getColumnText(Object element, int columnIndex) {
+                       return element.toString();
+               }
+
+               public Image getColumnImage(Object element, int columnIndex) {
+                       return null;
+               }
+
+       }
+
+       class ViewDoubleClickListener implements IDoubleClickListener {
+               public void doubleClick(DoubleClickEvent evt) {
+                       Object obj = ((IStructuredSelection) evt.getSelection())
+                                       .getFirstElement();
+
+                       if (obj instanceof ArgeoUser) {
+                               ArgeoUser argeoUser = (ArgeoUser) obj;
+
+                               IWorkbench iw = SecurityUiPlugin.getDefault().getWorkbench();
+                               IHandlerService handlerService = (IHandlerService) iw
+                                               .getService(IHandlerService.class);
+                               try {
+                                       String commandId = OpenArgeoUserEditor.COMMAND_ID;
+                                       String paramName = OpenArgeoUserEditor.PARAM_USERNAME;
+
+                                       // TODO: factorize this
+                                       // execute related command
+                                       IWorkbenchWindow window = iw.getActiveWorkbenchWindow();
+                                       ICommandService cmdService = (ICommandService) window
+                                                       .getService(ICommandService.class);
+                                       Command cmd = cmdService.getCommand(commandId);
+                                       ArrayList<Parameterization> parameters = new ArrayList<Parameterization>();
+                                       IParameter iparam = cmd.getParameter(paramName);
+                                       Parameterization param = new Parameterization(iparam,
+                                                       argeoUser.getUsername());
+                                       parameters.add(param);
+                                       ParameterizedCommand pc = new ParameterizedCommand(cmd,
+                                                       parameters.toArray(new Parameterization[parameters
+                                                                       .size()]));
+                                       handlerService = (IHandlerService) window
+                                                       .getService(IHandlerService.class);
+                                       handlerService.executeCommand(pc, null);
+                               } catch (Exception e) {
+                                       throw new ArgeoException("Cannot open editor", e);
+                               }
+
+                       }
+               }
+       }
+
+       public String getNewRole() {
+               return newRole.getText();
+       }
+
+       public void refresh() {
+               viewer.refresh();
+               newRole.setText(addNewRoleText);
+       }
+}
index d830155207f1db851dbb51297a5ebb2076e23d89..34feebbe8f722fe8e72bbf7b1875453e768beaf4 100644 (file)
@@ -6,6 +6,7 @@ import org.argeo.ArgeoException;
 import org.argeo.security.ArgeoSecurityService;
 import org.argeo.security.ArgeoUser;
 import org.argeo.security.equinox.CurrentUser;
+import org.argeo.security.nature.SimpleUserNature;
 import org.argeo.security.ui.SecurityUiPlugin;
 import org.argeo.security.ui.commands.OpenArgeoUserEditor;
 import org.eclipse.core.commands.Command;
@@ -31,49 +32,38 @@ import org.eclipse.ui.commands.ICommandService;
 import org.eclipse.ui.handlers.IHandlerService;
 import org.eclipse.ui.part.ViewPart;
 
+/** List all users. */
 public class UsersView extends ViewPart {
        private TableViewer viewer;
        private ArgeoSecurityService securityService;
 
+       private String simpleNatureType = null;
+
        @Override
        public void createPartControl(Composite parent) {
-
-               // viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
-               // | SWT.V_SCROLL);
                viewer = new TableViewer(createTable(parent));
                viewer.setContentProvider(new UsersContentProvider());
                viewer.setLabelProvider(new UsersLabelProvider());
-               viewer.setInput(getViewSite());
                viewer.addDoubleClickListener(new ViewDoubleClickListener());
-               // viewer.setInput(SecurityContextHolder.getContext());
+               viewer.setInput(getViewSite());
        }
 
        protected Table createTable(Composite parent) {
-               int style = SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL;
-
-               Table table = new Table(parent, style);
-
-               // GridData gridData = new GridData(GridData.FILL_BOTH);
-               // gridData.grabExcessVerticalSpace = true;
-               // gridData.grabExcessHorizontalSpace = true;
-               // gridData.horizontalSpan = 3;
-               // table.setLayoutData(gridData);
-
+               Table table = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
                table.setLinesVisible(true);
                table.setHeaderVisible(true);
-
                TableColumn column = new TableColumn(table, SWT.LEFT, 0);
-               column.setText("ID");
-               column.setWidth(100);
-
+               column.setText("User");
+               column.setWidth(50);
                column = new TableColumn(table, SWT.LEFT, 1);
-               column.setText("Password");
-               column.setWidth(200);
-
-               // column = new TableColumn(table, SWT.LEFT, 2);
-               // column.setText("Roles");
-               // column.setWidth(300);
-
+               column.setText("First Name");
+               column.setWidth(100);
+               column = new TableColumn(table, SWT.LEFT, 2);
+               column.setText("Last Name");
+               column.setWidth(100);
+               column = new TableColumn(table, SWT.LEFT, 3);
+               column.setText("E-mail");
+               column.setWidth(100);
                return table;
        }
 
@@ -86,31 +76,20 @@ public class UsersView extends ViewPart {
                this.securityService = securityService;
        }
 
+       public void setSimpleNatureType(String simpleNatureType) {
+               this.simpleNatureType = simpleNatureType;
+       }
+
        private class UsersContentProvider implements IStructuredContentProvider {
 
                public Object[] getElements(Object inputElement) {
-                       return getChildren(inputElement);
-               }
-
-               public Object[] getChildren(Object parentElement) {
-                       // try {
-                       // Thread.sleep(3000);
-                       // } catch (InterruptedException e) {
-                       // // TODO Auto-generated catch block
-                       // e.printStackTrace();
-                       // }
-
                        return securityService.getSecurityDao().listUsers().toArray();
                }
 
                public void dispose() {
-                       // TODO Auto-generated method stub
-
                }
 
                public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
-                       // TODO Auto-generated method stub
-
                }
 
        }
@@ -119,17 +98,21 @@ public class UsersView extends ViewPart {
                        ITableLabelProvider {
                public String getColumnText(Object element, int columnIndex) {
                        String currentUsername = CurrentUser.getUsername();
-                       ArgeoUser argeoUser = (ArgeoUser) element;
+                       ArgeoUser user = (ArgeoUser) element;
+                       SimpleUserNature simpleNature = SecurityUiPlugin
+                                       .findSimpleUserNature(user, simpleNatureType);
                        switch (columnIndex) {
                        case 0:
-                               String userName = argeoUser.getUsername();
+                               String userName = user.getUsername();
                                if (userName.equals(currentUsername))
                                        userName = userName + "*";
                                return userName;
                        case 1:
-                               return argeoUser.getPassword();
+                               return simpleNature.getFirstName();
                        case 2:
-                               return argeoUser.getRoles().toString();
+                               return simpleNature.getLastName();
+                       case 3:
+                               return simpleNature.getEmail();
                        default:
                                throw new ArgeoException("Unmanaged column " + columnIndex);
                        }
index f91e86748457669de1ce0cd31edcaae995f85742..85d83afa55c3b716baefddc4a942d785c19c52a4 100644 (file)
@@ -25,16 +25,26 @@ import java.util.List;
 public interface ArgeoSecurityDao {
        // public ArgeoUser getCurrentUser();
 
+       /** List all users */
        public List<ArgeoUser> listUsers();
 
+       /** List roles that can be modified */
        public List<String> listEditableRoles();
 
+       /**
+        * Creates a new user in the underlying storage. <b>DO NOT CALL DIRECTLY</b>
+        * use {@link ArgeoSecurityService#newUser(ArgeoUser)} instead.
+        */
        public void create(ArgeoUser user);
 
        public void update(ArgeoUser user);
 
        public void delete(String username);
 
+       /**
+        * Creates a new role in the underlying storage. <b>DO NOT CALL DIRECTLY</b>
+        * use {@link ArgeoSecurityService#newRole(String)} instead.
+        */
        public void createRole(String role, String superuserName);
 
        public void deleteRole(String role);