]> 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
Update and session management pattern for view and editor to remove old deprecated...
[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 Argeo GmbH
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 // Initialize buttons
62 setPageComplete(false);
63 getContainer().updateButtons();
64 }
65
66 @Override
67 public void modifyText(ModifyEvent event) {
68 String message = checkComplete();
69 if (message != null) {
70 setMessage(message, WizardPage.ERROR);
71 setPageComplete(false);
72 } else {
73 setMessage("Complete", WizardPage.INFORMATION);
74 setPageComplete(true);
75 }
76 getContainer().updateButtons();
77 }
78
79 /** @return error message or null if complete */
80 protected String checkComplete() {
81 // if (!username.getText().matches(UserAdminService.USERNAME_PATTERN))
82 // return
83 // "Wrong user name format, should be lower case, between 3 and 64 characters with only '_' an '@' as acceptable special character.";
84
85 if (username.getText().trim().equals(""))
86 return "User name must not be empty";
87
88 try {
89 UserDetails userDetails = userAdminService
90 .loadUserByUsername(username.getText());
91 return "User " + userDetails.getUsername() + " already exists";
92 } catch (UsernameNotFoundException e) {
93 // silent
94 }
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";
107 return null;
108 }
109
110 public String getUsername() {
111 return username.getText();
112 }
113
114 public String getPassword() {
115 return password1.getText();
116 }
117
118 public void mapToProfileNode(Node up) {
119 try {
120 up.setProperty(ARGEO_PRIMARY_EMAIL, primaryEmail.getText());
121 up.setProperty(ARGEO_FIRST_NAME, firstName.getText());
122 up.setProperty(ARGEO_LAST_NAME, lastName.getText());
123
124 // derived values
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);
131 }
132 }
133 }