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 javax
.jcr
.Node
;
19 import javax
.jcr
.Property
;
20 import javax
.jcr
.RepositoryException
;
21 import javax
.jcr
.Session
;
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
;
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
;
54 private MainUserInfoWizardPage mainUserInfo
;
56 public UserCreationWizard(Session session
, UserAdminService userAdminService
,
57 JcrSecurityModel jcrSecurityModel
) {
58 this.session
= session
;
59 this.userAdminService
= userAdminService
;
60 this.jcrSecurityModel
= jcrSecurityModel
;
64 public void addPages() {
65 mainUserInfo
= new MainUserInfoWizardPage(userAdminService
);
66 addPage(mainUserInfo
);
70 public boolean performFinish() {
74 String username
= mainUserInfo
.getUsername();
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();
82 JcrUserDetails jcrUserDetails
= new JcrUserDetails(userProfile
,
83 password
, new GrantedAuthority
[0]);
85 session
.getWorkspace().getVersionManager()
86 .checkin(userProfile
.getPath());
87 userAdminService
.createUser(jcrUserDetails
);
89 } catch (Exception e
) {
90 JcrUtils
.discardQuietly(session
);
91 Node userHome
= UserJcrUtils
.getUserHome(session
, username
);
92 if (userHome
!= null) {
96 } catch (RepositoryException e1
) {
97 JcrUtils
.discardQuietly(session
);
98 log
.warn("Error when trying to clean up failed new user "
102 MessageDialog
.openError(getShell(), "Eroor",
103 "Cannot create new user " + username
);
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
,
114 private UserAdminService userAdminService
;
116 public MainUserInfoWizardPage(UserAdminService userAdminService
) {
118 this.userAdminService
= userAdminService
;
119 setTitle("Required Information");
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",
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
);
139 // Initialize buttons
140 setPageComplete(false);
141 getContainer().updateButtons();
145 public void modifyText(ModifyEvent event
) {
146 String message
= checkComplete();
147 if (message
!= null) {
148 setMessage(message
, WizardPage
.ERROR
);
149 setPageComplete(false);
151 setMessage("Complete", WizardPage
.INFORMATION
);
152 setPageComplete(true);
154 getContainer().updateButtons();
157 /** @return error message or null if complete */
158 protected String
checkComplete() {
160 // (!username.getText().matches(UserAdminService.USERNAME_PATTERN))
162 // "Wrong user name format, should be lower case, between 3 and 64 characters with only '_' an '@' as acceptable special character.";
164 if (username
.getText().trim().equals(""))
165 return "User name must not be empty";
168 UserDetails userDetails
= userAdminService
169 .loadUserByUsername(username
.getText());
170 return "User " + userDetails
.getUsername() + " already exists";
171 } catch (UsernameNotFoundException e
) {
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";
189 public String
getUsername() {
190 return username
.getText();
193 public String
getPassword() {
194 return password1
.getText();
197 public void mapToProfileNode(Node up
) {
199 up
.setProperty(ARGEO_PRIMARY_EMAIL
, primaryEmail
.getText());
200 up
.setProperty(ARGEO_FIRST_NAME
, firstName
.getText());
201 up
.setProperty(ARGEO_LAST_NAME
, lastName
.getText());
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
);