]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/commands/OpenChangePasswordDialog.java
64f4ff9b800f1b1f15990b55d2c94b9d9f84be2b
[lgpl/argeo-commons.git] / org.argeo.cms.ui.workbench / src / org / argeo / cms / ui / workbench / commands / OpenChangePasswordDialog.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.cms.ui.workbench.commands;
17
18 import static org.argeo.cms.CmsMsg.changePassword;
19 import static org.argeo.cms.CmsMsg.currentPassword;
20 import static org.argeo.cms.CmsMsg.newPassword;
21 import static org.argeo.cms.CmsMsg.passwordChanged;
22 import static org.argeo.cms.CmsMsg.repeatNewPassword;
23 import static org.eclipse.jface.dialogs.IMessageProvider.INFORMATION;
24
25 import java.security.AccessController;
26 import java.util.Arrays;
27
28 import javax.naming.InvalidNameException;
29 import javax.naming.ldap.LdapName;
30 import javax.security.auth.Subject;
31 import javax.security.auth.x500.X500Principal;
32 import javax.transaction.UserTransaction;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.argeo.cms.CmsException;
37 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
38 import org.eclipse.core.commands.AbstractHandler;
39 import org.eclipse.core.commands.ExecutionEvent;
40 import org.eclipse.core.commands.ExecutionException;
41 import org.eclipse.jface.dialogs.Dialog;
42 import org.eclipse.jface.dialogs.MessageDialog;
43 import org.eclipse.jface.dialogs.TitleAreaDialog;
44 import org.eclipse.swt.SWT;
45 import org.eclipse.swt.graphics.Point;
46 import org.eclipse.swt.layout.GridData;
47 import org.eclipse.swt.layout.GridLayout;
48 import org.eclipse.swt.widgets.Composite;
49 import org.eclipse.swt.widgets.Control;
50 import org.eclipse.swt.widgets.Label;
51 import org.eclipse.swt.widgets.Shell;
52 import org.eclipse.swt.widgets.Text;
53 import org.eclipse.ui.handlers.HandlerUtil;
54 import org.osgi.service.useradmin.User;
55 import org.osgi.service.useradmin.UserAdmin;
56
57 /** Open the change password dialog */
58 public class OpenChangePasswordDialog extends AbstractHandler {
59 private final static Log log = LogFactory.getLog(OpenChangePasswordDialog.class);
60 private UserAdmin userAdmin;
61 private UserTransaction userTransaction;
62
63 public Object execute(ExecutionEvent event) throws ExecutionException {
64 ChangePasswordDialog dialog = new ChangePasswordDialog(HandlerUtil.getActiveShell(event), userAdmin);
65 if (dialog.open() == Dialog.OK) {
66 MessageDialog.openInformation(HandlerUtil.getActiveShell(event), passwordChanged.lead(),
67 passwordChanged.lead());
68 }
69 return null;
70 }
71
72 @SuppressWarnings("unchecked")
73 protected void changePassword(char[] oldPassword, char[] newPassword) {
74 Subject subject = Subject.getSubject(AccessController.getContext());
75 String name = subject.getPrincipals(X500Principal.class).iterator().next().toString();
76 LdapName dn;
77 try {
78 dn = new LdapName(name);
79 } catch (InvalidNameException e) {
80 throw new CmsException("Invalid user dn " + name, e);
81 }
82 User user = (User) userAdmin.getRole(dn.toString());
83 if (!user.hasCredential(null, oldPassword))
84 throw new CmsException("Invalid password");
85 if (Arrays.equals(newPassword, new char[0]))
86 throw new CmsException("New password empty");
87 try {
88 userTransaction.begin();
89 user.getCredentials().put(null, newPassword);
90 userTransaction.commit();
91 } catch (Exception e) {
92 try {
93 userTransaction.rollback();
94 } catch (Exception e1) {
95 log.error("Could not roll back", e1);
96 }
97 if (e instanceof RuntimeException)
98 throw (RuntimeException) e;
99 else
100 throw new CmsException("Cannot change password", e);
101 }
102 }
103
104 class ChangePasswordDialog extends TitleAreaDialog {
105 private static final long serialVersionUID = -6963970583882720962L;
106 private Text oldPassword, newPassword1, newPassword2;
107
108 public ChangePasswordDialog(Shell parentShell, UserAdmin securityService) {
109 super(parentShell);
110 }
111
112 protected Point getInitialSize() {
113 return new Point(400, 450);
114 }
115
116 protected Control createDialogArea(Composite parent) {
117 Composite dialogarea = (Composite) super.createDialogArea(parent);
118 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
119 Composite composite = new Composite(dialogarea, SWT.NONE);
120 composite.setLayout(new GridLayout(2, false));
121 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
122 oldPassword = createLP(composite, currentPassword.lead());
123 newPassword1 = createLP(composite, newPassword.lead());
124 newPassword2 = createLP(composite, repeatNewPassword.lead());
125
126 setMessage(changePassword.lead(), INFORMATION);
127 parent.pack();
128 oldPassword.setFocus();
129 return composite;
130 }
131
132 @Override
133 protected void okPressed() {
134 try {
135 if (!newPassword1.getText().equals(newPassword2.getText()))
136 throw new CmsException("New passwords are different");
137 changePassword(oldPassword.getTextChars(), newPassword1.getTextChars());
138 close();
139 } catch (Exception e) {
140 ErrorFeedback.show("Cannot change password", e);
141 }
142 }
143
144 /** Creates label and password. */
145 protected Text createLP(Composite parent, String label) {
146 new Label(parent, SWT.NONE).setText(label);
147 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.PASSWORD | SWT.BORDER);
148 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
149 return text;
150 }
151
152 protected void configureShell(Shell shell) {
153 super.configureShell(shell);
154 shell.setText(changePassword.lead());
155 }
156 }
157
158 public void setUserAdmin(UserAdmin userAdmin) {
159 this.userAdmin = userAdmin;
160 }
161
162 public void setUserTransaction(UserTransaction userTransaction) {
163 this.userTransaction = userTransaction;
164 }
165 }