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
.eclipse
.ui
.dialogs
;
18 import java
.util
.ArrayList
;
20 import javax
.jcr
.Node
;
21 import javax
.jcr
.Property
;
22 import javax
.jcr
.RepositoryException
;
23 import javax
.jcr
.Session
;
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
;
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
;
56 private MainUserInfoWizardPage mainUserInfo
;
58 public UserCreationWizard(Session session
,
59 UserAdminService userAdminService
, JcrSecurityModel jcrSecurityModel
) {
60 this.session
= session
;
61 this.userAdminService
= userAdminService
;
62 this.jcrSecurityModel
= jcrSecurityModel
;
66 public void addPages() {
67 mainUserInfo
= new MainUserInfoWizardPage(userAdminService
);
68 addPage(mainUserInfo
);
72 public boolean performFinish() {
76 String username
= mainUserInfo
.getUsername();
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();
84 JcrUserDetails jcrUserDetails
= new JcrUserDetails(userProfile
,
85 password
, new ArrayList
<GrantedAuthority
>());
87 session
.getWorkspace().getVersionManager()
88 .checkin(userProfile
.getPath());
89 userAdminService
.createUser(jcrUserDetails
);
91 } catch (Exception e
) {
92 JcrUtils
.discardQuietly(session
);
93 Node userHome
= UserJcrUtils
.getUserHome(session
, username
);
94 if (userHome
!= null) {
98 } catch (RepositoryException e1
) {
99 JcrUtils
.discardQuietly(session
);
100 log
.warn("Error when trying to clean up failed new user "
104 // FIXME re-get ErrorFeedback dialog after single sourcing
106 MessageDialog
.openError(getShell(), "Error",
107 "Cannot create new user " + username
);
108 log
.error("Cannot create new user " + username
);
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
,
120 private UserAdminService userAdminService
;
122 public MainUserInfoWizardPage(UserAdminService userAdminService
) {
124 this.userAdminService
= userAdminService
;
125 setTitle("Required Information");
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",
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
);
145 // Initialize buttons
146 setPageComplete(false);
147 getContainer().updateButtons();
151 public void modifyText(ModifyEvent event
) {
152 String message
= checkComplete();
153 if (message
!= null) {
154 setMessage(message
, WizardPage
.ERROR
);
155 setPageComplete(false);
157 setMessage("Complete", WizardPage
.INFORMATION
);
158 setPageComplete(true);
160 getContainer().updateButtons();
163 /** @return error message or null if complete */
164 protected String
checkComplete() {
166 // (!username.getText().matches(UserAdminService.USERNAME_PATTERN))
168 // "Wrong user name format, should be lower case, between 3 and 64 characters with only '_' an '@' as acceptable special character.";
170 if (username
.getText().trim().equals(""))
171 return "User name must not be empty";
174 UserDetails userDetails
= userAdminService
175 .loadUserByUsername(username
.getText());
176 return "User " + userDetails
.getUsername() + " already exists";
177 } catch (UsernameNotFoundException e
) {
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";
195 public String
getUsername() {
196 return username
.getText();
199 public String
getPassword() {
200 return password1
.getText();
203 public void mapToProfileNode(Node up
) {
205 up
.setProperty(ARGEO_PRIMARY_EMAIL
, primaryEmail
.getText());
206 up
.setProperty(ARGEO_FIRST_NAME
, firstName
.getText());
207 up
.setProperty(ARGEO_LAST_NAME
, lastName
.getText());
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
);