2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.security
.ui
.commands
;
18 import java
.security
.AccessController
;
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
;
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
;
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
;
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.");
66 protected void changePassword(char[] oldPassword
, char[] newPassword
) {
67 Subject subject
= Subject
.getSubject(AccessController
.getContext());
68 String name
= subject
.getPrincipals(X500Principal
.class).iterator()
72 dn
= new LdapName(name
);
73 } catch (InvalidNameException e
) {
74 throw new ArgeoException("Invalid user dn " + name
, e
);
77 userTransaction
.begin();
78 User user
= (User
) userAdmin
.getRole(dn
.toString());
79 if (user
.hasCredential(null, oldPassword
)) {
80 user
.getCredentials().put(null, newPassword
);
82 userTransaction
.commit();
83 } catch (Exception e
) {
85 userTransaction
.rollback();
86 } catch (Exception e1
) {
87 log
.error("Could not roll back", e1
);
89 if (e
instanceof RuntimeException
)
90 throw (RuntimeException
) e
;
92 throw new ArgeoException("Cannot change password", e
);
96 public void setUserAdmin(UserAdmin userDetailsManager
) {
97 this.userAdmin
= userDetailsManager
;
100 public void setUserTransaction(UserTransaction userTransaction
) {
101 this.userTransaction
= userTransaction
;
104 class ChangePasswordDialog
extends TitleAreaDialog
{
105 private static final long serialVersionUID
= -6963970583882720962L;
106 private Text currentPassword
, newPassword1
, newPassword2
;
108 public ChangePasswordDialog(Shell parentShell
, UserAdmin securityService
) {
112 protected Point
getInitialSize() {
113 return new Point(400, 450);
116 protected Control
createDialogArea(Composite parent
) {
117 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
118 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, 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,
124 currentPassword
= createLP(composite
, "Current password");
125 newPassword1
= createLP(composite
, "New password");
126 newPassword2
= createLP(composite
, "Repeat new password");
128 setMessage("Change password", IMessageProvider
.INFORMATION
);
134 protected void okPressed() {
135 if (!newPassword1
.getText().equals(newPassword2
.getText()))
136 throw new ArgeoException("Passwords are different");
138 changePassword(currentPassword
.getTextChars(),
139 newPassword1
.getTextChars());
141 } catch (Exception e
) {
142 MessageDialog
.openError(newPassword1
.getShell(), "Error",
143 "Cannot change password");
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
153 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
157 protected void configureShell(Shell shell
) {
158 super.configureShell(shell
);
159 shell
.setText("Change password");