]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/internal/commands/NewUser.java
Improve and simplify OSGi Boot
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / internal / commands / NewUser.java
index d8d697a1f17aa08a8ab4498ef7cb0b159d815d43..c04c83562f3cad02fdbbed82a40b5eedf4cd728e 100644 (file)
 package org.argeo.security.ui.admin.internal.commands;
 
 import java.util.Dictionary;
+import java.util.List;
+import java.util.Map;
 
+import javax.naming.InvalidNameException;
+import javax.naming.ldap.LdapName;
+import javax.naming.ldap.Rdn;
+
+import org.argeo.cms.CmsException;
+import org.argeo.cms.util.useradmin.UserAdminUtils;
 import org.argeo.eclipse.ui.EclipseUiUtils;
 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
 import org.argeo.jcr.ArgeoNames;
-import org.argeo.security.UserAdminService;
+import org.argeo.osgi.useradmin.LdifName;
+import org.argeo.osgi.useradmin.UserAdminConf;
 import org.argeo.security.ui.admin.SecurityAdminPlugin;
-import org.argeo.security.ui.admin.internal.UiAdminUtils;
-import org.argeo.security.ui.admin.internal.UserAdminConstants;
 import org.argeo.security.ui.admin.internal.UserAdminWrapper;
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
@@ -34,8 +41,11 @@ import org.eclipse.jface.wizard.WizardPage;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.ModifyEvent;
 import org.eclipse.swt.events.ModifyListener;
+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.Label;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.handlers.HandlerUtil;
 import org.osgi.service.useradmin.Role;
@@ -44,40 +54,28 @@ import org.osgi.service.useradmin.UserAdminEvent;
 
 /** Open a wizard that enables creation of a new user. */
 public class NewUser extends AbstractHandler {
+       /**
+        * Email addresses must match this regexp pattern ({@value #EMAIL_PATTERN}.
+        * Thanks to <a href=
+        * "http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/"
+        * >this tip</a>.
+        */
+       public final static String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        // private final static Log log = LogFactory.getLog(NewUser.class);
        public final static String ID = SecurityAdminPlugin.PLUGIN_ID + ".newUser";
 
        /* DEPENDENCY INJECTION */
        private UserAdminWrapper userAdminWrapper;
 
-       // TODO implement a dynamic choice of the base dn
-       private String getDn(String uid) {
-               return "uid=" + uid + ",ou=users,dc=example,dc=com";
-       }
-
        public Object execute(ExecutionEvent event) throws ExecutionException {
                NewUserWizard newUserWizard = new NewUserWizard();
+               newUserWizard.setWindowTitle("User creation");
                WizardDialog dialog = new WizardDialog(
                                HandlerUtil.getActiveShell(event), newUserWizard);
-
                dialog.open();
-
-               // // Force refresh until the listener are implemented
-               // if (Window.OK == dialog.open())
-               // forceRefresh(event);
                return null;
        }
 
-       // private void forceRefresh(ExecutionEvent event) {
-       // IWorkbenchWindow iww = HandlerUtil.getActiveWorkbenchWindow(event);
-       // if (iww == null)
-       // return;
-       // IWorkbenchPage activePage = iww.getActivePage();
-       // IWorkbenchPart part = activePage.getActivePart();
-       // if (part instanceof UsersView)
-       // ((UsersView) part).refresh();
-       // }
-
        private class NewUserWizard extends Wizard {
 
                // pages
@@ -86,15 +84,18 @@ public class NewUser extends AbstractHandler {
                // End user fields
                private Text dNameTxt, usernameTxt, firstNameTxt, lastNameTxt,
                                primaryMailTxt, pwd1Txt, pwd2Txt;
+               private Combo baseDnCmb;
 
                public NewUserWizard() {
+
                }
 
                @Override
                public void addPages() {
                        mainUserInfo = new MainUserInfoWizardPage();
                        addPage(mainUserInfo);
-                       String message = "Dummy wizard to ease user creation tests:\n Mail and last name are automatically "
+                       String message = "Default wizard that also eases user creation tests:\n "
+                                       + "Mail and last name are automatically "
                                        + "generated form the uid. Password are defauted to 'demo'.";
                        mainUserInfo.setMessage(message, WizardPage.WARNING);
                }
@@ -105,33 +106,33 @@ public class NewUser extends AbstractHandler {
                        if (!canFinish())
                                return false;
                        String username = mainUserInfo.getUsername();
+                       userAdminWrapper.beginTransactionIfNeeded();
                        try {
-                               userAdminWrapper.beginTransactionIfNeeded();
                                User user = (User) userAdminWrapper.getUserAdmin().createRole(
                                                getDn(username), Role.USER);
 
                                Dictionary props = user.getProperties();
 
                                String lastNameStr = lastNameTxt.getText();
-                               if (UiAdminUtils.notNull(lastNameStr))
-                                       props.put(UserAdminConstants.KEY_LASTNAME, lastNameStr);
+                               if (EclipseUiUtils.notEmpty(lastNameStr))
+                                       props.put(LdifName.sn.name(), lastNameStr);
 
                                String firstNameStr = firstNameTxt.getText();
-                               if (UiAdminUtils.notNull(firstNameStr))
-                                       props.put(UserAdminConstants.KEY_FIRSTNAME, firstNameStr);
+                               if (EclipseUiUtils.notEmpty(firstNameStr))
+                                       props.put(LdifName.givenName.name(), firstNameStr);
 
-                               String cn = UiAdminUtils
-                                               .getDefaultCn(firstNameStr, lastNameStr);
-                               if (UiAdminUtils.notNull(cn))
-                                       props.put(UserAdminConstants.KEY_CN, cn);
+                               String cn = UserAdminUtils.buildDefaultCn(firstNameStr,
+                                               lastNameStr);
+                               if (EclipseUiUtils.notEmpty(cn))
+                                       props.put(LdifName.cn.name(), cn);
 
                                String mailStr = primaryMailTxt.getText();
-                               if (UiAdminUtils.notNull(mailStr))
-                                       props.put(UserAdminConstants.KEY_MAIL, mailStr);
+                               if (EclipseUiUtils.notEmpty(mailStr))
+                                       props.put(LdifName.mail.name(), mailStr);
 
                                char[] password = mainUserInfo.getPassword();
                                user.getCredentials().put(null, password);
-
+                               userAdminWrapper.commitOrNotifyTransactionStateChange();
                                userAdminWrapper.notifyListeners(new UserAdminEvent(null,
                                                UserAdminEvent.ROLE_CREATED, user));
                                return true;
@@ -157,6 +158,20 @@ public class NewUser extends AbstractHandler {
                                dNameTxt = EclipseUiUtils.createGridLT(composite,
                                                "Distinguished name", this);
                                dNameTxt.setEnabled(false);
+
+                               baseDnCmb = createGridLC(composite, "Base DN");
+                               initialiseDnCmb(baseDnCmb);
+                               baseDnCmb.addModifyListener(this);
+                               baseDnCmb.addModifyListener(new ModifyListener() {
+                                       private static final long serialVersionUID = -1435351236582736843L;
+
+                                       @Override
+                                       public void modifyText(ModifyEvent event) {
+                                               String name = usernameTxt.getText();
+                                               dNameTxt.setText(getDn(name));
+                                       }
+                               });
+
                                usernameTxt = EclipseUiUtils.createGridLT(composite,
                                                "Local ID", this);
                                usernameTxt.addModifyListener(new ModifyListener() {
@@ -174,7 +189,7 @@ public class NewUser extends AbstractHandler {
                                                } else {
                                                        dNameTxt.setText(getDn(name));
                                                        lastNameTxt.setText(name.toUpperCase());
-                                                       primaryMailTxt.setText(name + "@example.com");
+                                                       primaryMailTxt.setText(getMail(name));
                                                        pwd1Txt.setText("demo");
                                                        pwd2Txt.setText("demo");
                                                }
@@ -221,8 +236,7 @@ public class NewUser extends AbstractHandler {
                                                .getRole(getDn(name));
                                if (role != null)
                                        return "User " + name + " already exists";
-                               if (!primaryMailTxt.getText().matches(
-                                               UserAdminService.EMAIL_PATTERN))
+                               if (!primaryMailTxt.getText().matches(EMAIL_PATTERN))
                                        return "Not a valid email address";
                                if (lastNameTxt.getText().trim().equals(""))
                                        return "Specify a last name";
@@ -239,7 +253,10 @@ public class NewUser extends AbstractHandler {
                        public void setVisible(boolean visible) {
                                super.setVisible(visible);
                                if (visible)
-                                       usernameTxt.setFocus();
+                                       if (baseDnCmb.getSelectionIndex() == -1)
+                                               baseDnCmb.setFocus();
+                                       else
+                                               usernameTxt.setFocus();
                        }
 
                        public String getUsername() {
@@ -251,6 +268,57 @@ public class NewUser extends AbstractHandler {
                        }
 
                }
+
+               private Map<String, String> getDns() {
+                       return userAdminWrapper.getKnownBaseDns(true);
+               }
+
+               private String getDn(String uid) {
+                       Map<String, String> dns = getDns();
+                       String bdn = baseDnCmb.getText();
+                       if (EclipseUiUtils.notEmpty(bdn)) {
+                               Dictionary<String, ?> props = UserAdminConf.uriAsProperties(dns
+                                               .get(bdn));
+                               String dn = LdifName.uid.name() + "=" + uid + ","
+                                               + UserAdminConf.userBase.getValue(props) + "," + bdn;
+                               return dn;
+                       }
+                       return null;
+               }
+
+               private void initialiseDnCmb(Combo combo) {
+                       Map<String, String> dns = userAdminWrapper.getKnownBaseDns(true);
+                       if (dns.isEmpty())
+                               throw new CmsException(
+                                               "No writable base dn found. Cannot create user");
+                       combo.setItems(dns.keySet().toArray(new String[0]));
+                       if (dns.size() == 1)
+                               combo.select(0);
+               }
+
+               private String getMail(String username) {
+                       if (baseDnCmb.getSelectionIndex() == -1)
+                               return null;
+                       String baseDn = baseDnCmb.getText();
+                       try {
+                               LdapName name = new LdapName(baseDn);
+                               List<Rdn> rdns = name.getRdns();
+                               return username + "@" + (String) rdns.get(1).getValue() + '.'
+                                               + (String) rdns.get(0).getValue();
+                       } catch (InvalidNameException e) {
+                               throw new CmsException("Unable to generate mail for "
+                                               + username + " with base dn " + baseDn, e);
+                       }
+               }
+       }
+
+       private Combo createGridLC(Composite parent, String label) {
+               Label lbl = new Label(parent, SWT.LEAD);
+               lbl.setText(label);
+               lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
+               Combo combo = new Combo(parent, SWT.LEAD | SWT.BORDER | SWT.READ_ONLY);
+               combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
+               return combo;
        }
 
        /* DEPENDENCY INJECTION */