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 public void setUserAdmin(UserAdmin userAdmin
) {
84 this.userAdmin
= userAdmin
;
87 private class NewUserWizard
extends Wizard
{
90 private MainUserInfoWizardPage mainUserInfo
;
93 private Text dNameTxt
, usernameTxt
, firstNameTxt
, lastNameTxt
,
94 primaryMailTxt
, pwd1Txt
, pwd2Txt
;
96 public NewUserWizard() {
100 public void addPages() {
101 mainUserInfo
= new MainUserInfoWizardPage();
102 addPage(mainUserInfo
);
103 String message
= "Dummy wizard to ease user creation tests: mail, last name are automatically "
104 + "generated form the uid, password are defauted to 'demo'.";
105 mainUserInfo
.setMessage(message
, WizardPage
.WARNING
);
108 @SuppressWarnings({ "rawtypes", "unchecked" })
110 public boolean performFinish() {
113 String username
= mainUserInfo
.getUsername();
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();