1 package org
.argeo
.cms
.e4
.handlers
;
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
;
9 import java
.util
.Arrays
;
11 import javax
.inject
.Inject
;
12 import javax
.naming
.InvalidNameException
;
13 import javax
.naming
.ldap
.LdapName
;
15 import org
.argeo
.api
.cms
.keyring
.CryptoKeyring
;
16 import org
.argeo
.api
.cms
.transaction
.WorkTransaction
;
17 import org
.argeo
.cms
.CurrentUser
;
18 import org
.argeo
.cms
.swt
.dialogs
.CmsFeedback
;
19 import org
.argeo
.cms
.swt
.dialogs
.CmsMessageDialog
;
20 import org
.argeo
.cms
.ux
.widgets
.CmsDialog
;
21 import org
.eclipse
.e4
.core
.di
.annotations
.Execute
;
22 import org
.eclipse
.e4
.core
.di
.annotations
.Optional
;
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
;
35 /** Change the password of the logged-in user. */
36 public class ChangePassword
{
38 private UserAdmin userAdmin
;
40 private WorkTransaction userTransaction
;
43 private CryptoKeyring keyring
= null;
46 public void execute() {
47 ChangePasswordDialog dialog
= new ChangePasswordDialog(Display
.getCurrent().getActiveShell(), userAdmin
);
48 if (dialog
.open() == CmsDialog
.OK
) {
49 new CmsMessageDialog(Display
.getCurrent().getActiveShell(), passwordChanged
.lead(),
50 CmsMessageDialog
.INFORMATION
).open();
54 protected void changePassword(char[] oldPassword
, char[] newPassword
) {
55 String name
= CurrentUser
.getUsername();
58 dn
= new LdapName(name
);
59 } catch (InvalidNameException e
) {
60 throw new IllegalArgumentException("Invalid user dn " + name
, e
);
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");
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
74 userTransaction
.commit();
75 } catch (Exception e
) {
77 userTransaction
.rollback();
78 } catch (Exception e1
) {
81 if (e
instanceof RuntimeException
)
82 throw (RuntimeException
) e
;
84 throw new IllegalStateException("Cannot change password", e
);
88 class ChangePasswordDialog
extends CmsMessageDialog
{
89 private Text oldPassword
, newPassword1
, newPassword2
;
91 public ChangePasswordDialog(Shell parentShell
, UserAdmin securityService
) {
92 super(parentShell
, changePassword
.lead(), CONFIRM
);
95 // protected Point getInitialSize() {
96 // return new Point(400, 450);
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());
110 oldPassword
.setFocus();
115 protected void okPressed() {
117 if (!newPassword1
.getText().equals(newPassword2
.getText()))
118 throw new IllegalArgumentException("New passwords are different");
119 changePassword(oldPassword
.getTextChars(), newPassword1
.getTextChars());
120 closeShell(CmsDialog
.OK
);
121 } catch (Exception e
) {
122 CmsFeedback
.error("Cannot change password", e
);
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));