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