]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ChangePassword.java
dd761267d5e6d3097a88c54fea07c69a15b3cd4d
[lgpl/argeo-commons.git] / ChangePassword.java
1 package org.argeo.cms.e4.handlers;
2
3 import static org.argeo.cms.CmsMsg.changePassword;
4 import static org.argeo.cms.CmsMsg.currentPassword;
5 import static org.argeo.cms.CmsMsg.newPassword;
6 import static org.argeo.cms.CmsMsg.passwordChanged;
7 import static org.argeo.cms.CmsMsg.repeatNewPassword;
8
9 import java.util.Arrays;
10
11 import javax.inject.Inject;
12 import javax.naming.InvalidNameException;
13 import javax.naming.ldap.LdapName;
14
15 import org.argeo.api.cms.keyring.CryptoKeyring;
16 import org.argeo.api.cms.transaction.WorkTransaction;
17 import org.argeo.cms.auth.CurrentUser;
18 import org.argeo.cms.swt.dialogs.CmsFeedback;
19 import org.argeo.cms.swt.dialogs.CmsMessageDialog;
20 import org.eclipse.e4.core.di.annotations.Execute;
21 import org.eclipse.e4.core.di.annotations.Optional;
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.swt.widgets.Text;
32 import org.osgi.service.useradmin.User;
33 import org.osgi.service.useradmin.UserAdmin;
34
35 /** Change the password of the logged-in user. */
36 public class ChangePassword {
37 @Inject
38 private UserAdmin userAdmin;
39 @Inject
40 private WorkTransaction userTransaction;
41 @Inject
42 @Optional
43 private CryptoKeyring keyring = null;
44
45 @Execute
46 public void execute() {
47 ChangePasswordDialog dialog = new ChangePasswordDialog(Display.getCurrent().getActiveShell(), userAdmin);
48 if (dialog.open() == Dialog.OK) {
49 new CmsMessageDialog(Display.getCurrent().getActiveShell(), passwordChanged.lead(),
50 CmsMessageDialog.INFORMATION).open();
51 }
52 }
53
54 protected void changePassword(char[] oldPassword, char[] newPassword) {
55 String name = CurrentUser.getUsername();
56 LdapName dn;
57 try {
58 dn = new LdapName(name);
59 } catch (InvalidNameException e) {
60 throw new IllegalArgumentException("Invalid user dn " + name, e);
61 }
62 User user = (User) userAdmin.getRole(dn.toString());
63 if (!user.hasCredential(null, oldPassword))
64 throw new IllegalArgumentException("Invalid password");
65 if (Arrays.equals(newPassword, new char[0]))
66 throw new IllegalArgumentException("New password empty");
67 try {
68 userTransaction.begin();
69 user.getCredentials().put(null, newPassword);
70 if (keyring != null) {
71 keyring.changePassword(oldPassword, newPassword);
72 // TODO change secret keys in the CMS session
73 }
74 userTransaction.commit();
75 } catch (Exception e) {
76 try {
77 userTransaction.rollback();
78 } catch (Exception e1) {
79 e1.printStackTrace();
80 }
81 if (e instanceof RuntimeException)
82 throw (RuntimeException) e;
83 else
84 throw new IllegalStateException("Cannot change password", e);
85 }
86 }
87
88 class ChangePasswordDialog extends CmsMessageDialog {
89 private Text oldPassword, newPassword1, newPassword2;
90
91 public ChangePasswordDialog(Shell parentShell, UserAdmin securityService) {
92 super(parentShell, changePassword.lead(), CONFIRM);
93 }
94
95 // protected Point getInitialSize() {
96 // return new Point(400, 450);
97 // }
98
99 protected Control createDialogArea(Composite parent) {
100 Composite dialogarea = (Composite) super.createDialogArea(parent);
101 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
102 Composite composite = new Composite(dialogarea, SWT.NONE);
103 composite.setLayout(new GridLayout(2, false));
104 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
105 oldPassword = createLP(composite, currentPassword.lead());
106 newPassword1 = createLP(composite, newPassword.lead());
107 newPassword2 = createLP(composite, repeatNewPassword.lead());
108
109 // parent.pack();
110 oldPassword.setFocus();
111 return composite;
112 }
113
114 @Override
115 protected void okPressed() {
116 try {
117 if (!newPassword1.getText().equals(newPassword2.getText()))
118 throw new IllegalArgumentException("New passwords are different");
119 changePassword(oldPassword.getTextChars(), newPassword1.getTextChars());
120 closeShell(OK);
121 } catch (Exception e) {
122 CmsFeedback.error("Cannot change password", e);
123 }
124 }
125
126 /** Creates label and password. */
127 protected Text createLP(Composite parent, String label) {
128 new Label(parent, SWT.NONE).setText(label);
129 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.PASSWORD | SWT.BORDER);
130 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
131 return text;
132 }
133
134 }
135
136 }