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
.cms
.users
;
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
.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
;
48 /** Wizard to create a new user */
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
;
57 private MainUserInfoWizardPage mainUserInfo
;
59 public UserCreationWizard(Session session
,
60 UserAdminService userAdminService
, JcrSecurityModel jcrSecurityModel
) {
61 this.session
= session
;
62 this.userAdminService
= userAdminService
;
63 this.jcrSecurityModel
= jcrSecurityModel
;
67 public void addPages() {
68 mainUserInfo
= new MainUserInfoWizardPage(userAdminService
);
69 addPage(mainUserInfo
);
73 public boolean performFinish() {
77 String username
= mainUserInfo
.getUsername();
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();
85 JcrUserDetails jcrUserDetails
= new JcrUserDetails(userProfile
,
86 password
, new ArrayList
<GrantedAuthority
>());
88 session
.getWorkspace().getVersionManager()
89 .checkin(userProfile
.getPath());
90 userAdminService
.createUser(jcrUserDetails
);
92 } catch (Exception e
) {
93 JcrUtils
.discardQuietly(session
);
94 Node userHome
= UserJcrUtils
.getUserHome(session
, username
);
95 if (userHome
!= null) {
99 } catch (RepositoryException e1
) {
100 JcrUtils
.discardQuietly(session
);
101 log
.warn("Error when trying to clean up failed new user "
105 // FIXME re-get ErrorFeedback dialog after single sourcing
107 MessageDialog
.openError(getShell(), "Error",
108 "Cannot create new user " + username
);
109 log
.error("Cannot create new user " + username
);
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
,
121 private UserAdminService userAdminService
;
123 public MainUserInfoWizardPage(UserAdminService userAdminService
) {
125 this.userAdminService
= userAdminService
;
126 setTitle("Required Information");
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",
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
);
146 // Initialize buttons
147 setPageComplete(false);
148 getContainer().updateButtons();
152 public void modifyText(ModifyEvent event
) {
153 String message
= checkComplete();
154 if (message
!= null) {
155 setMessage(message
, WizardPage
.ERROR
);
156 setPageComplete(false);
158 setMessage("Complete", WizardPage
.INFORMATION
);
159 setPageComplete(true);
161 getContainer().updateButtons();
164 /** @return error message or null if complete */
165 protected String
checkComplete() {
167 // (!username.getText().matches(UserAdminService.USERNAME_PATTERN))
169 // "Wrong user name format, should be lower case, between 3 and 64 characters with only '_' an '@' as acceptable special character.";
171 if (username
.getText().trim().equals(""))
172 return "User name must not be empty";
175 UserDetails userDetails
= userAdminService
176 .loadUserByUsername(username
.getText());
177 return "User " + userDetails
.getUsername() + " already exists";
178 } catch (UsernameNotFoundException e
) {
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";
196 public String
getUsername() {
197 return username
.getText();
200 public String
getPassword() {
201 return password1
.getText();
204 public void mapToProfileNode(Node up
) {
206 up
.setProperty(ARGEO_PRIMARY_EMAIL
, primaryEmail
.getText());
207 up
.setProperty(ARGEO_FIRST_NAME
, firstName
.getText());
208 up
.setProperty(ARGEO_LAST_NAME
, lastName
.getText());
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
);