]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui/src/org/argeo/security/ui/commands/OpenChangePasswordDialog.java
Continue framework clean up.
[lgpl/argeo-commons.git] / org.argeo.security.ui / src / org / argeo / security / ui / 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.security.ui.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
27 import javax.naming.InvalidNameException;
28 import javax.naming.ldap.LdapName;
29 import javax.security.auth.Subject;
30 import javax.security.auth.x500.X500Principal;
31 import javax.transaction.UserTransaction;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.argeo.ArgeoException;
36 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
37 import org.eclipse.core.commands.AbstractHandler;
38 import org.eclipse.core.commands.ExecutionEvent;
39 import org.eclipse.core.commands.ExecutionException;
40 import org.eclipse.jface.dialogs.Dialog;
41 import org.eclipse.jface.dialogs.MessageDialog;
42 import org.eclipse.jface.dialogs.TitleAreaDialog;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.graphics.Point;
45 import org.eclipse.swt.layout.GridData;
46 import org.eclipse.swt.layout.GridLayout;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.swt.widgets.Control;
49 import org.eclipse.swt.widgets.Label;
50 import org.eclipse.swt.widgets.Shell;
51 import org.eclipse.swt.widgets.Text;
52 import org.eclipse.ui.handlers.HandlerUtil;
53 import org.osgi.service.useradmin.User;
54 import org.osgi.service.useradmin.UserAdmin;
55
56 /** Opens the change password dialog. */
57 public class OpenChangePasswordDialog extends AbstractHandler {
58 private final static Log log = LogFactory
59 .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(
65 HandlerUtil.getActiveShell(event), userAdmin);
66 if (dialog.open() == Dialog.OK) {
67 MessageDialog.openInformation(HandlerUtil.getActiveShell(event),
68 passwordChanged.lead(), passwordChanged.lead());
69 }
70 return null;
71 }
72
73 @SuppressWarnings("unchecked")
74 protected void changePassword(char[] oldPassword, char[] newPassword) {
75 Subject subject = Subject.getSubject(AccessController.getContext());
76 String name = subject.getPrincipals(X500Principal.class).iterator()
77 .next().toString();
78 LdapName dn;
79 try {
80 dn = new LdapName(name);
81 } catch (InvalidNameException e) {
82 throw new ArgeoException("Invalid user dn " + name, e);
83 }
84 User user = (User) userAdmin.getRole(dn.toString());
85 if (!user.hasCredential(null, oldPassword))
86 throw new ArgeoException("Invalid password");
87 if (newPassword.equals(""))
88 throw new ArgeoException("New password empty");
89 try {
90 userTransaction.begin();
91 user.getCredentials().put(null, newPassword);
92 userTransaction.commit();
93 } catch (Exception e) {
94 try {
95 userTransaction.rollback();
96 } catch (Exception e1) {
97 log.error("Could not roll back", e1);
98 }
99 if (e instanceof RuntimeException)
100 throw (RuntimeException) e;
101 else
102 throw new ArgeoException("Cannot change password", e);
103 }
104 }
105
106 public void setUserAdmin(UserAdmin userDetailsManager) {
107 this.userAdmin = userDetailsManager;
108 }
109
110 public void setUserTransaction(UserTransaction userTransaction) {
111 this.userTransaction = userTransaction;
112 }
113
114 class ChangePasswordDialog extends TitleAreaDialog {
115 private static final long serialVersionUID = -6963970583882720962L;
116 private Text oldPassword, newPassword1, newPassword2;
117
118 public ChangePasswordDialog(Shell parentShell, UserAdmin securityService) {
119 super(parentShell);
120 }
121
122 protected Point getInitialSize() {
123 return new Point(400, 450);
124 }
125
126 protected Control createDialogArea(Composite parent) {
127 Composite dialogarea = (Composite) super.createDialogArea(parent);
128 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
129 true));
130 Composite composite = new Composite(dialogarea, SWT.NONE);
131 composite.setLayout(new GridLayout(2, false));
132 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
133 false));
134 oldPassword = createLP(composite, currentPassword.lead());
135 newPassword1 = createLP(composite, newPassword.lead());
136 newPassword2 = createLP(composite, repeatNewPassword.lead());
137
138 setMessage(changePassword.lead(), INFORMATION);
139 parent.pack();
140 return composite;
141 }
142
143 @Override
144 protected void okPressed() {
145 try {
146 if (!newPassword1.getText().equals(newPassword2.getText()))
147 throw new ArgeoException("New passwords are different");
148 changePassword(oldPassword.getTextChars(),
149 newPassword1.getTextChars());
150 close();
151 } catch (Exception e) {
152 ErrorFeedback.show("Cannot change password", e);
153 }
154 }
155
156 /** Creates label and password. */
157 protected Text createLP(Composite parent, String label) {
158 new Label(parent, SWT.NONE).setText(label);
159 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.PASSWORD
160 | SWT.BORDER);
161 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
162 return text;
163 }
164
165 protected void configureShell(Shell shell) {
166 super.configureShell(shell);
167 shell.setText(changePassword.lead());
168 }
169
170 }
171 }