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 javax
.jcr
.Node
;
19 import javax
.jcr
.Property
;
20 import javax
.jcr
.RepositoryException
;
22 import org
.argeo
.ArgeoException
;
23 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
24 import org
.argeo
.jcr
.ArgeoNames
;
25 import org
.argeo
.security
.UserAdminService
;
26 import org
.eclipse
.jface
.wizard
.WizardPage
;
27 import org
.eclipse
.swt
.SWT
;
28 import org
.eclipse
.swt
.events
.ModifyEvent
;
29 import org
.eclipse
.swt
.events
.ModifyListener
;
30 import org
.eclipse
.swt
.layout
.GridLayout
;
31 import org
.eclipse
.swt
.widgets
.Composite
;
32 import org
.eclipse
.swt
.widgets
.Text
;
33 import org
.springframework
.security
.core
.userdetails
.UserDetails
;
34 import org
.springframework
.security
.core
.userdetails
.UsernameNotFoundException
;
36 public class MainUserInfoWizardPage
extends WizardPage
implements
37 ModifyListener
, ArgeoNames
{
38 private static final long serialVersionUID
= -3367329974808698649L;
39 private Text username
, firstName
, lastName
, primaryEmail
, password1
,
41 private UserAdminService userAdminService
;
43 public MainUserInfoWizardPage(UserAdminService userAdminService
) {
45 this.userAdminService
= userAdminService
;
46 setTitle("Required Information");
50 public void createControl(Composite parent
) {
51 Composite composite
= new Composite(parent
, SWT
.NONE
);
52 composite
.setLayout(new GridLayout(2, false));
53 username
= EclipseUiUtils
.createGridLT(composite
, "Username", this);
54 primaryEmail
= EclipseUiUtils
.createGridLT(composite
, "Email", this);
55 firstName
= EclipseUiUtils
.createGridLT(composite
, "First name", this);
56 lastName
= EclipseUiUtils
.createGridLT(composite
, "Last name", this);
57 password1
= EclipseUiUtils
.createGridLP(composite
, "Password", this);
58 password2
= EclipseUiUtils
.createGridLP(composite
, "Repeat password",
60 setControl(composite
);
63 setPageComplete(false);
64 getContainer().updateButtons();
68 public void modifyText(ModifyEvent event
) {
69 String message
= checkComplete();
70 if (message
!= null) {
71 setMessage(message
, WizardPage
.ERROR
);
72 setPageComplete(false);
74 setMessage("Complete", WizardPage
.INFORMATION
);
75 setPageComplete(true);
77 getContainer().updateButtons();
80 /** @return error message or null if complete */
81 protected String
checkComplete() {
82 // if (!username.getText().matches(UserAdminService.USERNAME_PATTERN))
84 // "Wrong user name format, should be lower case, between 3 and 64 characters with only '_' an '@' as acceptable special character.";
86 if (username
.getText().trim().equals(""))
87 return "User name must not be empty";
90 UserDetails userDetails
= userAdminService
91 .loadUserByUsername(username
.getText());
92 return "User " + userDetails
.getUsername() + " already exists";
93 } catch (UsernameNotFoundException e
) {
96 if (!primaryEmail
.getText().matches(UserAdminService
.EMAIL_PATTERN
))
97 return "Not a valid email address";
98 if (firstName
.getText().trim().equals(""))
99 return "Specify a first name";
100 if (lastName
.getText().trim().equals(""))
101 return "Specify a last name";
102 if (password1
.getText().trim().equals(""))
103 return "Specify a password";
104 if (password2
.getText().trim().equals(""))
105 return "Repeat the password";
106 if (!password2
.getText().equals(password1
.getText()))
107 return "Passwords are different";
111 public String
getUsername() {
112 return username
.getText();
115 public char[] getPassword() {
116 return password1
.getTextChars();
119 public void mapToProfileNode(Node up
) {
121 up
.setProperty(ARGEO_PRIMARY_EMAIL
, primaryEmail
.getText());
122 up
.setProperty(ARGEO_FIRST_NAME
, firstName
.getText());
123 up
.setProperty(ARGEO_LAST_NAME
, lastName
.getText());
126 // TODO add wizard pages to do it
127 up
.setProperty(Property
.JCR_TITLE
, firstName
.getText() + " "
128 + lastName
.getText());
129 up
.setProperty(Property
.JCR_DESCRIPTION
, "");
130 } catch (RepositoryException e
) {
131 throw new ArgeoException("Cannot map to " + up
, e
);