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 // TODO MANAGE Password
139 // pwd1Txt.getText();
141 } catch (Exception e
) {
142 ErrorFeedback
.show("Cannot create new user " + username
, e
);
147 private class MainUserInfoWizardPage
extends WizardPage
implements
148 ModifyListener
, ArgeoNames
{
149 private static final long serialVersionUID
= -3150193365151601807L;
151 public MainUserInfoWizardPage() {
153 setTitle("Required Information");
157 public void createControl(Composite parent
) {
158 Composite composite
= new Composite(parent
, SWT
.NONE
);
159 composite
.setLayout(new GridLayout(2, false));
160 dNameTxt
= EclipseUiUtils
.createGridLT(composite
,
161 "Distinguished name", this);
162 dNameTxt
.setEnabled(false);
163 usernameTxt
= EclipseUiUtils
.createGridLT(composite
,
165 usernameTxt
.addModifyListener(new ModifyListener() {
166 private static final long serialVersionUID
= -1435351236582736843L;
169 public void modifyText(ModifyEvent event
) {
170 String name
= usernameTxt
.getText();
171 if (name
.trim().equals("")) {
172 dNameTxt
.setText("");
173 lastNameTxt
.setText("");
174 primaryMailTxt
.setText("");
178 dNameTxt
.setText(getDn(name
));
179 lastNameTxt
.setText(name
.toUpperCase());
180 primaryMailTxt
.setText(name
+ "@example.com");
181 pwd1Txt
.setText("demo");
182 pwd2Txt
.setText("demo");
187 primaryMailTxt
= EclipseUiUtils
.createGridLT(composite
,
189 firstNameTxt
= EclipseUiUtils
.createGridLT(composite
,
191 lastNameTxt
= EclipseUiUtils
.createGridLT(composite
,
193 pwd1Txt
= EclipseUiUtils
.createGridLP(composite
, "Password",
195 pwd2Txt
= EclipseUiUtils
.createGridLP(composite
,
196 "Repeat password", this);
197 setControl(composite
);
199 // Initialize buttons
200 setPageComplete(false);
201 getContainer().updateButtons();
205 public void modifyText(ModifyEvent event
) {
206 String message
= checkComplete();
207 if (message
!= null) {
208 setMessage(message
, WizardPage
.ERROR
);
209 setPageComplete(false);
211 setMessage("Complete", WizardPage
.INFORMATION
);
212 setPageComplete(true);
214 getContainer().updateButtons();
217 /** @return error message or null if complete */
218 protected String
checkComplete() {
219 String name
= usernameTxt
.getText();
221 if (name
.trim().equals(""))
222 return "User name must not be empty";
223 Role role
= userAdmin
.getRole(getDn(name
));
225 return "User " + name
+ " already exists";
226 if (!primaryMailTxt
.getText().matches(
227 UserAdminService
.EMAIL_PATTERN
))
228 return "Not a valid email address";
229 if (lastNameTxt
.getText().trim().equals(""))
230 return "Specify a last name";
231 if (pwd1Txt
.getText().trim().equals(""))
232 return "Specify a password";
233 if (pwd2Txt
.getText().trim().equals(""))
234 return "Repeat the password";
235 if (!pwd2Txt
.getText().equals(pwd1Txt
.getText()))
236 return "Passwords are different";
241 public void setVisible(boolean visible
) {
242 super.setVisible(visible
);
244 usernameTxt
.setFocus();
247 public String
getUsername() {
248 return usernameTxt
.getText();
251 public char[] getPassword() {
252 return pwd1Txt
.getTextChars();
258 /* DEPENDENCY INJECTION */
259 public void setUserAdmin(UserAdmin userAdmin
) {
260 this.userAdmin
= userAdmin
;
263 public void setUserTransaction(UserTransaction userTransaction
) {
264 this.userTransaction
= userTransaction
;