Remove 32 bits SWT.
[lgpl/argeo-commons.git] / org.argeo.cms.ui.workbench / src / org / argeo / cms / ui / workbench / commands / OpenChangePasswordDialog.java
index d48291f385645c5b3352e77f260fbd1ba8c55dd9..30836b948c19593f6fd15661524b568861c2785f 100644 (file)
@@ -23,6 +23,7 @@ import static org.argeo.cms.CmsMsg.repeatNewPassword;
 import static org.eclipse.jface.dialogs.IMessageProvider.INFORMATION;
 
 import java.security.AccessController;
+import java.util.Arrays;
 
 import javax.naming.InvalidNameException;
 import javax.naming.ldap.LdapName;
@@ -34,6 +35,7 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.cms.CmsException;
 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
+import org.argeo.node.security.CryptoKeyring;
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
@@ -53,19 +55,18 @@ import org.eclipse.ui.handlers.HandlerUtil;
 import org.osgi.service.useradmin.User;
 import org.osgi.service.useradmin.UserAdmin;
 
-/** Opens the change password dialog. */
+/** Open the change password dialog */
 public class OpenChangePasswordDialog extends AbstractHandler {
-       private final static Log log = LogFactory
-                       .getLog(OpenChangePasswordDialog.class);
+       private final static Log log = LogFactory.getLog(OpenChangePasswordDialog.class);
        private UserAdmin userAdmin;
        private UserTransaction userTransaction;
+       private CryptoKeyring keyring = null;
 
        public Object execute(ExecutionEvent event) throws ExecutionException {
-               ChangePasswordDialog dialog = new ChangePasswordDialog(
-                               HandlerUtil.getActiveShell(event), userAdmin);
+               ChangePasswordDialog dialog = new ChangePasswordDialog(HandlerUtil.getActiveShell(event), userAdmin);
                if (dialog.open() == Dialog.OK) {
-                       MessageDialog.openInformation(HandlerUtil.getActiveShell(event),
-                                       passwordChanged.lead(), passwordChanged.lead());
+                       MessageDialog.openInformation(HandlerUtil.getActiveShell(event), passwordChanged.lead(),
+                                       passwordChanged.lead());
                }
                return null;
        }
@@ -73,8 +74,7 @@ public class OpenChangePasswordDialog extends AbstractHandler {
        @SuppressWarnings("unchecked")
        protected void changePassword(char[] oldPassword, char[] newPassword) {
                Subject subject = Subject.getSubject(AccessController.getContext());
-               String name = subject.getPrincipals(X500Principal.class).iterator()
-                               .next().toString();
+               String name = subject.getPrincipals(X500Principal.class).iterator().next().toString();
                LdapName dn;
                try {
                        dn = new LdapName(name);
@@ -84,11 +84,15 @@ public class OpenChangePasswordDialog extends AbstractHandler {
                User user = (User) userAdmin.getRole(dn.toString());
                if (!user.hasCredential(null, oldPassword))
                        throw new CmsException("Invalid password");
-               if (newPassword.equals(""))
+               if (Arrays.equals(newPassword, new char[0]))
                        throw new CmsException("New password empty");
                try {
                        userTransaction.begin();
                        user.getCredentials().put(null, newPassword);
+                       if (keyring != null) {
+                               keyring.changePassword(oldPassword, newPassword);
+                               // TODO change secret keys in the CMS session
+                       }
                        userTransaction.commit();
                } catch (Exception e) {
                        try {
@@ -103,14 +107,6 @@ public class OpenChangePasswordDialog extends AbstractHandler {
                }
        }
 
-       public void setUserAdmin(UserAdmin userDetailsManager) {
-               this.userAdmin = userDetailsManager;
-       }
-
-       public void setUserTransaction(UserTransaction userTransaction) {
-               this.userTransaction = userTransaction;
-       }
-
        class ChangePasswordDialog extends TitleAreaDialog {
                private static final long serialVersionUID = -6963970583882720962L;
                private Text oldPassword, newPassword1, newPassword2;
@@ -125,18 +121,17 @@ public class OpenChangePasswordDialog extends AbstractHandler {
 
                protected Control createDialogArea(Composite parent) {
                        Composite dialogarea = (Composite) super.createDialogArea(parent);
-                       dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
-                                       true));
+                       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,
-                                       false));
+                       composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
                        oldPassword = createLP(composite, currentPassword.lead());
                        newPassword1 = createLP(composite, newPassword.lead());
                        newPassword2 = createLP(composite, repeatNewPassword.lead());
 
                        setMessage(changePassword.lead(), INFORMATION);
                        parent.pack();
+                       oldPassword.setFocus();
                        return composite;
                }
 
@@ -145,8 +140,7 @@ public class OpenChangePasswordDialog extends AbstractHandler {
                        try {
                                if (!newPassword1.getText().equals(newPassword2.getText()))
                                        throw new CmsException("New passwords are different");
-                               changePassword(oldPassword.getTextChars(),
-                                               newPassword1.getTextChars());
+                               changePassword(oldPassword.getTextChars(), newPassword1.getTextChars());
                                close();
                        } catch (Exception e) {
                                ErrorFeedback.show("Cannot change password", e);
@@ -156,8 +150,7 @@ public class OpenChangePasswordDialog extends AbstractHandler {
                /** Creates label and password. */
                protected Text createLP(Composite parent, String label) {
                        new Label(parent, SWT.NONE).setText(label);
-                       Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.PASSWORD
-                                       | SWT.BORDER);
+                       Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.PASSWORD | SWT.BORDER);
                        text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
                        return text;
                }
@@ -166,6 +159,18 @@ public class OpenChangePasswordDialog extends AbstractHandler {
                        super.configureShell(shell);
                        shell.setText(changePassword.lead());
                }
+       }
 
+       public void setUserAdmin(UserAdmin userAdmin) {
+               this.userAdmin = userAdmin;
        }
+
+       public void setUserTransaction(UserTransaction userTransaction) {
+               this.userTransaction = userTransaction;
+       }
+
+       public void setKeyring(CryptoKeyring keyring) {
+               this.keyring = keyring;
+       }
+
 }