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
.cms
.ui
.workbench
.commands
;
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
;
25 import java
.security
.AccessController
;
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
;
33 import org
.apache
.commons
.logging
.Log
;
34 import org
.apache
.commons
.logging
.LogFactory
;
35 import org
.argeo
.cms
.CmsException
;
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
;
56 /** Open the change password dialog */
57 public class OpenChangePasswordDialog
extends AbstractHandler
{
58 private final static Log log
= LogFactory
.getLog(OpenChangePasswordDialog
.class);
59 private UserAdmin userAdmin
;
60 private UserTransaction userTransaction
;
62 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
63 ChangePasswordDialog dialog
= new ChangePasswordDialog(HandlerUtil
.getActiveShell(event
), userAdmin
);
64 if (dialog
.open() == Dialog
.OK
) {
65 MessageDialog
.openInformation(HandlerUtil
.getActiveShell(event
), passwordChanged
.lead(),
66 passwordChanged
.lead());
71 @SuppressWarnings("unchecked")
72 protected void changePassword(char[] oldPassword
, char[] newPassword
) {
73 Subject subject
= Subject
.getSubject(AccessController
.getContext());
74 String name
= subject
.getPrincipals(X500Principal
.class).iterator().next().toString();
77 dn
= new LdapName(name
);
78 } catch (InvalidNameException e
) {
79 throw new CmsException("Invalid user dn " + name
, e
);
81 User user
= (User
) userAdmin
.getRole(dn
.toString());
82 if (!user
.hasCredential(null, oldPassword
))
83 throw new CmsException("Invalid password");
84 if (newPassword
.equals(""))
85 throw new CmsException("New password empty");
87 userTransaction
.begin();
88 user
.getCredentials().put(null, newPassword
);
89 userTransaction
.commit();
90 } catch (Exception e
) {
92 userTransaction
.rollback();
93 } catch (Exception e1
) {
94 log
.error("Could not roll back", e1
);
96 if (e
instanceof RuntimeException
)
97 throw (RuntimeException
) e
;
99 throw new CmsException("Cannot change password", e
);
103 class ChangePasswordDialog
extends TitleAreaDialog
{
104 private static final long serialVersionUID
= -6963970583882720962L;
105 private Text oldPassword
, newPassword1
, newPassword2
;
107 public ChangePasswordDialog(Shell parentShell
, UserAdmin securityService
) {
111 protected Point
getInitialSize() {
112 return new Point(400, 450);
115 protected Control
createDialogArea(Composite parent
) {
116 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
117 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
118 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
119 composite
.setLayout(new GridLayout(2, false));
120 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
121 oldPassword
= createLP(composite
, currentPassword
.lead());
122 newPassword1
= createLP(composite
, newPassword
.lead());
123 newPassword2
= createLP(composite
, repeatNewPassword
.lead());
125 setMessage(changePassword
.lead(), INFORMATION
);
131 protected void okPressed() {
133 if (!newPassword1
.getText().equals(newPassword2
.getText()))
134 throw new CmsException("New passwords are different");
135 changePassword(oldPassword
.getTextChars(), newPassword1
.getTextChars());
137 } catch (Exception e
) {
138 ErrorFeedback
.show("Cannot change password", e
);
142 /** Creates label and password. */
143 protected Text
createLP(Composite parent
, String label
) {
144 new Label(parent
, SWT
.NONE
).setText(label
);
145 Text text
= new Text(parent
, SWT
.SINGLE
| SWT
.LEAD
| SWT
.PASSWORD
| SWT
.BORDER
);
146 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
150 protected void configureShell(Shell shell
) {
151 super.configureShell(shell
);
152 shell
.setText(changePassword
.lead());
156 public void setUserAdmin(UserAdmin userAdmin
) {
157 this.userAdmin
= userAdmin
;
160 public void setUserTransaction(UserTransaction userTransaction
) {
161 this.userTransaction
= userTransaction
;