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