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