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
;
19 import java
.util
.List
;
21 import javax
.naming
.InvalidNameException
;
22 import javax
.naming
.ldap
.LdapName
;
23 import javax
.naming
.ldap
.Rdn
;
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
;
53 /** Open a wizard that enables creation of a new user. */
54 public class NewUser
extends AbstractHandler
{
56 * Email addresses must match this regexp pattern ({@value #EMAIL_PATTERN}.
58 * "http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/"
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";
65 /* DEPENDENCY INJECTION */
66 private UserAdminWrapper userAdminWrapper
;
68 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
69 NewUserWizard newUserWizard
= new NewUserWizard();
70 WizardDialog dialog
= new WizardDialog(
71 HandlerUtil
.getActiveShell(event
), newUserWizard
);
76 private class NewUserWizard
extends Wizard
{
79 private MainUserInfoWizardPage mainUserInfo
;
82 private Text dNameTxt
, usernameTxt
, firstNameTxt
, lastNameTxt
,
83 primaryMailTxt
, pwd1Txt
, pwd2Txt
;
84 private Combo baseDnCmb
;
86 public NewUserWizard() {
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
);
98 @SuppressWarnings({ "rawtypes", "unchecked" })
100 public boolean performFinish() {
103 String username
= mainUserInfo
.getUsername();
105 userAdminWrapper
.beginTransactionIfNeeded();
106 User user
= (User
) userAdminWrapper
.getUserAdmin().createRole(
107 getDn(username
), Role
.USER
);
109 Dictionary props
= user
.getProperties();
111 String lastNameStr
= lastNameTxt
.getText();
112 if (UiAdminUtils
.notNull(lastNameStr
))
113 props
.put(LdifName
.dn
.name(), lastNameStr
);
115 String firstNameStr
= firstNameTxt
.getText();
116 if (UiAdminUtils
.notNull(firstNameStr
))
117 props
.put(LdifName
.givenname
.name(), firstNameStr
);
119 String cn
= UiAdminUtils
120 .getDefaultCn(firstNameStr
, lastNameStr
);
121 if (UiAdminUtils
.notNull(cn
))
122 props
.put(LdifName
.cn
.name(), cn
);
124 String mailStr
= primaryMailTxt
.getText();
125 if (UiAdminUtils
.notNull(mailStr
))
126 props
.put(LdifName
.mail
.name(), mailStr
);
128 char[] password
= mainUserInfo
.getPassword();
129 user
.getCredentials().put(null, password
);
131 userAdminWrapper
.notifyListeners(new UserAdminEvent(null,
132 UserAdminEvent
.ROLE_CREATED
, user
));
134 } catch (Exception e
) {
135 ErrorFeedback
.show("Cannot create new user " + username
, e
);
140 private class MainUserInfoWizardPage
extends WizardPage
implements
141 ModifyListener
, ArgeoNames
{
142 private static final long serialVersionUID
= -3150193365151601807L;
144 public MainUserInfoWizardPage() {
146 setTitle("Required Information");
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);
157 baseDnCmb
= createGridLC(composite
, "Base DN", this);
158 initialiseDnCmb(baseDnCmb
);
159 baseDnCmb
.addModifyListener(new ModifyListener() {
160 private static final long serialVersionUID
= -1435351236582736843L;
163 public void modifyText(ModifyEvent event
) {
164 String name
= usernameTxt
.getText();
165 dNameTxt
.setText(getDn(name
));
169 usernameTxt
= EclipseUiUtils
.createGridLT(composite
,
171 usernameTxt
.addModifyListener(new ModifyListener() {
172 private static final long serialVersionUID
= -1435351236582736843L;
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("");
184 dNameTxt
.setText(getDn(name
));
185 lastNameTxt
.setText(name
.toUpperCase());
186 primaryMailTxt
.setText(getMail(name
));
187 pwd1Txt
.setText("demo");
188 pwd2Txt
.setText("demo");
193 primaryMailTxt
= EclipseUiUtils
.createGridLT(composite
,
195 firstNameTxt
= EclipseUiUtils
.createGridLT(composite
,
197 lastNameTxt
= EclipseUiUtils
.createGridLT(composite
,
199 pwd1Txt
= EclipseUiUtils
.createGridLP(composite
, "Password",
201 pwd2Txt
= EclipseUiUtils
.createGridLP(composite
,
202 "Repeat password", this);
203 setControl(composite
);
205 // Initialize buttons
206 setPageComplete(false);
207 getContainer().updateButtons();
211 public void modifyText(ModifyEvent event
) {
212 String message
= checkComplete();
213 if (message
!= null) {
214 setMessage(message
, WizardPage
.ERROR
);
215 setPageComplete(false);
217 setMessage("Complete", WizardPage
.INFORMATION
);
218 setPageComplete(true);
220 getContainer().updateButtons();
223 /** @return error message or null if complete */
224 protected String
checkComplete() {
225 String name
= usernameTxt
.getText();
227 if (name
.trim().equals(""))
228 return "User name must not be empty";
229 Role role
= userAdminWrapper
.getUserAdmin()
230 .getRole(getDn(name
));
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";
247 public void setVisible(boolean visible
) {
248 super.setVisible(visible
);
250 usernameTxt
.setFocus();
253 public String
getUsername() {
254 return usernameTxt
.getText();
257 public char[] getPassword() {
258 return pwd1Txt
.getTextChars();
263 private String
getDn(String uid
) {
264 return "uid=" + uid
+ ",ou=users," + baseDnCmb
.getText();
267 private void initialiseDnCmb(Combo combo
) {
268 List
<String
> dns
= userAdminWrapper
.getKnownBaseDns(true);
270 throw new ArgeoException(
271 "No writable base dn found. Cannot create user");
272 combo
.setItems(dns
.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() + '.'
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
);
293 private Combo
createGridLC(Composite parent
, String label
,
294 ModifyListener modifyListener
) {
295 Label lbl
= new Label(parent
, SWT
.LEAD
);
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
);
305 /* DEPENDENCY INJECTION */
306 public void setUserAdminWrapper(UserAdminWrapper userAdminWrapper
) {
307 this.userAdminWrapper
= userAdminWrapper
;