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