]> git.argeo.org Git - lgpl/argeo-commons.git/blob - NewUser.java
9d4151caf15826c100b126644ee5df961b138a11
[lgpl/argeo-commons.git] / NewUser.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.security.ui.admin.internal.commands;
17
18 import java.util.Dictionary;
19 import java.util.List;
20
21 import javax.naming.InvalidNameException;
22 import javax.naming.ldap.LdapName;
23 import javax.naming.ldap.Rdn;
24
25 import org.argeo.ArgeoException;
26 import org.argeo.eclipse.ui.EclipseUiUtils;
27 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
28 import org.argeo.jcr.ArgeoNames;
29 import org.argeo.osgi.useradmin.LdifName;
30 import org.argeo.security.ui.admin.SecurityAdminPlugin;
31 import org.argeo.security.ui.admin.internal.UiAdminUtils;
32 import org.argeo.security.ui.admin.internal.UserAdminWrapper;
33 import org.eclipse.core.commands.AbstractHandler;
34 import org.eclipse.core.commands.ExecutionEvent;
35 import org.eclipse.core.commands.ExecutionException;
36 import org.eclipse.jface.wizard.Wizard;
37 import org.eclipse.jface.wizard.WizardDialog;
38 import org.eclipse.jface.wizard.WizardPage;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.events.ModifyEvent;
41 import org.eclipse.swt.events.ModifyListener;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Combo;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Label;
47 import org.eclipse.swt.widgets.Text;
48 import org.eclipse.ui.handlers.HandlerUtil;
49 import org.osgi.service.useradmin.Role;
50 import org.osgi.service.useradmin.User;
51 import org.osgi.service.useradmin.UserAdminEvent;
52
53 /** Open a wizard that enables creation of a new user. */
54 public class NewUser extends AbstractHandler {
55 /**
56 * Email addresses must match this regexp pattern ({@value #EMAIL_PATTERN}.
57 * Thanks to <a href=
58 * "http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/"
59 * >this tip</a>.
60 */
61 public final static String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
62 // private final static Log log = LogFactory.getLog(NewUser.class);
63 public final static String ID = SecurityAdminPlugin.PLUGIN_ID + ".newUser";
64
65 /* DEPENDENCY INJECTION */
66 private UserAdminWrapper userAdminWrapper;
67
68 public Object execute(ExecutionEvent event) throws ExecutionException {
69 NewUserWizard newUserWizard = new NewUserWizard();
70 WizardDialog dialog = new WizardDialog(
71 HandlerUtil.getActiveShell(event), newUserWizard);
72 dialog.open();
73 return null;
74 }
75
76 private class NewUserWizard extends Wizard {
77
78 // pages
79 private MainUserInfoWizardPage mainUserInfo;
80
81 // End user fields
82 private Text dNameTxt, usernameTxt, firstNameTxt, lastNameTxt,
83 primaryMailTxt, pwd1Txt, pwd2Txt;
84 private Combo baseDnCmb;
85
86 public NewUserWizard() {
87 }
88
89 @Override
90 public void addPages() {
91 mainUserInfo = new MainUserInfoWizardPage();
92 addPage(mainUserInfo);
93 String message = "Dummy wizard to ease user creation tests:\n Mail and last name are automatically "
94 + "generated form the uid. Password are defauted to 'demo'.";
95 mainUserInfo.setMessage(message, WizardPage.WARNING);
96 }
97
98 @SuppressWarnings({ "rawtypes", "unchecked" })
99 @Override
100 public boolean performFinish() {
101 if (!canFinish())
102 return false;
103 String username = mainUserInfo.getUsername();
104 try {
105 userAdminWrapper.beginTransactionIfNeeded();
106 User user = (User) userAdminWrapper.getUserAdmin().createRole(
107 getDn(username), Role.USER);
108
109 Dictionary props = user.getProperties();
110
111 String lastNameStr = lastNameTxt.getText();
112 if (UiAdminUtils.notNull(lastNameStr))
113 props.put(LdifName.dn.name(), lastNameStr);
114
115 String firstNameStr = firstNameTxt.getText();
116 if (UiAdminUtils.notNull(firstNameStr))
117 props.put(LdifName.givenname.name(), firstNameStr);
118
119 String cn = UiAdminUtils
120 .getDefaultCn(firstNameStr, lastNameStr);
121 if (UiAdminUtils.notNull(cn))
122 props.put(LdifName.cn.name(), cn);
123
124 String mailStr = primaryMailTxt.getText();
125 if (UiAdminUtils.notNull(mailStr))
126 props.put(LdifName.mail.name(), mailStr);
127
128 char[] password = mainUserInfo.getPassword();
129 user.getCredentials().put(null, password);
130
131 userAdminWrapper.notifyListeners(new UserAdminEvent(null,
132 UserAdminEvent.ROLE_CREATED, user));
133 return true;
134 } catch (Exception e) {
135 ErrorFeedback.show("Cannot create new user " + username, e);
136 return false;
137 }
138 }
139
140 private class MainUserInfoWizardPage extends WizardPage implements
141 ModifyListener, ArgeoNames {
142 private static final long serialVersionUID = -3150193365151601807L;
143
144 public MainUserInfoWizardPage() {
145 super("Main");
146 setTitle("Required Information");
147 }
148
149 @Override
150 public void createControl(Composite parent) {
151 Composite composite = new Composite(parent, SWT.NONE);
152 composite.setLayout(new GridLayout(2, false));
153 dNameTxt = EclipseUiUtils.createGridLT(composite,
154 "Distinguished name", this);
155 dNameTxt.setEnabled(false);
156
157 baseDnCmb = createGridLC(composite, "Base DN", this);
158 initialiseDnCmb(baseDnCmb);
159 baseDnCmb.addModifyListener(new ModifyListener() {
160 private static final long serialVersionUID = -1435351236582736843L;
161
162 @Override
163 public void modifyText(ModifyEvent event) {
164 String name = usernameTxt.getText();
165 dNameTxt.setText(getDn(name));
166 }
167 });
168
169 usernameTxt = EclipseUiUtils.createGridLT(composite,
170 "Local ID", this);
171 usernameTxt.addModifyListener(new ModifyListener() {
172 private static final long serialVersionUID = -1435351236582736843L;
173
174 @Override
175 public void modifyText(ModifyEvent event) {
176 String name = usernameTxt.getText();
177 if (name.trim().equals("")) {
178 dNameTxt.setText("");
179 lastNameTxt.setText("");
180 primaryMailTxt.setText("");
181 pwd1Txt.setText("");
182 pwd2Txt.setText("");
183 } else {
184 dNameTxt.setText(getDn(name));
185 lastNameTxt.setText(name.toUpperCase());
186 primaryMailTxt.setText(getMail(name));
187 pwd1Txt.setText("demo");
188 pwd2Txt.setText("demo");
189 }
190 }
191 });
192
193 primaryMailTxt = EclipseUiUtils.createGridLT(composite,
194 "Email", this);
195 firstNameTxt = EclipseUiUtils.createGridLT(composite,
196 "First name", this);
197 lastNameTxt = EclipseUiUtils.createGridLT(composite,
198 "Last name", this);
199 pwd1Txt = EclipseUiUtils.createGridLP(composite, "Password",
200 this);
201 pwd2Txt = EclipseUiUtils.createGridLP(composite,
202 "Repeat password", this);
203 setControl(composite);
204
205 // Initialize buttons
206 setPageComplete(false);
207 getContainer().updateButtons();
208 }
209
210 @Override
211 public void modifyText(ModifyEvent event) {
212 String message = checkComplete();
213 if (message != null) {
214 setMessage(message, WizardPage.ERROR);
215 setPageComplete(false);
216 } else {
217 setMessage("Complete", WizardPage.INFORMATION);
218 setPageComplete(true);
219 }
220 getContainer().updateButtons();
221 }
222
223 /** @return error message or null if complete */
224 protected String checkComplete() {
225 String name = usernameTxt.getText();
226
227 if (name.trim().equals(""))
228 return "User name must not be empty";
229 Role role = userAdminWrapper.getUserAdmin()
230 .getRole(getDn(name));
231 if (role != null)
232 return "User " + name + " already exists";
233 if (!primaryMailTxt.getText().matches(EMAIL_PATTERN))
234 return "Not a valid email address";
235 if (lastNameTxt.getText().trim().equals(""))
236 return "Specify a last name";
237 if (pwd1Txt.getText().trim().equals(""))
238 return "Specify a password";
239 if (pwd2Txt.getText().trim().equals(""))
240 return "Repeat the password";
241 if (!pwd2Txt.getText().equals(pwd1Txt.getText()))
242 return "Passwords are different";
243 return null;
244 }
245
246 @Override
247 public void setVisible(boolean visible) {
248 super.setVisible(visible);
249 if (visible)
250 usernameTxt.setFocus();
251 }
252
253 public String getUsername() {
254 return usernameTxt.getText();
255 }
256
257 public char[] getPassword() {
258 return pwd1Txt.getTextChars();
259 }
260
261 }
262
263 private String getDn(String uid) {
264 return "uid=" + uid + ",ou=users," + baseDnCmb.getText();
265 }
266
267 private void initialiseDnCmb(Combo combo) {
268 List<String> dns = userAdminWrapper.getKnownBaseDns(true);
269 if (dns.isEmpty())
270 throw new ArgeoException(
271 "No writable base dn found. Cannot create user");
272 combo.setItems(dns.toArray(new String[0]));
273 // combo.select(0);
274 }
275
276 private String getMail(String username) {
277 if (baseDnCmb.getSelectionIndex() == -1)
278 return null;
279 String baseDn = baseDnCmb.getText();
280 try {
281 LdapName name = new LdapName(baseDn);
282 List<Rdn> rdns = name.getRdns();
283 return username + "@" + (String) rdns.get(1).getValue() + '.'
284 + (String) rdns.get(0).getValue();
285 } catch (InvalidNameException e) {
286 throw new ArgeoException("Unable to generate mail for "
287 + username + " with base dn " + baseDn, e);
288 }
289 }
290
291 }
292
293 private Combo createGridLC(Composite parent, String label,
294 ModifyListener modifyListener) {
295 Label lbl = new Label(parent, SWT.LEAD);
296 lbl.setText(label);
297 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
298 Combo combo = new Combo(parent, SWT.LEAD | SWT.BORDER | SWT.READ_ONLY);
299 combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
300 if (modifyListener != null)
301 combo.addModifyListener(modifyListener);
302 return combo;
303 }
304
305 /* DEPENDENCY INJECTION */
306 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper) {
307 this.userAdminWrapper = userAdminWrapper;
308 }
309 }