]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/plugins/org.argeo.security.ui.admin/src/main/java/org/argeo/security/ui/admin/wizards/MainUserInfoWizardPage.java
Refactor JCR utils and home usage
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui.admin / src / main / java / org / argeo / security / ui / admin / wizards / MainUserInfoWizardPage.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.security.ui.admin.wizards;
17
18 import javax.jcr.Node;
19 import javax.jcr.Property;
20 import javax.jcr.RepositoryException;
21
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;
35
36 public class MainUserInfoWizardPage extends WizardPage implements
37 ModifyListener, ArgeoNames {
38 private Text username, firstName, lastName, primaryEmail, password1,
39 password2;
40 private UserAdminService userAdminService;
41
42 public MainUserInfoWizardPage(UserAdminService userAdminService) {
43 super("Main");
44 this.userAdminService = userAdminService;
45 setTitle("Required Information");
46 }
47
48 @Override
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",
58 this);
59 setControl(composite);
60 }
61
62 @Override
63 public void modifyText(ModifyEvent event) {
64 String message = checkComplete();
65 if (message != null)
66 setMessage(message, WizardPage.ERROR);
67 else {
68 setMessage("Complete", WizardPage.INFORMATION);
69 setPageComplete(true);
70 }
71 }
72
73 /** @return error message or null if complete */
74 protected String checkComplete() {
75 // if (!username.getText().matches(UserAdminService.USERNAME_PATTERN))
76 // return "Wrong user name format, should be lower case, between 3 and 64 characters with only '_' an '@' as acceptable special character.";
77 try {
78 UserDetails userDetails = userAdminService
79 .loadUserByUsername(username.getText());
80 return "User " + userDetails.getUsername() + " already exists";
81 } catch (UsernameNotFoundException e) {
82 // silent
83 }
84 if (!primaryEmail.getText().matches(UserAdminService.EMAIL_PATTERN))
85 return "Not a valid email address";
86 if (firstName.getText().trim().equals(""))
87 return "Specify a first name";
88 if (lastName.getText().trim().equals(""))
89 return "Specify a last name";
90 if (password1.getText().trim().equals(""))
91 return "Specify a password";
92 if (password2.getText().trim().equals(""))
93 return "Repeat the password";
94 if (!password2.getText().equals(password1.getText()))
95 return "Passwords are different";
96 return null;
97 }
98
99 public String getUsername() {
100 return username.getText();
101 }
102
103 public String getPassword() {
104 return password1.getText();
105 }
106
107 public void mapToProfileNode(Node up) {
108 try {
109 up.setProperty(ARGEO_PRIMARY_EMAIL, primaryEmail.getText());
110 up.setProperty(ARGEO_FIRST_NAME, firstName.getText());
111 up.setProperty(ARGEO_LAST_NAME, lastName.getText());
112
113 // derived values
114 // TODO add wizard pages to do it
115 up.setProperty(Property.JCR_TITLE, firstName.getText() + " "
116 + lastName.getText());
117 up.setProperty(Property.JCR_DESCRIPTION, "");
118 } catch (RepositoryException e) {
119 throw new ArgeoException("Cannot map to " + up, e);
120 }
121 }
122 }