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
.security
.AccessController
;
10 import java
.util
.Arrays
;
12 import javax
.inject
.Inject
;
13 import javax
.naming
.InvalidNameException
;
14 import javax
.naming
.ldap
.LdapName
;
15 import javax
.security
.auth
.Subject
;
16 import javax
.security
.auth
.x500
.X500Principal
;
17 import javax
.transaction
.UserTransaction
;
19 import org
.argeo
.cms
.CmsException
;
20 import org
.argeo
.cms
.ui
.dialogs
.CmsMessageDialog
;
21 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
22 import org
.argeo
.node
.security
.CryptoKeyring
;
23 import org
.eclipse
.e4
.core
.di
.annotations
.Execute
;
24 import org
.eclipse
.jface
.dialogs
.Dialog
;
25 import org
.eclipse
.swt
.SWT
;
26 import org
.eclipse
.swt
.graphics
.Point
;
27 import org
.eclipse
.swt
.layout
.GridData
;
28 import org
.eclipse
.swt
.layout
.GridLayout
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Control
;
31 import org
.eclipse
.swt
.widgets
.Display
;
32 import org
.eclipse
.swt
.widgets
.Label
;
33 import org
.eclipse
.swt
.widgets
.Shell
;
34 import org
.eclipse
.swt
.widgets
.Text
;
35 import org
.osgi
.service
.useradmin
.User
;
36 import org
.osgi
.service
.useradmin
.UserAdmin
;
38 public class ChangePassword
{
40 private UserAdmin userAdmin
;
42 private UserTransaction userTransaction
;
44 private CryptoKeyring keyring
= null;
47 public void execute() {
48 ChangePasswordDialog dialog
= new ChangePasswordDialog(Display
.getCurrent().getActiveShell(), userAdmin
);
49 if (dialog
.open() == Dialog
.OK
) {
50 new CmsMessageDialog(Display
.getCurrent().getActiveShell(), passwordChanged
.lead(),
51 CmsMessageDialog
.INFORMATION
).open();
55 protected void changePassword(char[] oldPassword
, char[] newPassword
) {
56 Subject subject
= Subject
.getSubject(AccessController
.getContext());
57 String name
= subject
.getPrincipals(X500Principal
.class).iterator().next().toString();
60 dn
= new LdapName(name
);
61 } catch (InvalidNameException e
) {
62 throw new CmsException("Invalid user dn " + name
, e
);
64 User user
= (User
) userAdmin
.getRole(dn
.toString());
65 if (!user
.hasCredential(null, oldPassword
))
66 throw new CmsException("Invalid password");
67 if (Arrays
.equals(newPassword
, new char[0]))
68 throw new CmsException("New password empty");
70 userTransaction
.begin();
71 user
.getCredentials().put(null, newPassword
);
72 if (keyring
!= null) {
73 keyring
.changePassword(oldPassword
, newPassword
);
74 // TODO change secret keys in the CMS session
76 userTransaction
.commit();
77 } catch (Exception e
) {
79 userTransaction
.rollback();
80 } catch (Exception e1
) {
83 if (e
instanceof RuntimeException
)
84 throw (RuntimeException
) e
;
86 throw new CmsException("Cannot change password", e
);
90 class ChangePasswordDialog
extends CmsMessageDialog
{
91 private Text oldPassword
, newPassword1
, newPassword2
;
93 public ChangePasswordDialog(Shell parentShell
, UserAdmin securityService
) {
94 super(parentShell
, changePassword
.lead(), CONFIRM
);
97 protected Point
getInitialSize() {
98 return new Point(400, 450);
101 protected Control
createDialogArea(Composite parent
) {
102 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
103 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
104 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
105 composite
.setLayout(new GridLayout(2, false));
106 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
107 oldPassword
= createLP(composite
, currentPassword
.lead());
108 newPassword1
= createLP(composite
, newPassword
.lead());
109 newPassword2
= createLP(composite
, repeatNewPassword
.lead());
112 oldPassword
.setFocus();
117 protected void okPressed() {
119 if (!newPassword1
.getText().equals(newPassword2
.getText()))
120 throw new CmsException("New passwords are different");
121 changePassword(oldPassword
.getTextChars(), newPassword1
.getTextChars());
123 } catch (Exception e
) {
124 ErrorFeedback
.show("Cannot change password", e
);
128 /** Creates label and password. */
129 protected Text
createLP(Composite parent
, String label
) {
130 new Label(parent
, SWT
.NONE
).setText(label
);
131 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.PASSWORD
| SWT
.BORDER
);
132 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));