Make security UI more robust
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 30 Jan 2011 21:51:48 +0000 (21:51 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 30 Jan 2011 21:51:48 +0000 (21:51 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@4100 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

security/eclipse/plugins/org.argeo.security.ui.application/src/main/java/org/argeo/security/ui/application/AbstractSecureApplication.java
security/eclipse/plugins/org.argeo.security.ui.application/src/main/java/org/argeo/security/ui/application/RapSecureWorkbenchWindowAdvisor.java
security/eclipse/plugins/org.argeo.security.ui.application/src/main/java/org/argeo/security/ui/application/SecureActionBarAdvisor.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/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/DefaultSecurityService.java
security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/ldap/nature/SimpleUserNatureMapper.java

index c3bf6b1131adf9adf933ec3e03c5f4c69e191e48..3a92e5273058ca2914e3253bc84787cf3d3c5d27 100644 (file)
@@ -23,6 +23,7 @@ public abstract class AbstractSecureApplication implements IApplication {
 
        protected abstract WorkbenchAdvisor createWorkbenchAdvisor();
 
+       @SuppressWarnings("unchecked")
        public Object start(IApplicationContext context) throws Exception {
 
                Integer returnCode = null;
@@ -49,8 +50,6 @@ public abstract class AbstractSecureApplication implements IApplication {
                                log.debug("Logged in as " + username);
                        returnCode = (Integer) Subject.doAs(CurrentUser.getSubject(),
                                        getRunAction(display));
-                       if (log.isDebugEnabled())
-                               log.debug("secure action completed");
                        CurrentUser.logout();
                        return processReturnCode(returnCode);
                } catch (Exception e) {
index 6cc6fe2ae087fd4fbd13ab48040a7abdc18dfd28..fb4911a081dedb000150709faa95d160c9654fac 100644 (file)
@@ -1,7 +1,5 @@
 package org.argeo.security.ui.application;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.graphics.Rectangle;
@@ -12,9 +10,6 @@ import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
 
 public class RapSecureWorkbenchWindowAdvisor extends
                SecureWorkbenchWindowAdvisor {
-       private final static Log log = LogFactory
-                       .getLog(RapSecureWorkbenchWindowAdvisor.class);
-
        public RapSecureWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
                super(configurer);
        }
@@ -27,11 +22,6 @@ public class RapSecureWorkbenchWindowAdvisor extends
 
        public void preWindowOpen() {
                IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
-               // configurer.setInitialSize(new Point(800, 600));
-
-//             if (log.isDebugEnabled())
-//                     log.debug("CHAR ENCODING"
-//                                     + System.getProperty("file.encoding"));
                configurer.setShowCoolBar(true);
                configurer.setShowMenuBar(true);
                configurer.setShowStatusLine(false);
index fee492691b903da3fdd0d6aea95f7f1bb915dc2d..61a3c040726cc17ef807c5ebeca49fdf66374acc 100644 (file)
@@ -20,7 +20,6 @@ public class SecureActionBarAdvisor extends ActionBarAdvisor {
        private IWorkbenchAction openPerspectiveDialogAction;
        private IWorkbenchAction showViewMenuAction;
        private IWorkbenchAction preferences;
-       private IWorkbenchAction helpContentAction;
        private IWorkbenchAction saveAction;
        private IWorkbenchAction saveAllAction;
        private IWorkbenchAction closeAllAction;
@@ -40,8 +39,6 @@ public class SecureActionBarAdvisor extends ActionBarAdvisor {
                register(openPerspectiveDialogAction);
                showViewMenuAction = ActionFactory.SHOW_VIEW_MENU.create(window);
                register(showViewMenuAction);
-               helpContentAction = ActionFactory.HELP_CONTENTS.create(window);
-               register(helpContentAction);
 
                exitAction = ActionFactory.QUIT.create(window);
                register(exitAction);
@@ -63,15 +60,12 @@ public class SecureActionBarAdvisor extends ActionBarAdvisor {
                                IWorkbenchActionConstants.M_EDIT);
                MenuManager windowMenu = new MenuManager("&Window",
                                IWorkbenchActionConstants.M_WINDOW);
-               MenuManager helpMenu = new MenuManager("&Help",
-                               IWorkbenchActionConstants.M_HELP);
 
                menuBar.add(fileMenu);
                menuBar.add(editMenu);
                menuBar.add(windowMenu);
                // Add a group marker indicating where action set menus will appear.
                menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
-               menuBar.add(helpMenu);
 
                // File
                fileMenu.add(saveAction);
@@ -87,10 +81,6 @@ public class SecureActionBarAdvisor extends ActionBarAdvisor {
                // Window
                windowMenu.add(openPerspectiveDialogAction);
                windowMenu.add(showViewMenuAction);
-
-               // Help
-               helpMenu.add(helpContentAction);
-               // helpMenu.add(aboutAction);
        }
 
        @Override
index 1cc41783ca730547b4801bcfc5a39a4aa799529d..80abbe7f462f9331d2f54dc5c83dc1b7178f1e3f 100644 (file)
@@ -51,15 +51,8 @@ public class ArgeoUserEditor extends FormEditor {
                if (securityService.getSecurityDao().userExists(user.getUsername()))
                        securityService.updateUser(user);
                else {
-                       try {
-                               // FIXME: make it cleaner
-                               ((SimpleArgeoUser)user).setPassword(user.getUsername());
-                               securityService.newUser(user);
-                               setPartName(user.getUsername());
-                       } catch (Exception e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
-                       }
+                       securityService.newUser(user);
+                       setPartName(user.getUsername());
                }
                firePropertyChange(PROP_DIRTY);
        }
index ee63e24dbd20bbfdea76c64fc1c5062c8e6ce446..696d23d6d2a4cebcb53ff068d4e141c76719bbec 100644 (file)
@@ -74,6 +74,7 @@ public class DefaultUserMainPage extends FormPage {
 
                createGeneralPart(form.getBody());
                createRolesPart(form.getBody());
+               createPassworPart(form.getBody());
        }
 
        /** Creates the general section */
@@ -123,6 +124,8 @@ public class DefaultUserMainPage extends FormPage {
                                        log.trace("General part committed");
                        }
                };
+               if (username != null)
+                       username.addModifyListener(new FormPartML(part));
                firstName.addModifyListener(new FormPartML(part));
                lastName.addModifyListener(new FormPartML(part));
                email.addModifyListener(new FormPartML(part));
@@ -130,6 +133,39 @@ public class DefaultUserMainPage extends FormPage {
                getManagedForm().addPart(part);
        }
 
+       /** Creates the password section */
+       protected void createPassworPart(Composite parent) {
+               FormToolkit tk = getManagedForm().getToolkit();
+               Section section = tk.createSection(parent, Section.TITLE_BAR);
+               section.setText("Password");
+
+               Composite body = tk.createComposite(section, SWT.WRAP);
+               section.setClient(body);
+               GridLayout layout = new GridLayout();
+               layout.marginWidth = layout.marginHeight = 0;
+               layout.numColumns = 2;
+               body.setLayout(layout);
+
+               // add widgets (view)
+               final Text password1 = createLP(body, "New password", "");
+               final Text password2 = createLP(body, "Repeat password", "");
+               // create form part (controller)
+               AbstractFormPart part = new SectionPart(section) {
+                       public void commit(boolean onSave) {
+                               if (!password1.getText().equals("")
+                                               && password1.getText().equals(password2.getText())) {
+                                       ((SimpleArgeoUser) user).setPassword(password1.getText());
+                               }
+                               super.commit(onSave);
+                               if (log.isTraceEnabled())
+                                       log.trace("Password part committed");
+                       }
+               };
+               password1.addModifyListener(new FormPartML(part));
+               password2.addModifyListener(new FormPartML(part));
+               getManagedForm().addPart(part);
+       }
+
        /** Creates the role section */
        protected void createRolesPart(Composite parent) {
                FormToolkit tk = getManagedForm().getToolkit();
@@ -215,6 +251,15 @@ public class DefaultUserMainPage extends FormPage {
                return text;
        }
 
+       /** Creates label and password. */
+       protected Text createLP(Composite body, String label, String value) {
+               FormToolkit toolkit = getManagedForm().getToolkit();
+               toolkit.createLabel(body, label);
+               Text text = toolkit.createText(body, value, SWT.BORDER | SWT.PASSWORD);
+               text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+               return text;
+       }
+
        public void setSimpleNatureType(String simpleNatureType) {
                this.simpleNatureType = simpleNatureType;
        }
index d6ff69cc23b549632e26edfbe4f93a7ac081b0ad..b9b85087b31f45c1b15786e918eb2d9f5e60bc29 100644 (file)
@@ -59,7 +59,7 @@ public class DefaultSecurityService implements ArgeoSecurityService {
        public void updateUserPassword(String username, String password) {
                SimpleArgeoUser user = new SimpleArgeoUser(
                                securityDao.getUser(username));
-               user.setPassword(password);
+               user.setPassword(securityDao.encodePassword(password));
                securityDao.update(user);
        }
 
@@ -72,14 +72,26 @@ public class DefaultSecurityService implements ArgeoSecurityService {
        }
 
        public void newUser(ArgeoUser user) {
-//             user.getUserNatures().clear();
                argeoSecurity.beforeCreate(user);
+               // normalize password
+               if (user instanceof SimpleArgeoUser) {
+                       if (user.getPassword() == null || user.getPassword().equals(""))
+                               ((SimpleArgeoUser) user).setPassword(securityDao
+                                               .encodePassword(user.getUsername()));
+                       else if (!user.getPassword().startsWith("{"))
+                               ((SimpleArgeoUser) user).setPassword(securityDao
+                                               .encodePassword(user.getPassword()));
+               }
                securityDao.create(user);
        }
 
        public void updateUser(ArgeoUser user) {
-               String password = securityDao.getUserWithPassword(user.getUsername())
-                               .getPassword();
+               String password = user.getPassword();
+               if (password == null)
+                       password = securityDao.getUserWithPassword(user.getUsername())
+                                       .getPassword();
+               if (!password.startsWith("{"))
+                       password = securityDao.encodePassword(user.getPassword());
                SimpleArgeoUser simpleArgeoUser = new SimpleArgeoUser(user);
                simpleArgeoUser.setPassword(password);
                securityDao.update(simpleArgeoUser);
index bc20c9db8026909b3352ac417b0448a1ccfdd26a..681c5174a65b4029c3e0782fb597d29e07d88835 100644 (file)
@@ -44,7 +44,8 @@ public class SimpleUserNatureMapper implements UserNatureMapper {
                ctx.setAttributeValue("sn", nature.getLastName());
                ctx.setAttributeValue("givenName", nature.getFirstName());
                ctx.setAttributeValue("mail", nature.getEmail());
-               if (nature.getDescription() != null) {
+               if (nature.getDescription() != null
+                               && !nature.getDescription().equals("")) {
                        ctx.setAttributeValue("description", nature.getDescription());
                }
        }