]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui/src/org/argeo/security/ui/commands/OpenChangePasswordDialog.java
Make CMS login UI more extensible
[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 @SuppressWarnings("unchecked")
67 protected void changePassword(char[] oldPassword, char[] newPassword) {
68 Subject subject = Subject.getSubject(AccessController.getContext());
69 String name = subject.getPrincipals(X500Principal.class).iterator()
70 .next().toString();
71 LdapName dn;
72 try {
73 dn = new LdapName(name);
74 } catch (InvalidNameException e) {
75 throw new ArgeoException("Invalid user dn " + name, e);
76 }
77 try {
78 userTransaction.begin();
79 User user = (User) userAdmin.getRole(dn.toString());
80 if (user.hasCredential(null, oldPassword))
81 user.getCredentials().put(null, newPassword);
82 userTransaction.commit();
83 } catch (Exception e) {
84 try {
85 userTransaction.rollback();
86 } catch (Exception e1) {
87 log.error("Could not roll back", e1);
88 }
89 if (e instanceof RuntimeException)
90 throw (RuntimeException) e;
91 else
92 throw new ArgeoException("Cannot change password", e);
93 }
94 }
95
96 public void setUserAdmin(UserAdmin userDetailsManager) {
97 this.userAdmin = userDetailsManager;
98 }
99
100 public void setUserTransaction(UserTransaction userTransaction) {
101 this.userTransaction = userTransaction;
102 }
103
104 class ChangePasswordDialog extends TitleAreaDialog {
105 private static final long serialVersionUID = -6963970583882720962L;
106 private Text currentPassword, 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,
119 true));
120 Composite composite = new Composite(dialogarea, SWT.NONE);
121 composite.setLayout(new GridLayout(2, false));
122 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
123 false));
124 currentPassword = createLP(composite, "Current password");
125 newPassword1 = createLP(composite, "New password");
126 newPassword2 = createLP(composite, "Repeat new password");
127
128 setMessage("Change password", IMessageProvider.INFORMATION);
129 parent.pack();
130 return composite;
131 }
132
133 @Override
134 protected void okPressed() {
135 if (!newPassword1.getText().equals(newPassword2.getText()))
136 throw new ArgeoException("Passwords are different");
137 try {
138 changePassword(currentPassword.getTextChars(),
139 newPassword1.getTextChars());
140 close();
141 } catch (Exception e) {
142 MessageDialog.openError(newPassword1.getShell(), "Error",
143 "Cannot change password");
144 e.printStackTrace();
145 }
146 }
147
148 /** Creates label and password. */
149 protected Text createLP(Composite parent, String label) {
150 new Label(parent, SWT.NONE).setText(label);
151 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.PASSWORD
152 | SWT.BORDER);
153 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
154 return text;
155 }
156
157 protected void configureShell(Shell shell) {
158 super.configureShell(shell);
159 shell.setText("Change password");
160 }
161
162 }
163 }