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
.cms
.e4
.users
.handlers
;
18 import java
.util
.Dictionary
;
19 import java
.util
.List
;
22 import javax
.inject
.Inject
;
23 import javax
.naming
.InvalidNameException
;
24 import javax
.naming
.ldap
.LdapName
;
25 import javax
.naming
.ldap
.Rdn
;
27 import org
.argeo
.cms
.ArgeoNames
;
28 import org
.argeo
.cms
.CmsException
;
29 import org
.argeo
.cms
.e4
.users
.UiAdminUtils
;
30 import org
.argeo
.cms
.e4
.users
.UserAdminWrapper
;
31 import org
.argeo
.cms
.util
.UserAdminUtils
;
32 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
33 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
34 import org
.argeo
.naming
.LdapAttrs
;
35 import org
.argeo
.osgi
.useradmin
.UserAdminConf
;
36 import org
.eclipse
.e4
.core
.di
.annotations
.Execute
;
37 import org
.eclipse
.jface
.wizard
.Wizard
;
38 import org
.eclipse
.jface
.wizard
.WizardDialog
;
39 import org
.eclipse
.jface
.wizard
.WizardPage
;
40 import org
.eclipse
.swt
.SWT
;
41 import org
.eclipse
.swt
.events
.ModifyEvent
;
42 import org
.eclipse
.swt
.events
.ModifyListener
;
43 import org
.eclipse
.swt
.layout
.GridData
;
44 import org
.eclipse
.swt
.layout
.GridLayout
;
45 import org
.eclipse
.swt
.widgets
.Combo
;
46 import org
.eclipse
.swt
.widgets
.Composite
;
47 import org
.eclipse
.swt
.widgets
.Display
;
48 import org
.eclipse
.swt
.widgets
.Label
;
49 import org
.eclipse
.swt
.widgets
.Text
;
50 import org
.osgi
.service
.useradmin
.Role
;
51 import org
.osgi
.service
.useradmin
.User
;
52 import org
.osgi
.service
.useradmin
.UserAdminEvent
;
54 /** Open a wizard that enables creation of a new user. */
55 public class NewUser
{
56 // private final static Log log = LogFactory.getLog(NewUser.class);
57 // public final static String ID = WorkbenchUiPlugin.PLUGIN_ID + ".newUser";
59 /* DEPENDENCY INJECTION */
61 private UserAdminWrapper userAdminWrapper
;
64 public Object
execute() {
65 NewUserWizard newUserWizard
= new NewUserWizard();
66 newUserWizard
.setWindowTitle("User creation");
67 WizardDialog dialog
= new WizardDialog(Display
.getCurrent().getActiveShell(), newUserWizard
);
72 private class NewUserWizard
extends Wizard
{
75 private MainUserInfoWizardPage mainUserInfo
;
78 private Text dNameTxt
, usernameTxt
, firstNameTxt
, lastNameTxt
, primaryMailTxt
, pwd1Txt
, pwd2Txt
;
79 private Combo baseDnCmb
;
81 public NewUserWizard() {
86 public void addPages() {
87 mainUserInfo
= new MainUserInfoWizardPage();
88 addPage(mainUserInfo
);
89 String message
= "Default wizard that also eases user creation tests:\n "
90 + "Mail and last name are automatically "
91 + "generated form the uid. Password are defauted to 'demo'.";
92 mainUserInfo
.setMessage(message
, WizardPage
.WARNING
);
95 @SuppressWarnings({ "rawtypes", "unchecked" })
97 public boolean performFinish() {
100 String username
= mainUserInfo
.getUsername();
101 userAdminWrapper
.beginTransactionIfNeeded();
103 User user
= (User
) userAdminWrapper
.getUserAdmin().createRole(getDn(username
), Role
.USER
);
105 Dictionary props
= user
.getProperties();
107 String lastNameStr
= lastNameTxt
.getText();
108 if (EclipseUiUtils
.notEmpty(lastNameStr
))
109 props
.put(LdapAttrs
.sn
.name(), lastNameStr
);
111 String firstNameStr
= firstNameTxt
.getText();
112 if (EclipseUiUtils
.notEmpty(firstNameStr
))
113 props
.put(LdapAttrs
.givenName
.name(), firstNameStr
);
115 String cn
= UserAdminUtils
.buildDefaultCn(firstNameStr
, lastNameStr
);
116 if (EclipseUiUtils
.notEmpty(cn
))
117 props
.put(LdapAttrs
.cn
.name(), cn
);
119 String mailStr
= primaryMailTxt
.getText();
120 if (EclipseUiUtils
.notEmpty(mailStr
))
121 props
.put(LdapAttrs
.mail
.name(), mailStr
);
123 char[] password
= mainUserInfo
.getPassword();
124 user
.getCredentials().put(null, password
);
125 userAdminWrapper
.commitOrNotifyTransactionStateChange();
126 userAdminWrapper
.notifyListeners(new UserAdminEvent(null, UserAdminEvent
.ROLE_CREATED
, user
));
128 } catch (Exception e
) {
129 ErrorFeedback
.show("Cannot create new user " + username
, e
);
134 private class MainUserInfoWizardPage
extends WizardPage
implements ModifyListener
, ArgeoNames
{
135 private static final long serialVersionUID
= -3150193365151601807L;
137 public MainUserInfoWizardPage() {
139 setTitle("Required Information");
143 public void createControl(Composite parent
) {
144 Composite composite
= new Composite(parent
, SWT
.NONE
);
145 composite
.setLayout(new GridLayout(2, false));
146 dNameTxt
= EclipseUiUtils
.createGridLT(composite
, "Distinguished name", this);
147 dNameTxt
.setEnabled(false);
149 baseDnCmb
= createGridLC(composite
, "Base DN");
150 initialiseDnCmb(baseDnCmb
);
151 baseDnCmb
.addModifyListener(this);
152 baseDnCmb
.addModifyListener(new ModifyListener() {
153 private static final long serialVersionUID
= -1435351236582736843L;
156 public void modifyText(ModifyEvent event
) {
157 String name
= usernameTxt
.getText();
158 dNameTxt
.setText(getDn(name
));
162 usernameTxt
= EclipseUiUtils
.createGridLT(composite
, "Local ID", this);
163 usernameTxt
.addModifyListener(new ModifyListener() {
164 private static final long serialVersionUID
= -1435351236582736843L;
167 public void modifyText(ModifyEvent event
) {
168 String name
= usernameTxt
.getText();
169 if (name
.trim().equals("")) {
170 dNameTxt
.setText("");
171 lastNameTxt
.setText("");
172 primaryMailTxt
.setText("");
176 dNameTxt
.setText(getDn(name
));
177 lastNameTxt
.setText(name
.toUpperCase());
178 primaryMailTxt
.setText(getMail(name
));
179 pwd1Txt
.setText("demo");
180 pwd2Txt
.setText("demo");
185 primaryMailTxt
= EclipseUiUtils
.createGridLT(composite
, "Email", this);
186 firstNameTxt
= EclipseUiUtils
.createGridLT(composite
, "First name", this);
187 lastNameTxt
= EclipseUiUtils
.createGridLT(composite
, "Last name", this);
188 pwd1Txt
= EclipseUiUtils
.createGridLP(composite
, "Password", this);
189 pwd2Txt
= EclipseUiUtils
.createGridLP(composite
, "Repeat password", this);
190 setControl(composite
);
192 // Initialize buttons
193 setPageComplete(false);
194 getContainer().updateButtons();
198 public void modifyText(ModifyEvent event
) {
199 String message
= checkComplete();
200 if (message
!= null) {
201 setMessage(message
, WizardPage
.ERROR
);
202 setPageComplete(false);
204 setMessage("Complete", WizardPage
.INFORMATION
);
205 setPageComplete(true);
207 getContainer().updateButtons();
210 /** @return error message or null if complete */
211 protected String
checkComplete() {
212 String name
= usernameTxt
.getText();
214 if (name
.trim().equals(""))
215 return "User name must not be empty";
216 Role role
= userAdminWrapper
.getUserAdmin().getRole(getDn(name
));
218 return "User " + name
+ " already exists";
219 if (!primaryMailTxt
.getText().matches(UiAdminUtils
.EMAIL_PATTERN
))
220 return "Not a valid email address";
221 if (lastNameTxt
.getText().trim().equals(""))
222 return "Specify a last name";
223 if (pwd1Txt
.getText().trim().equals(""))
224 return "Specify a password";
225 if (pwd2Txt
.getText().trim().equals(""))
226 return "Repeat the password";
227 if (!pwd2Txt
.getText().equals(pwd1Txt
.getText()))
228 return "Passwords are different";
233 public void setVisible(boolean visible
) {
234 super.setVisible(visible
);
236 if (baseDnCmb
.getSelectionIndex() == -1)
237 baseDnCmb
.setFocus();
239 usernameTxt
.setFocus();
242 public String
getUsername() {
243 return usernameTxt
.getText();
246 public char[] getPassword() {
247 return pwd1Txt
.getTextChars();
252 private Map
<String
, String
> getDns() {
253 return userAdminWrapper
.getKnownBaseDns(true);
256 private String
getDn(String uid
) {
257 Map
<String
, String
> dns
= getDns();
258 String bdn
= baseDnCmb
.getText();
259 if (EclipseUiUtils
.notEmpty(bdn
)) {
260 Dictionary
<String
, ?
> props
= UserAdminConf
.uriAsProperties(dns
.get(bdn
));
261 String dn
= LdapAttrs
.uid
.name() + "=" + uid
+ "," + UserAdminConf
.userBase
.getValue(props
) + "," + bdn
;
267 private void initialiseDnCmb(Combo combo
) {
268 Map
<String
, String
> dns
= userAdminWrapper
.getKnownBaseDns(true);
270 throw new CmsException("No writable base dn found. Cannot create user");
271 combo
.setItems(dns
.keySet().toArray(new String
[0]));
276 private String
getMail(String username
) {
277 if (baseDnCmb
.getSelectionIndex() == -1)
279 String baseDn
= baseDnCmb
.getText();
281 LdapName name
= new LdapName(baseDn
);
282 List
<Rdn
> rdns
= name
.getRdns();
283 return username
+ "@" + (String
) rdns
.get(1).getValue() + '.' + (String
) rdns
.get(0).getValue();
284 } catch (InvalidNameException e
) {
285 throw new CmsException("Unable to generate mail for " + username
+ " with base dn " + baseDn
, e
);
290 private Combo
createGridLC(Composite parent
, String label
) {
291 Label lbl
= new Label(parent
, SWT
.LEAD
);
293 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
294 Combo combo
= new Combo(parent
, SWT
.LEAD
| SWT
.BORDER
| SWT
.READ_ONLY
);
295 combo
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
299 /* DEPENDENCY INJECTION */
300 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper
) {
301 this.userAdminWrapper
= userAdminWrapper
;