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
.Status
;
21 import javax
.transaction
.UserTransaction
;
23 import org
.argeo
.ArgeoException
;
24 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
25 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
26 import org
.argeo
.jcr
.ArgeoNames
;
27 import org
.argeo
.security
.UserAdminService
;
28 import org
.argeo
.security
.ui
.admin
.SecurityAdminPlugin
;
29 import org
.argeo
.security
.ui
.admin
.internal
.UiAdminUtils
;
30 import org
.argeo
.security
.ui
.admin
.internal
.UserAdminConstants
;
31 import org
.argeo
.security
.ui
.admin
.views
.UsersView
;
32 import org
.eclipse
.core
.commands
.AbstractHandler
;
33 import org
.eclipse
.core
.commands
.ExecutionEvent
;
34 import org
.eclipse
.core
.commands
.ExecutionException
;
35 import org
.eclipse
.jface
.window
.Window
;
36 import org
.eclipse
.jface
.wizard
.Wizard
;
37 import org
.eclipse
.jface
.wizard
.WizardDialog
;
38 import org
.eclipse
.jface
.wizard
.WizardPage
;
39 import org
.eclipse
.swt
.SWT
;
40 import org
.eclipse
.swt
.events
.ModifyEvent
;
41 import org
.eclipse
.swt
.events
.ModifyListener
;
42 import org
.eclipse
.swt
.layout
.GridLayout
;
43 import org
.eclipse
.swt
.widgets
.Composite
;
44 import org
.eclipse
.swt
.widgets
.Text
;
45 import org
.eclipse
.ui
.IWorkbenchPage
;
46 import org
.eclipse
.ui
.IWorkbenchPart
;
47 import org
.eclipse
.ui
.IWorkbenchWindow
;
48 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
49 import org
.osgi
.service
.useradmin
.Role
;
50 import org
.osgi
.service
.useradmin
.User
;
51 import org
.osgi
.service
.useradmin
.UserAdmin
;
53 /** Open a wizard that enables creation of a new user. */
54 public class NewUser
extends AbstractHandler
{
55 // private final static Log log = LogFactory.getLog(NewUser.class);
56 public final static String ID
= SecurityAdminPlugin
.PLUGIN_ID
+ ".newUser";
58 /* DEPENDENCY INJECTION */
59 private UserAdmin userAdmin
;
60 private UserTransaction userTransaction
;
62 // TODO implement a dynamic choice of the base dn
63 private String
getDn(String uid
) {
64 return "uid=" + uid
+ ",ou=users,dc=example,dc=com";
67 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
68 NewUserWizard newUserWizard
= new NewUserWizard();
69 WizardDialog dialog
= new WizardDialog(
70 HandlerUtil
.getActiveShell(event
), newUserWizard
);
72 // Force refresh until the listener are implemented
73 if (Window
.OK
== dialog
.open())
78 private void forceRefresh(ExecutionEvent event
) {
79 IWorkbenchWindow iww
= HandlerUtil
.getActiveWorkbenchWindow(event
);
82 IWorkbenchPage activePage
= iww
.getActivePage();
83 IWorkbenchPart part
= activePage
.getActivePart();
84 if (part
instanceof UsersView
)
85 ((UsersView
) part
).refresh();
88 private class NewUserWizard
extends Wizard
{
91 private MainUserInfoWizardPage mainUserInfo
;
94 private Text dNameTxt
, usernameTxt
, firstNameTxt
, lastNameTxt
,
95 primaryMailTxt
, pwd1Txt
, pwd2Txt
;
97 public NewUserWizard() {
101 public void addPages() {
102 mainUserInfo
= new MainUserInfoWizardPage();
103 addPage(mainUserInfo
);
104 String message
= "Dummy wizard to ease user creation tests:\n Mail and last name are automatically "
105 + "generated form the uid. Password are defauted to 'demo'.";
106 mainUserInfo
.setMessage(message
, WizardPage
.WARNING
);
109 @SuppressWarnings({ "rawtypes", "unchecked" })
111 public boolean performFinish() {
114 String username
= mainUserInfo
.getUsername();
116 // Begin transaction if needed
118 if (userTransaction
.getStatus() == Status
.STATUS_NO_TRANSACTION
)
119 userTransaction
.begin();
120 } catch (Exception e
) {
121 throw new ArgeoException("Unable to start "
122 + "transaction to create user " + username
, e
);
126 char[] password
= mainUserInfo
.getPassword();
127 User user
= (User
) userAdmin
.createRole(getDn(username
),
130 Dictionary props
= user
.getProperties();
132 String lastNameStr
= lastNameTxt
.getText();
133 if (UiAdminUtils
.notNull(lastNameStr
))
134 props
.put(UserAdminConstants
.KEY_LASTNAME
, lastNameStr
);
136 String firstNameStr
= firstNameTxt
.getText();
137 if (UiAdminUtils
.notNull(firstNameStr
))
138 props
.put(UserAdminConstants
.KEY_FIRSTNAME
, firstNameStr
);
140 String cn
= UiAdminUtils
141 .getDefaultCn(firstNameStr
, lastNameStr
);
142 if (UiAdminUtils
.notNull(cn
))
143 props
.put(UserAdminConstants
.KEY_CN
, cn
);
145 String mailStr
= primaryMailTxt
.getText();
146 if (UiAdminUtils
.notNull(mailStr
))
147 props
.put(UserAdminConstants
.KEY_MAIL
, mailStr
);
149 // TODO MANAGE Password
150 // pwd1Txt.getText();
152 } catch (Exception e
) {
153 ErrorFeedback
.show("Cannot create new user " + username
, e
);
158 private class MainUserInfoWizardPage
extends WizardPage
implements
159 ModifyListener
, ArgeoNames
{
160 private static final long serialVersionUID
= -3150193365151601807L;
162 public MainUserInfoWizardPage() {
164 setTitle("Required Information");
168 public void createControl(Composite parent
) {
169 Composite composite
= new Composite(parent
, SWT
.NONE
);
170 composite
.setLayout(new GridLayout(2, false));
171 dNameTxt
= EclipseUiUtils
.createGridLT(composite
,
172 "Distinguished name", this);
173 dNameTxt
.setEnabled(false);
174 usernameTxt
= EclipseUiUtils
.createGridLT(composite
,
176 usernameTxt
.addModifyListener(new ModifyListener() {
177 private static final long serialVersionUID
= -1435351236582736843L;
180 public void modifyText(ModifyEvent event
) {
181 String name
= usernameTxt
.getText();
182 if (name
.trim().equals("")) {
183 dNameTxt
.setText("");
184 lastNameTxt
.setText("");
185 primaryMailTxt
.setText("");
189 dNameTxt
.setText(getDn(name
));
190 lastNameTxt
.setText(name
.toUpperCase());
191 primaryMailTxt
.setText(name
+ "@example.com");
192 pwd1Txt
.setText("demo");
193 pwd2Txt
.setText("demo");
198 primaryMailTxt
= EclipseUiUtils
.createGridLT(composite
,
200 firstNameTxt
= EclipseUiUtils
.createGridLT(composite
,
202 lastNameTxt
= EclipseUiUtils
.createGridLT(composite
,
204 pwd1Txt
= EclipseUiUtils
.createGridLP(composite
, "Password",
206 pwd2Txt
= EclipseUiUtils
.createGridLP(composite
,
207 "Repeat password", this);
208 setControl(composite
);
210 // Initialize buttons
211 setPageComplete(false);
212 getContainer().updateButtons();
216 public void modifyText(ModifyEvent event
) {
217 String message
= checkComplete();
218 if (message
!= null) {
219 setMessage(message
, WizardPage
.ERROR
);
220 setPageComplete(false);
222 setMessage("Complete", WizardPage
.INFORMATION
);
223 setPageComplete(true);
225 getContainer().updateButtons();
228 /** @return error message or null if complete */
229 protected String
checkComplete() {
230 String name
= usernameTxt
.getText();
232 if (name
.trim().equals(""))
233 return "User name must not be empty";
234 Role role
= userAdmin
.getRole(getDn(name
));
236 return "User " + name
+ " already exists";
237 if (!primaryMailTxt
.getText().matches(
238 UserAdminService
.EMAIL_PATTERN
))
239 return "Not a valid email address";
240 if (lastNameTxt
.getText().trim().equals(""))
241 return "Specify a last name";
242 if (pwd1Txt
.getText().trim().equals(""))
243 return "Specify a password";
244 if (pwd2Txt
.getText().trim().equals(""))
245 return "Repeat the password";
246 if (!pwd2Txt
.getText().equals(pwd1Txt
.getText()))
247 return "Passwords are different";
252 public void setVisible(boolean visible
) {
253 super.setVisible(visible
);
255 usernameTxt
.setFocus();
258 public String
getUsername() {
259 return usernameTxt
.getText();
262 public char[] getPassword() {
263 return pwd1Txt
.getTextChars();
269 /* DEPENDENCY INJECTION */
270 public void setUserAdmin(UserAdmin userAdmin
) {
271 this.userAdmin
= userAdmin
;
274 public void setUserTransaction(UserTransaction userTransaction
) {
275 this.userTransaction
= userTransaction
;