]> git.argeo.org Git - lgpl/argeo-commons.git/blob - dialogs/UserCreationWizard.java
Prepare next development cycle
[lgpl/argeo-commons.git] / dialogs / UserCreationWizard.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.eclipse.ui.dialogs;
17
18 import java.util.ArrayList;
19
20 import javax.jcr.Node;
21 import javax.jcr.Property;
22 import javax.jcr.RepositoryException;
23 import javax.jcr.Session;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.ArgeoException;
28 import org.argeo.eclipse.ui.EclipseUiUtils;
29 import org.argeo.jcr.ArgeoNames;
30 import org.argeo.jcr.JcrUtils;
31 import org.argeo.jcr.UserJcrUtils;
32 import org.argeo.security.UserAdminService;
33 import org.argeo.security.jcr.JcrSecurityModel;
34 import org.argeo.security.jcr.JcrUserDetails;
35 import org.eclipse.jface.dialogs.MessageDialog;
36 import org.eclipse.jface.wizard.Wizard;
37 import org.eclipse.jface.wizard.WizardPage;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.events.ModifyEvent;
40 import org.eclipse.swt.events.ModifyListener;
41 import org.eclipse.swt.layout.GridLayout;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Text;
44 import org.springframework.security.core.GrantedAuthority;
45 import org.springframework.security.core.userdetails.UserDetails;
46 import org.springframework.security.core.userdetails.UsernameNotFoundException;
47
48 /** Wizard to create a new user */
49 public class UserCreationWizard extends Wizard {
50 private final static Log log = LogFactory.getLog(UserCreationWizard.class);
51 private Session session;
52 private UserAdminService userAdminService;
53 private JcrSecurityModel jcrSecurityModel;
54
55 // pages
56 private MainUserInfoWizardPage mainUserInfo;
57
58 public UserCreationWizard(Session session,
59 UserAdminService userAdminService, JcrSecurityModel jcrSecurityModel) {
60 this.session = session;
61 this.userAdminService = userAdminService;
62 this.jcrSecurityModel = jcrSecurityModel;
63 }
64
65 @Override
66 public void addPages() {
67 mainUserInfo = new MainUserInfoWizardPage(userAdminService);
68 addPage(mainUserInfo);
69 }
70
71 @Override
72 public boolean performFinish() {
73 if (!canFinish())
74 return false;
75
76 String username = mainUserInfo.getUsername();
77 try {
78 Node userProfile = jcrSecurityModel.sync(session, username, null);
79 session.getWorkspace().getVersionManager()
80 .checkout(userProfile.getPath());
81 mainUserInfo.mapToProfileNode(userProfile);
82 String password = mainUserInfo.getPassword();
83 // TODO add roles
84 JcrUserDetails jcrUserDetails = new JcrUserDetails(userProfile,
85 password, new ArrayList<GrantedAuthority>());
86 session.save();
87 session.getWorkspace().getVersionManager()
88 .checkin(userProfile.getPath());
89 userAdminService.createUser(jcrUserDetails);
90 return true;
91 } catch (Exception e) {
92 JcrUtils.discardQuietly(session);
93 Node userHome = UserJcrUtils.getUserHome(session, username);
94 if (userHome != null) {
95 try {
96 userHome.remove();
97 session.save();
98 } catch (RepositoryException e1) {
99 JcrUtils.discardQuietly(session);
100 log.warn("Error when trying to clean up failed new user "
101 + username, e1);
102 }
103 }
104 // FIXME re-get ErrorFeedback dialog after single sourcing
105 // refactoring
106 MessageDialog.openError(getShell(), "Error",
107 "Cannot create new user " + username);
108 log.error("Cannot create new user " + username);
109 e.printStackTrace();
110 return false;
111 }
112 }
113
114 /** First page, collect all main info and check their validity */
115 protected class MainUserInfoWizardPage extends WizardPage implements
116 ModifyListener, ArgeoNames {
117 private static final long serialVersionUID = -3367329974808698649L;
118 private Text username, firstName, lastName, primaryEmail, password1,
119 password2;
120 private UserAdminService userAdminService;
121
122 public MainUserInfoWizardPage(UserAdminService userAdminService) {
123 super("Main");
124 this.userAdminService = userAdminService;
125 setTitle("Required Information");
126 }
127
128 @Override
129 public void createControl(Composite parent) {
130 Composite composite = new Composite(parent, SWT.NONE);
131 composite.setLayout(new GridLayout(2, false));
132 username = EclipseUiUtils.createGridLT(composite, "Username", this);
133 primaryEmail = EclipseUiUtils
134 .createGridLT(composite, "Email", this);
135 firstName = EclipseUiUtils.createGridLT(composite, "First name",
136 this);
137 lastName = EclipseUiUtils
138 .createGridLT(composite, "Last name", this);
139 password1 = EclipseUiUtils
140 .createGridLP(composite, "Password", this);
141 password2 = EclipseUiUtils.createGridLP(composite,
142 "Repeat password", this);
143 setControl(composite);
144
145 // Initialize buttons
146 setPageComplete(false);
147 getContainer().updateButtons();
148 }
149
150 @Override
151 public void modifyText(ModifyEvent event) {
152 String message = checkComplete();
153 if (message != null) {
154 setMessage(message, WizardPage.ERROR);
155 setPageComplete(false);
156 } else {
157 setMessage("Complete", WizardPage.INFORMATION);
158 setPageComplete(true);
159 }
160 getContainer().updateButtons();
161 }
162
163 /** @return error message or null if complete */
164 protected String checkComplete() {
165 // if
166 // (!username.getText().matches(UserAdminService.USERNAME_PATTERN))
167 // return
168 // "Wrong user name format, should be lower case, between 3 and 64 characters with only '_' an '@' as acceptable special character.";
169
170 if (username.getText().trim().equals(""))
171 return "User name must not be empty";
172
173 try {
174 UserDetails userDetails = userAdminService
175 .loadUserByUsername(username.getText());
176 return "User " + userDetails.getUsername() + " already exists";
177 } catch (UsernameNotFoundException e) {
178 // silent
179 }
180 if (!primaryEmail.getText().matches(UserAdminService.EMAIL_PATTERN))
181 return "Not a valid email address";
182 if (firstName.getText().trim().equals(""))
183 return "Specify a first name";
184 if (lastName.getText().trim().equals(""))
185 return "Specify a last name";
186 if (password1.getText().trim().equals(""))
187 return "Specify a password";
188 if (password2.getText().trim().equals(""))
189 return "Repeat the password";
190 if (!password2.getText().equals(password1.getText()))
191 return "Passwords are different";
192 return null;
193 }
194
195 public String getUsername() {
196 return username.getText();
197 }
198
199 public String getPassword() {
200 return password1.getText();
201 }
202
203 public void mapToProfileNode(Node up) {
204 try {
205 up.setProperty(ARGEO_PRIMARY_EMAIL, primaryEmail.getText());
206 up.setProperty(ARGEO_FIRST_NAME, firstName.getText());
207 up.setProperty(ARGEO_LAST_NAME, lastName.getText());
208
209 // derived values
210 // TODO add wizard pages to do it
211 up.setProperty(Property.JCR_TITLE, firstName.getText() + " "
212 + lastName.getText());
213 up.setProperty(Property.JCR_DESCRIPTION, "");
214 } catch (RepositoryException e) {
215 throw new ArgeoException("Cannot map to " + up, e);
216 }
217 }
218 }
219 }