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 org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
21 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
22 import org
.argeo
.jcr
.ArgeoNames
;
23 import org
.argeo
.security
.UserAdminService
;
24 import org
.argeo
.security
.ui
.admin
.SecurityAdminPlugin
;
25 import org
.argeo
.security
.ui
.admin
.internal
.UiAdminUtils
;
26 import org
.argeo
.security
.ui
.admin
.internal
.UserAdminConstants
;
27 import org
.argeo
.security
.ui
.admin
.views
.UsersView
;
28 import org
.eclipse
.core
.commands
.AbstractHandler
;
29 import org
.eclipse
.core
.commands
.ExecutionEvent
;
30 import org
.eclipse
.core
.commands
.ExecutionException
;
31 import org
.eclipse
.jface
.window
.Window
;
32 import org
.eclipse
.jface
.wizard
.Wizard
;
33 import org
.eclipse
.jface
.wizard
.WizardDialog
;
34 import org
.eclipse
.jface
.wizard
.WizardPage
;
35 import org
.eclipse
.swt
.SWT
;
36 import org
.eclipse
.swt
.events
.ModifyEvent
;
37 import org
.eclipse
.swt
.events
.ModifyListener
;
38 import org
.eclipse
.swt
.layout
.GridLayout
;
39 import org
.eclipse
.swt
.widgets
.Composite
;
40 import org
.eclipse
.swt
.widgets
.Text
;
41 import org
.eclipse
.ui
.IWorkbenchPage
;
42 import org
.eclipse
.ui
.IWorkbenchPart
;
43 import org
.eclipse
.ui
.IWorkbenchWindow
;
44 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
45 import org
.osgi
.service
.useradmin
.Role
;
46 import org
.osgi
.service
.useradmin
.User
;
47 import org
.osgi
.service
.useradmin
.UserAdmin
;
49 /** Open a wizard that enables creation of a new user. */
50 public class NewUser
extends AbstractHandler
{
51 // private final static Log log = LogFactory.getLog(NewUser.class);
53 public final static String ID
= SecurityAdminPlugin
.PLUGIN_ID
+ ".newUser";
55 private UserAdmin userAdmin
;
57 // TODO implement a dynamic choice of the base dn
58 private String
getDn(String uid
) {
59 return "uid=" + uid
+ ",ou=users,dc=example,dc=com";
62 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
63 NewUserWizard newUserWizard
= new NewUserWizard();
64 WizardDialog dialog
= new WizardDialog(
65 HandlerUtil
.getActiveShell(event
), newUserWizard
);
67 // Force refresh until the listener are implemented
68 if (Window
.OK
== dialog
.open())
73 private void forceRefresh(ExecutionEvent event
) {
74 IWorkbenchWindow iww
= HandlerUtil
.getActiveWorkbenchWindow(event
);
77 IWorkbenchPage activePage
= iww
.getActivePage();
78 IWorkbenchPart part
= activePage
.getActivePart();
79 if (part
instanceof UsersView
)
80 ((UsersView
) part
).refresh();
83 private class NewUserWizard
extends Wizard
{
86 private MainUserInfoWizardPage mainUserInfo
;
89 private Text dNameTxt
, usernameTxt
, firstNameTxt
, lastNameTxt
,
90 primaryMailTxt
, pwd1Txt
, pwd2Txt
;
92 public NewUserWizard() {
96 public void addPages() {
97 mainUserInfo
= new MainUserInfoWizardPage();
98 addPage(mainUserInfo
);
99 String message
= "Dummy wizard to ease user creation tests:\n Mail and last name are automatically "
100 + "generated form the uid. Password are defauted to 'demo'.";
101 mainUserInfo
.setMessage(message
, WizardPage
.WARNING
);
104 @SuppressWarnings({ "rawtypes", "unchecked" })
106 public boolean performFinish() {
109 String username
= mainUserInfo
.getUsername();
111 char[] password
= mainUserInfo
.getPassword();
112 User user
= (User
) userAdmin
.createRole(getDn(username
),
115 Dictionary props
= user
.getProperties();
117 String lastNameStr
= lastNameTxt
.getText();
118 if (UiAdminUtils
.notNull(lastNameStr
))
119 props
.put(UserAdminConstants
.KEY_LASTNAME
, lastNameStr
);
121 String firstNameStr
= firstNameTxt
.getText();
122 if (UiAdminUtils
.notNull(firstNameStr
))
123 props
.put(UserAdminConstants
.KEY_FIRSTNAME
, firstNameStr
);
125 String cn
= UiAdminUtils
126 .getDefaultCn(firstNameStr
, lastNameStr
);
127 if (UiAdminUtils
.notNull(cn
))
128 props
.put(UserAdminConstants
.KEY_CN
, cn
);
130 String mailStr
= primaryMailTxt
.getText();
131 if (UiAdminUtils
.notNull(mailStr
))
132 props
.put(UserAdminConstants
.KEY_MAIL
, mailStr
);
134 // TODO MANAGE Password
135 // pwd1Txt.getText();
137 } catch (Exception e
) {
138 ErrorFeedback
.show("Cannot create new user " + username
, e
);
143 private class MainUserInfoWizardPage
extends WizardPage
implements
144 ModifyListener
, ArgeoNames
{
145 private static final long serialVersionUID
= -3150193365151601807L;
147 public MainUserInfoWizardPage() {
149 setTitle("Required Information");
153 public void createControl(Composite parent
) {
154 Composite composite
= new Composite(parent
, SWT
.NONE
);
155 composite
.setLayout(new GridLayout(2, false));
156 dNameTxt
= EclipseUiUtils
.createGridLT(composite
,
157 "Distinguished name", this);
158 dNameTxt
.setEnabled(false);
159 usernameTxt
= EclipseUiUtils
.createGridLT(composite
,
161 usernameTxt
.addModifyListener(new ModifyListener() {
162 private static final long serialVersionUID
= -1435351236582736843L;
165 public void modifyText(ModifyEvent event
) {
166 String name
= usernameTxt
.getText();
167 if (name
.trim().equals("")) {
168 dNameTxt
.setText("");
169 lastNameTxt
.setText("");
170 primaryMailTxt
.setText("");
174 dNameTxt
.setText(getDn(name
));
175 lastNameTxt
.setText(name
.toUpperCase());
176 primaryMailTxt
.setText(name
+ "@example.com");
177 pwd1Txt
.setText("demo");
178 pwd2Txt
.setText("demo");
183 primaryMailTxt
= EclipseUiUtils
.createGridLT(composite
,
185 firstNameTxt
= EclipseUiUtils
.createGridLT(composite
,
187 lastNameTxt
= EclipseUiUtils
.createGridLT(composite
,
189 pwd1Txt
= EclipseUiUtils
.createGridLP(composite
, "Password",
191 pwd2Txt
= EclipseUiUtils
.createGridLP(composite
,
192 "Repeat password", this);
193 setControl(composite
);
195 // Initialize buttons
196 setPageComplete(false);
197 getContainer().updateButtons();
201 public void modifyText(ModifyEvent event
) {
202 String message
= checkComplete();
203 if (message
!= null) {
204 setMessage(message
, WizardPage
.ERROR
);
205 setPageComplete(false);
207 setMessage("Complete", WizardPage
.INFORMATION
);
208 setPageComplete(true);
210 getContainer().updateButtons();
213 /** @return error message or null if complete */
214 protected String
checkComplete() {
215 String name
= usernameTxt
.getText();
217 if (name
.trim().equals(""))
218 return "User name must not be empty";
219 Role role
= userAdmin
.getRole(getDn(name
));
221 return "User " + name
+ " already exists";
222 if (!primaryMailTxt
.getText().matches(
223 UserAdminService
.EMAIL_PATTERN
))
224 return "Not a valid email address";
225 if (lastNameTxt
.getText().trim().equals(""))
226 return "Specify a last name";
227 if (pwd1Txt
.getText().trim().equals(""))
228 return "Specify a password";
229 if (pwd2Txt
.getText().trim().equals(""))
230 return "Repeat the password";
231 if (!pwd2Txt
.getText().equals(pwd1Txt
.getText()))
232 return "Passwords are different";
237 public void setVisible(boolean visible
) {
238 super.setVisible(visible
);
240 usernameTxt
.setFocus();
243 public String
getUsername() {
244 return usernameTxt
.getText();
247 public char[] getPassword() {
248 return pwd1Txt
.getTextChars();
254 /* DEPENDENCY INJECTION */
255 public void setUserAdmin(UserAdmin userAdmin
) {
256 this.userAdmin
= userAdmin
;