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
.security
.ui
.admin
.wizards
;
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
.userdetails
.UserDetails
;
34 import org
.springframework
.security
.userdetails
.UsernameNotFoundException
;
36 public class MainUserInfoWizardPage
extends WizardPage
implements
37 ModifyListener
, ArgeoNames
{
38 private Text username
, firstName
, lastName
, primaryEmail
, password1
,
40 private UserAdminService userAdminService
;
42 public MainUserInfoWizardPage(UserAdminService userAdminService
) {
44 this.userAdminService
= userAdminService
;
45 setTitle("Required Information");
49 public void createControl(Composite parent
) {
50 Composite composite
= new Composite(parent
, SWT
.NONE
);
51 composite
.setLayout(new GridLayout(2, false));
52 username
= EclipseUiUtils
.createGridLT(composite
, "Username", this);
53 primaryEmail
= EclipseUiUtils
.createGridLT(composite
, "Email", this);
54 firstName
= EclipseUiUtils
.createGridLT(composite
, "First name", this);
55 lastName
= EclipseUiUtils
.createGridLT(composite
, "Last name", this);
56 password1
= EclipseUiUtils
.createGridLP(composite
, "Password", this);
57 password2
= EclipseUiUtils
.createGridLP(composite
, "Repeat password",
59 setControl(composite
);
62 setPageComplete(false);
63 getContainer().updateButtons();
67 public void modifyText(ModifyEvent event
) {
68 String message
= checkComplete();
69 if (message
!= null) {
70 setMessage(message
, WizardPage
.ERROR
);
71 setPageComplete(false);
73 setMessage("Complete", WizardPage
.INFORMATION
);
74 setPageComplete(true);
76 getContainer().updateButtons();
79 /** @return error message or null if complete */
80 protected String
checkComplete() {
81 // if (!username.getText().matches(UserAdminService.USERNAME_PATTERN))
83 // "Wrong user name format, should be lower case, between 3 and 64 characters with only '_' an '@' as acceptable special character.";
85 if (username
.getText().trim().equals(""))
86 return "User name must not be empty";
89 UserDetails userDetails
= userAdminService
90 .loadUserByUsername(username
.getText());
91 return "User " + userDetails
.getUsername() + " already exists";
92 } catch (UsernameNotFoundException e
) {
95 if (!primaryEmail
.getText().matches(UserAdminService
.EMAIL_PATTERN
))
96 return "Not a valid email address";
97 if (firstName
.getText().trim().equals(""))
98 return "Specify a first name";
99 if (lastName
.getText().trim().equals(""))
100 return "Specify a last name";
101 if (password1
.getText().trim().equals(""))
102 return "Specify a password";
103 if (password2
.getText().trim().equals(""))
104 return "Repeat the password";
105 if (!password2
.getText().equals(password1
.getText()))
106 return "Passwords are different";
110 public String
getUsername() {
111 return username
.getText();
114 public String
getPassword() {
115 return password1
.getText();
118 public void mapToProfileNode(Node up
) {
120 up
.setProperty(ARGEO_PRIMARY_EMAIL
, primaryEmail
.getText());
121 up
.setProperty(ARGEO_FIRST_NAME
, firstName
.getText());
122 up
.setProperty(ARGEO_LAST_NAME
, lastName
.getText());
125 // TODO add wizard pages to do it
126 up
.setProperty(Property
.JCR_TITLE
, firstName
.getText() + " "
127 + lastName
.getText());
128 up
.setProperty(Property
.JCR_DESCRIPTION
, "");
129 } catch (RepositoryException e
) {
130 throw new ArgeoException("Cannot map to " + up
, e
);