]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/UserCreationWizard.java
98531d79f14014b899f527772227d7b241c10345
[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, UserAdminService userAdminService,
57 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 MessageDialog.openError(getShell(), "Eroor",
103 "Cannot create new user " + username);
104 return false;
105 }
106 }
107
108 /** First page, collect all main info and check their validity */
109 protected class MainUserInfoWizardPage extends WizardPage implements
110 ModifyListener, ArgeoNames {
111 private static final long serialVersionUID = -3367329974808698649L;
112 private Text username, firstName, lastName, primaryEmail, password1,
113 password2;
114 private UserAdminService userAdminService;
115
116 public MainUserInfoWizardPage(UserAdminService userAdminService) {
117 super("Main");
118 this.userAdminService = userAdminService;
119 setTitle("Required Information");
120 }
121
122 @Override
123 public void createControl(Composite parent) {
124 Composite composite = new Composite(parent, SWT.NONE);
125 composite.setLayout(new GridLayout(2, false));
126 username = EclipseUiUtils.createGridLT(composite, "Username", this);
127 primaryEmail = EclipseUiUtils
128 .createGridLT(composite, "Email", this);
129 firstName = EclipseUiUtils.createGridLT(composite, "First name",
130 this);
131 lastName = EclipseUiUtils
132 .createGridLT(composite, "Last name", this);
133 password1 = EclipseUiUtils
134 .createGridLP(composite, "Password", this);
135 password2 = EclipseUiUtils.createGridLP(composite,
136 "Repeat password", this);
137 setControl(composite);
138
139 // Initialize buttons
140 setPageComplete(false);
141 getContainer().updateButtons();
142 }
143
144 @Override
145 public void modifyText(ModifyEvent event) {
146 String message = checkComplete();
147 if (message != null) {
148 setMessage(message, WizardPage.ERROR);
149 setPageComplete(false);
150 } else {
151 setMessage("Complete", WizardPage.INFORMATION);
152 setPageComplete(true);
153 }
154 getContainer().updateButtons();
155 }
156
157 /** @return error message or null if complete */
158 protected String checkComplete() {
159 // if
160 // (!username.getText().matches(UserAdminService.USERNAME_PATTERN))
161 // return
162 // "Wrong user name format, should be lower case, between 3 and 64 characters with only '_' an '@' as acceptable special character.";
163
164 if (username.getText().trim().equals(""))
165 return "User name must not be empty";
166
167 try {
168 UserDetails userDetails = userAdminService
169 .loadUserByUsername(username.getText());
170 return "User " + userDetails.getUsername() + " already exists";
171 } catch (UsernameNotFoundException e) {
172 // silent
173 }
174 if (!primaryEmail.getText().matches(UserAdminService.EMAIL_PATTERN))
175 return "Not a valid email address";
176 if (firstName.getText().trim().equals(""))
177 return "Specify a first name";
178 if (lastName.getText().trim().equals(""))
179 return "Specify a last name";
180 if (password1.getText().trim().equals(""))
181 return "Specify a password";
182 if (password2.getText().trim().equals(""))
183 return "Repeat the password";
184 if (!password2.getText().equals(password1.getText()))
185 return "Passwords are different";
186 return null;
187 }
188
189 public String getUsername() {
190 return username.getText();
191 }
192
193 public String getPassword() {
194 return password1.getText();
195 }
196
197 public void mapToProfileNode(Node up) {
198 try {
199 up.setProperty(ARGEO_PRIMARY_EMAIL, primaryEmail.getText());
200 up.setProperty(ARGEO_FIRST_NAME, firstName.getText());
201 up.setProperty(ARGEO_LAST_NAME, lastName.getText());
202
203 // derived values
204 // TODO add wizard pages to do it
205 up.setProperty(Property.JCR_TITLE, firstName.getText() + " "
206 + lastName.getText());
207 up.setProperty(Property.JCR_DESCRIPTION, "");
208 } catch (RepositoryException e) {
209 throw new ArgeoException("Cannot map to " + up, e);
210 }
211 }
212 }
213 }