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
.admin
.commands
;
18 import java
.util
.Dictionary
;
20 import javax
.transaction
.UserTransaction
;
22 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
23 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
24 import org
.argeo
.jcr
.ArgeoNames
;
25 import org
.argeo
.security
.UserAdminService
;
26 import org
.argeo
.security
.ui
.admin
.SecurityAdminPlugin
;
27 import org
.argeo
.security
.ui
.admin
.internal
.UiAdminUtils
;
28 import org
.argeo
.security
.ui
.admin
.internal
.UserAdminConstants
;
29 import org
.argeo
.security
.ui
.admin
.views
.UsersView
;
30 import org
.eclipse
.core
.commands
.AbstractHandler
;
31 import org
.eclipse
.core
.commands
.ExecutionEvent
;
32 import org
.eclipse
.core
.commands
.ExecutionException
;
33 import org
.eclipse
.jface
.window
.Window
;
34 import org
.eclipse
.jface
.wizard
.Wizard
;
35 import org
.eclipse
.jface
.wizard
.WizardDialog
;
36 import org
.eclipse
.jface
.wizard
.WizardPage
;
37 import org
.eclipse
.swt
.SWT
;
38 import org
.eclipse
.swt
.events
.ModifyEvent
;
39 import org
.eclipse
.swt
.events
.ModifyListener
;
40 import org
.eclipse
.swt
.layout
.GridLayout
;
41 import org
.eclipse
.swt
.widgets
.Composite
;
42 import org
.eclipse
.swt
.widgets
.Text
;
43 import org
.eclipse
.ui
.IWorkbenchPage
;
44 import org
.eclipse
.ui
.IWorkbenchPart
;
45 import org
.eclipse
.ui
.IWorkbenchWindow
;
46 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
47 import org
.osgi
.service
.useradmin
.Role
;
48 import org
.osgi
.service
.useradmin
.User
;
49 import org
.osgi
.service
.useradmin
.UserAdmin
;
51 /** Open a wizard that enables creation of a new user. */
52 public class NewUser
extends AbstractHandler
{
53 // private final static Log log = LogFactory.getLog(NewUser.class);
54 public final static String ID
= SecurityAdminPlugin
.PLUGIN_ID
+ ".newUser";
56 /* DEPENDENCY INJECTION */
57 private UserAdmin userAdmin
;
58 private UserTransaction userTransaction
;
60 // TODO implement a dynamic choice of the base dn
61 private String
getDn(String uid
) {
62 return "uid=" + uid
+ ",ou=users,dc=example,dc=com";
65 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
66 NewUserWizard newUserWizard
= new NewUserWizard();
67 WizardDialog dialog
= new WizardDialog(
68 HandlerUtil
.getActiveShell(event
), newUserWizard
);
70 // Force refresh until the listener are implemented
71 if (Window
.OK
== dialog
.open())
76 private void forceRefresh(ExecutionEvent event
) {
77 IWorkbenchWindow iww
= HandlerUtil
.getActiveWorkbenchWindow(event
);
80 IWorkbenchPage activePage
= iww
.getActivePage();
81 IWorkbenchPart part
= activePage
.getActivePart();
82 if (part
instanceof UsersView
)
83 ((UsersView
) part
).refresh();
86 private class NewUserWizard
extends Wizard
{
89 private MainUserInfoWizardPage mainUserInfo
;
92 private Text dNameTxt
, usernameTxt
, firstNameTxt
, lastNameTxt
,
93 primaryMailTxt
, pwd1Txt
, pwd2Txt
;
95 public NewUserWizard() {
99 public void addPages() {
100 mainUserInfo
= new MainUserInfoWizardPage();
101 addPage(mainUserInfo
);
102 String message
= "Dummy wizard to ease user creation tests:\n Mail and last name are automatically "
103 + "generated form the uid. Password are defauted to 'demo'.";
104 mainUserInfo
.setMessage(message
, WizardPage
.WARNING
);
107 @SuppressWarnings({ "rawtypes", "unchecked" })
109 public boolean performFinish() {
112 String username
= mainUserInfo
.getUsername();
114 UiAdminUtils
.beginTransactionIfNeeded(userTransaction
);
115 char[] password
= mainUserInfo
.getPassword();
116 User user
= (User
) userAdmin
.createRole(getDn(username
),
119 Dictionary props
= user
.getProperties();
121 String lastNameStr
= lastNameTxt
.getText();
122 if (UiAdminUtils
.notNull(lastNameStr
))
123 props
.put(UserAdminConstants
.KEY_LASTNAME
, lastNameStr
);
125 String firstNameStr
= firstNameTxt
.getText();
126 if (UiAdminUtils
.notNull(firstNameStr
))
127 props
.put(UserAdminConstants
.KEY_FIRSTNAME
, firstNameStr
);
129 String cn
= UiAdminUtils
130 .getDefaultCn(firstNameStr
, lastNameStr
);
131 if (UiAdminUtils
.notNull(cn
))
132 props
.put(UserAdminConstants
.KEY_CN
, cn
);
134 String mailStr
= primaryMailTxt
.getText();
135 if (UiAdminUtils
.notNull(mailStr
))
136 props
.put(UserAdminConstants
.KEY_MAIL
, mailStr
);
138 user
.getCredentials().put(null, password
);
140 } catch (Exception e
) {
141 ErrorFeedback
.show("Cannot create new user " + username
, e
);
146 private class MainUserInfoWizardPage
extends WizardPage
implements
147 ModifyListener
, ArgeoNames
{
148 private static final long serialVersionUID
= -3150193365151601807L;
150 public MainUserInfoWizardPage() {
152 setTitle("Required Information");
156 public void createControl(Composite parent
) {
157 Composite composite
= new Composite(parent
, SWT
.NONE
);
158 composite
.setLayout(new GridLayout(2, false));
159 dNameTxt
= EclipseUiUtils
.createGridLT(composite
,
160 "Distinguished name", this);
161 dNameTxt
.setEnabled(false);
162 usernameTxt
= EclipseUiUtils
.createGridLT(composite
,
164 usernameTxt
.addModifyListener(new ModifyListener() {
165 private static final long serialVersionUID
= -1435351236582736843L;
168 public void modifyText(ModifyEvent event
) {
169 String name
= usernameTxt
.getText();
170 if (name
.trim().equals("")) {
171 dNameTxt
.setText("");
172 lastNameTxt
.setText("");
173 primaryMailTxt
.setText("");
177 dNameTxt
.setText(getDn(name
));
178 lastNameTxt
.setText(name
.toUpperCase());
179 primaryMailTxt
.setText(name
+ "@example.com");
180 pwd1Txt
.setText("demo");
181 pwd2Txt
.setText("demo");
186 primaryMailTxt
= EclipseUiUtils
.createGridLT(composite
,
188 firstNameTxt
= EclipseUiUtils
.createGridLT(composite
,
190 lastNameTxt
= EclipseUiUtils
.createGridLT(composite
,
192 pwd1Txt
= EclipseUiUtils
.createGridLP(composite
, "Password",
194 pwd2Txt
= EclipseUiUtils
.createGridLP(composite
,
195 "Repeat password", this);
196 setControl(composite
);
198 // Initialize buttons
199 setPageComplete(false);
200 getContainer().updateButtons();
204 public void modifyText(ModifyEvent event
) {
205 String message
= checkComplete();
206 if (message
!= null) {
207 setMessage(message
, WizardPage
.ERROR
);
208 setPageComplete(false);
210 setMessage("Complete", WizardPage
.INFORMATION
);
211 setPageComplete(true);
213 getContainer().updateButtons();
216 /** @return error message or null if complete */
217 protected String
checkComplete() {
218 String name
= usernameTxt
.getText();
220 if (name
.trim().equals(""))
221 return "User name must not be empty";
222 Role role
= userAdmin
.getRole(getDn(name
));
224 return "User " + name
+ " already exists";
225 if (!primaryMailTxt
.getText().matches(
226 UserAdminService
.EMAIL_PATTERN
))
227 return "Not a valid email address";
228 if (lastNameTxt
.getText().trim().equals(""))
229 return "Specify a last name";
230 if (pwd1Txt
.getText().trim().equals(""))
231 return "Specify a password";
232 if (pwd2Txt
.getText().trim().equals(""))
233 return "Repeat the password";
234 if (!pwd2Txt
.getText().equals(pwd1Txt
.getText()))
235 return "Passwords are different";
240 public void setVisible(boolean visible
) {
241 super.setVisible(visible
);
243 usernameTxt
.setFocus();
246 public String
getUsername() {
247 return usernameTxt
.getText();
250 public char[] getPassword() {
251 return pwd1Txt
.getTextChars();
257 /* DEPENDENCY INJECTION */
258 public void setUserAdmin(UserAdmin userAdmin
) {
259 this.userAdmin
= userAdmin
;
262 public void setUserTransaction(UserTransaction userTransaction
) {
263 this.userTransaction
= userTransaction
;