]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/editors/DefaultUserMainPage.java
Improve Security
[lgpl/argeo-commons.git] / security / eclipse / plugins / org.argeo.security.ui / src / main / java / org / argeo / security / ui / editors / DefaultUserMainPage.java
index 3da89f37c72f515570d13ab615e4b9f5c4d91d4a..6180de49ad871c37e8cf1bc56c297df9e63ab0ba 100644 (file)
@@ -4,6 +4,7 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.security.ArgeoSecurityService;
 import org.argeo.security.ArgeoUser;
+import org.argeo.security.SimpleArgeoUser;
 import org.argeo.security.nature.SimpleUserNature;
 import org.argeo.security.ui.SecurityUiPlugin;
 import org.eclipse.jface.viewers.CellEditor;
@@ -41,9 +42,9 @@ import org.eclipse.ui.forms.widgets.Section;
 public class DefaultUserMainPage extends FormPage {
        final static String ID = "argeoUserEditor.mainPage";
 
+       private final static Log log = LogFactory.getLog(DefaultUserMainPage.class);
        private final static Image ROLE_CHECKED = SecurityUiPlugin
                        .getImageDescriptor("icons/security.gif").createImage();
-       private final static Log log = LogFactory.getLog(ArgeoUserEditor.class);
 
        private ArgeoUser user;
        private SimpleUserNature simpleNature;
@@ -55,7 +56,7 @@ public class DefaultUserMainPage extends FormPage {
                super(editor, ID, "Main");
                this.securityService = securityService;
                this.user = user;
-               this.simpleNature = SecurityUiPlugin.findSimpleUserNature(user,
+               this.simpleNature = SimpleUserNature.findSimpleUserNature(user,
                                simpleNatureType);
        }
 
@@ -73,6 +74,7 @@ public class DefaultUserMainPage extends FormPage {
 
                createGeneralPart(form.getBody());
                createRolesPart(form.getBody());
+               createPassworPart(form.getBody());
        }
 
        /** Creates the general section */
@@ -89,8 +91,14 @@ public class DefaultUserMainPage extends FormPage {
                body.setLayout(layout);
 
                // add widgets (view)
-               tk.createLabel(body, "Username");
-               tk.createLabel(body, user.getUsername());
+               final Text username;
+               if (user.getUsername() != null) {
+                       tk.createLabel(body, "Username");
+                       tk.createLabel(body, user.getUsername());
+                       username = null;
+               } else {
+                       username = createLT(body, "Username", "");
+               }
                final Text firstName = createLT(body, "First name",
                                simpleNature.getFirstName());
                final Text lastName = createLT(body, "Last name",
@@ -102,15 +110,22 @@ public class DefaultUserMainPage extends FormPage {
                // create form part (controller)
                AbstractFormPart part = new SectionPart(section) {
                        public void commit(boolean onSave) {
+                               if (username != null) {
+                                       ((SimpleArgeoUser) user).setUsername(username.getText());
+                                       username.setEditable(false);
+                                       username.setEnabled(false);
+                               }
                                simpleNature.setFirstName(firstName.getText());
                                simpleNature.setLastName(lastName.getText());
                                simpleNature.setEmail(email.getText());
                                simpleNature.setDescription(description.getText());
                                super.commit(onSave);
-                               if (log.isDebugEnabled())
-                                       log.debug("General part committed");
+                               if (log.isTraceEnabled())
+                                       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));
@@ -118,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();
@@ -134,8 +182,8 @@ public class DefaultUserMainPage extends FormPage {
                        public void commit(boolean onSave) {
                                // roles have already been modified in editing
                                super.commit(onSave);
-                               if (log.isDebugEnabled())
-                                       log.debug("Role part committed");
+                               if (log.isTraceEnabled())
+                                       log.trace("Role part committed");
                        }
                };
                getManagedForm().addPart(part);
@@ -203,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;
        }
@@ -222,8 +279,7 @@ public class DefaultUserMainPage extends FormPage {
 
        private class RolesContentProvider implements IStructuredContentProvider {
                public Object[] getElements(Object inputElement) {
-                       return securityService.getSecurityDao().listEditableRoles()
-                                       .toArray();
+                       return securityService.listEditableRoles().toArray();
                }
 
                public void dispose() {