]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/editors/DefaultUserMainPage.java
Plug password management
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / editors / DefaultUserMainPage.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.editors;
17
18 import java.util.Arrays;
19
20 import javax.jcr.Node;
21 import javax.jcr.Property;
22 import javax.jcr.RepositoryException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.argeo.ArgeoException;
27 import org.argeo.jcr.ArgeoNames;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Text;
36 import org.eclipse.ui.forms.AbstractFormPart;
37 import org.eclipse.ui.forms.IManagedForm;
38 import org.eclipse.ui.forms.SectionPart;
39 import org.eclipse.ui.forms.editor.FormEditor;
40 import org.eclipse.ui.forms.editor.FormPage;
41 import org.eclipse.ui.forms.widgets.FormToolkit;
42 import org.eclipse.ui.forms.widgets.ScrolledForm;
43 import org.eclipse.ui.forms.widgets.Section;
44
45 /** Display/edit the properties common to all Argeo users */
46 public class DefaultUserMainPage extends FormPage implements ArgeoNames {
47 final static String ID = "argeoUserEditor.mainPage";
48
49 private final static Log log = LogFactory.getLog(DefaultUserMainPage.class);
50 private Node userProfile;
51
52 private char[] newPassword;
53
54 public DefaultUserMainPage(FormEditor editor, Node userProfile) {
55 super(editor, ID, "Main");
56 this.userProfile = userProfile;
57 }
58
59 protected void createFormContent(final IManagedForm mf) {
60 try {
61 ScrolledForm form = mf.getForm();
62 refreshFormTitle(form);
63 GridLayout mainLayout = new GridLayout(1, true);
64 form.getBody().setLayout(mainLayout);
65
66 createGeneralPart(form.getBody());
67 createPassworPart(form.getBody());
68 } catch (RepositoryException e) {
69 throw new ArgeoException("Cannot create form content", e);
70 }
71 }
72
73 /** Creates the general section */
74 protected void createGeneralPart(Composite parent)
75 throws RepositoryException {
76 FormToolkit tk = getManagedForm().getToolkit();
77 Section section = tk.createSection(parent, Section.TITLE_BAR);
78 section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
79 section.setText("General");
80 Composite body = tk.createComposite(section, SWT.WRAP);
81 section.setClient(body);
82 GridLayout layout = new GridLayout(2, false);
83 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
84 body.setLayout(layout);
85
86 final Text commonName = createLT(body, "Displayed Name",
87 getProperty(Property.JCR_TITLE));
88 final Text firstName = createLT(body, "First name",
89 getProperty(ARGEO_FIRST_NAME));
90 final Text lastName = createLT(body, "Last name",
91 getProperty(ARGEO_LAST_NAME));
92 final Text email = createLT(body, "Email",
93 getProperty(ARGEO_PRIMARY_EMAIL));
94 final Text description = createLMT(body, "Description",
95 getProperty(Property.JCR_DESCRIPTION));
96
97 // create form part (controller)
98 AbstractFormPart part = new SectionPart(section) {
99 public void commit(boolean onSave) {
100 try {
101 userProfile.getSession().getWorkspace().getVersionManager()
102 .checkout(userProfile.getPath());
103 userProfile.setProperty(Property.JCR_TITLE,
104 commonName.getText());
105 userProfile.setProperty(ARGEO_FIRST_NAME,
106 firstName.getText());
107 userProfile
108 .setProperty(ARGEO_LAST_NAME, lastName.getText());
109 userProfile.setProperty(ARGEO_PRIMARY_EMAIL,
110 email.getText());
111 userProfile.setProperty(Property.JCR_DESCRIPTION,
112 description.getText());
113 userProfile.getSession().save();
114 userProfile.getSession().getWorkspace().getVersionManager()
115 .checkin(userProfile.getPath());
116 super.commit(onSave);
117 refreshFormTitle(getManagedForm().getForm());
118 if (log.isTraceEnabled())
119 log.trace("General part committed");
120 } catch (RepositoryException e) {
121 throw new ArgeoException("Cannot commit", e);
122 }
123 }
124 };
125 // if (username != null)
126 // username.addModifyListener(new FormPartML(part));
127 commonName.addModifyListener(new FormPartML(part));
128 firstName.addModifyListener(new FormPartML(part));
129 lastName.addModifyListener(new FormPartML(part));
130 email.addModifyListener(new FormPartML(part));
131 description.addModifyListener(new FormPartML(part));
132 getManagedForm().addPart(part);
133 }
134
135 private void refreshFormTitle(ScrolledForm form) throws RepositoryException {
136 form.setText(getProperty(Property.JCR_TITLE)
137 + (userProfile.getProperty(ARGEO_ENABLED).getBoolean() ? ""
138 : " [DISABLED]"));
139 }
140
141 /** @return the property, or the empty string if not set */
142 protected String getProperty(String name) throws RepositoryException {
143 return userProfile.hasProperty(name) ? userProfile.getProperty(name)
144 .getString() : "";
145 }
146
147 /** Creates the password section */
148 protected void createPassworPart(Composite parent) {
149 FormToolkit tk = getManagedForm().getToolkit();
150 Section section = tk.createSection(parent, Section.TITLE_BAR);
151 section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
152 section.setText("Password");
153
154 Composite body = tk.createComposite(section, SWT.WRAP);
155 section.setClient(body);
156 GridLayout layout = new GridLayout(2, false);
157 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
158 body.setLayout(layout);
159
160 // add widgets (view)
161 final Text password1 = createLP(body, "New password", "");
162 final Text password2 = createLP(body, "Repeat password", "");
163 // create form part (controller)
164 AbstractFormPart part = new SectionPart(section) {
165
166 public void commit(boolean onSave) {
167 if (!password1.getText().equals("")
168 || !password2.getText().equals("")) {
169 if (password1.getText().equals(password2.getText())) {
170 newPassword = password1.getText().toCharArray();
171 password1.setText("");
172 password2.setText("");
173 super.commit(onSave);
174 } else {
175 password1.setText("");
176 password2.setText("");
177 throw new ArgeoException("Passwords are not equals");
178 }
179 }
180 }
181
182 };
183 password1.addModifyListener(new FormPartML(part));
184 password2.addModifyListener(new FormPartML(part));
185 getManagedForm().addPart(part);
186 }
187
188 /** Creates label and text. */
189 protected Text createLT(Composite body, String label, String value) {
190 FormToolkit toolkit = getManagedForm().getToolkit();
191 Label lbl = toolkit.createLabel(body, label);
192 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
193 Text text = toolkit.createText(body, value, SWT.BORDER);
194 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
195 return text;
196 }
197
198 /** Creates label and multiline text. */
199 protected Text createLMT(Composite body, String label, String value) {
200 FormToolkit toolkit = getManagedForm().getToolkit();
201 Label lbl = toolkit.createLabel(body, label);
202 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
203 Text text = toolkit.createText(body, value, SWT.BORDER | SWT.MULTI);
204 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
205 return text;
206 }
207
208 /** Creates label and password. */
209 protected Text createLP(Composite body, String label, String value) {
210 FormToolkit toolkit = getManagedForm().getToolkit();
211 Label lbl = toolkit.createLabel(body, label);
212 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
213 Text text = toolkit.createText(body, value, SWT.BORDER | SWT.PASSWORD);
214 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
215 return text;
216 }
217
218 private class FormPartML implements ModifyListener {
219 private static final long serialVersionUID = 6299808129505381333L;
220 private AbstractFormPart formPart;
221
222 public FormPartML(AbstractFormPart generalPart) {
223 this.formPart = generalPart;
224 }
225
226 public void modifyText(ModifyEvent e) {
227 formPart.markDirty();
228 }
229
230 }
231
232 public String getNewPassword() {
233 if (newPassword != null)
234 return new String(newPassword);
235 else
236 return null;
237 }
238
239 public void resetNewPassword() {
240 if (newPassword != null)
241 Arrays.fill(newPassword, 'x');
242 newPassword = null;
243 }
244 }