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
.internal
.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
.internal
.UserAdminWrapper
;
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
.wizard
.Wizard
;
32 import org
.eclipse
.jface
.wizard
.WizardDialog
;
33 import org
.eclipse
.jface
.wizard
.WizardPage
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.events
.ModifyEvent
;
36 import org
.eclipse
.swt
.events
.ModifyListener
;
37 import org
.eclipse
.swt
.layout
.GridLayout
;
38 import org
.eclipse
.swt
.widgets
.Composite
;
39 import org
.eclipse
.swt
.widgets
.Text
;
40 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
41 import org
.osgi
.service
.useradmin
.Role
;
42 import org
.osgi
.service
.useradmin
.User
;
43 import org
.osgi
.service
.useradmin
.UserAdminEvent
;
45 /** Open a wizard that enables creation of a new user. */
46 public class NewUser
extends AbstractHandler
{
47 // private final static Log log = LogFactory.getLog(NewUser.class);
48 public final static String ID
= SecurityAdminPlugin
.PLUGIN_ID
+ ".newUser";
50 /* DEPENDENCY INJECTION */
51 private UserAdminWrapper userAdminWrapper
;
53 // TODO implement a dynamic choice of the base dn
54 private String
getDn(String uid
) {
55 return "uid=" + uid
+ ",ou=users,dc=example,dc=com";
58 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
59 NewUserWizard newUserWizard
= new NewUserWizard();
60 WizardDialog dialog
= new WizardDialog(
61 HandlerUtil
.getActiveShell(event
), newUserWizard
);
65 // // Force refresh until the listener are implemented
66 // if (Window.OK == dialog.open())
67 // forceRefresh(event);
71 // private void forceRefresh(ExecutionEvent event) {
72 // IWorkbenchWindow iww = HandlerUtil.getActiveWorkbenchWindow(event);
75 // IWorkbenchPage activePage = iww.getActivePage();
76 // IWorkbenchPart part = activePage.getActivePart();
77 // if (part instanceof UsersView)
78 // ((UsersView) part).refresh();
81 private class NewUserWizard
extends Wizard
{
84 private MainUserInfoWizardPage mainUserInfo
;
87 private Text dNameTxt
, usernameTxt
, firstNameTxt
, lastNameTxt
,
88 primaryMailTxt
, pwd1Txt
, pwd2Txt
;
90 public NewUserWizard() {
94 public void addPages() {
95 mainUserInfo
= new MainUserInfoWizardPage();
96 addPage(mainUserInfo
);
97 String message
= "Dummy wizard to ease user creation tests:\n Mail and last name are automatically "
98 + "generated form the uid. Password are defauted to 'demo'.";
99 mainUserInfo
.setMessage(message
, WizardPage
.WARNING
);
102 @SuppressWarnings({ "rawtypes", "unchecked" })
104 public boolean performFinish() {
107 String username
= mainUserInfo
.getUsername();
109 userAdminWrapper
.beginTransactionIfNeeded();
110 char[] password
= mainUserInfo
.getPassword();
111 User user
= (User
) userAdminWrapper
.getUserAdmin().createRole(
112 getDn(username
), Role
.USER
);
114 Dictionary props
= user
.getProperties();
116 String lastNameStr
= lastNameTxt
.getText();
117 if (UiAdminUtils
.notNull(lastNameStr
))
118 props
.put(UserAdminConstants
.KEY_LASTNAME
, lastNameStr
);
120 String firstNameStr
= firstNameTxt
.getText();
121 if (UiAdminUtils
.notNull(firstNameStr
))
122 props
.put(UserAdminConstants
.KEY_FIRSTNAME
, firstNameStr
);
124 String cn
= UiAdminUtils
125 .getDefaultCn(firstNameStr
, lastNameStr
);
126 if (UiAdminUtils
.notNull(cn
))
127 props
.put(UserAdminConstants
.KEY_CN
, cn
);
129 String mailStr
= primaryMailTxt
.getText();
130 if (UiAdminUtils
.notNull(mailStr
))
131 props
.put(UserAdminConstants
.KEY_MAIL
, mailStr
);
133 user
.getCredentials().put(null, password
);
135 userAdminWrapper
.notifyListeners(new UserAdminEvent(null,
136 UserAdminEvent
.ROLE_CREATED
, user
));
138 } catch (Exception e
) {
139 ErrorFeedback
.show("Cannot create new user " + username
, e
);
144 private class MainUserInfoWizardPage
extends WizardPage
implements
145 ModifyListener
, ArgeoNames
{
146 private static final long serialVersionUID
= -3150193365151601807L;
148 public MainUserInfoWizardPage() {
150 setTitle("Required Information");
154 public void createControl(Composite parent
) {
155 Composite composite
= new Composite(parent
, SWT
.NONE
);
156 composite
.setLayout(new GridLayout(2, false));
157 dNameTxt
= EclipseUiUtils
.createGridLT(composite
,
158 "Distinguished name", this);
159 dNameTxt
.setEnabled(false);
160 usernameTxt
= EclipseUiUtils
.createGridLT(composite
,
162 usernameTxt
.addModifyListener(new ModifyListener() {
163 private static final long serialVersionUID
= -1435351236582736843L;
166 public void modifyText(ModifyEvent event
) {
167 String name
= usernameTxt
.getText();
168 if (name
.trim().equals("")) {
169 dNameTxt
.setText("");
170 lastNameTxt
.setText("");
171 primaryMailTxt
.setText("");
175 dNameTxt
.setText(getDn(name
));
176 lastNameTxt
.setText(name
.toUpperCase());
177 primaryMailTxt
.setText(name
+ "@example.com");
178 pwd1Txt
.setText("demo");
179 pwd2Txt
.setText("demo");
184 primaryMailTxt
= EclipseUiUtils
.createGridLT(composite
,
186 firstNameTxt
= EclipseUiUtils
.createGridLT(composite
,
188 lastNameTxt
= EclipseUiUtils
.createGridLT(composite
,
190 pwd1Txt
= EclipseUiUtils
.createGridLP(composite
, "Password",
192 pwd2Txt
= EclipseUiUtils
.createGridLP(composite
,
193 "Repeat password", this);
194 setControl(composite
);
196 // Initialize buttons
197 setPageComplete(false);
198 getContainer().updateButtons();
202 public void modifyText(ModifyEvent event
) {
203 String message
= checkComplete();
204 if (message
!= null) {
205 setMessage(message
, WizardPage
.ERROR
);
206 setPageComplete(false);
208 setMessage("Complete", WizardPage
.INFORMATION
);
209 setPageComplete(true);
211 getContainer().updateButtons();
214 /** @return error message or null if complete */
215 protected String
checkComplete() {
216 String name
= usernameTxt
.getText();
218 if (name
.trim().equals(""))
219 return "User name must not be empty";
220 Role role
= userAdminWrapper
.getUserAdmin()
221 .getRole(getDn(name
));
223 return "User " + name
+ " already exists";
224 if (!primaryMailTxt
.getText().matches(
225 UserAdminService
.EMAIL_PATTERN
))
226 return "Not a valid email address";
227 if (lastNameTxt
.getText().trim().equals(""))
228 return "Specify a last name";
229 if (pwd1Txt
.getText().trim().equals(""))
230 return "Specify a password";
231 if (pwd2Txt
.getText().trim().equals(""))
232 return "Repeat the password";
233 if (!pwd2Txt
.getText().equals(pwd1Txt
.getText()))
234 return "Passwords are different";
239 public void setVisible(boolean visible
) {
240 super.setVisible(visible
);
242 usernameTxt
.setFocus();
245 public String
getUsername() {
246 return usernameTxt
.getText();
249 public char[] getPassword() {
250 return pwd1Txt
.getTextChars();
256 /* DEPENDENCY INJECTION */
257 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper
) {
258 this.userAdminWrapper
= userAdminWrapper
;