]> git.argeo.org Git - lgpl/argeo-commons.git/blob - users/UserPart.java
Prepare next development cycle
[lgpl/argeo-commons.git] / users / UserPart.java
1 package org.argeo.cms.users;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.jcr.Item;
7 import javax.jcr.Node;
8 import javax.jcr.Property;
9 import javax.jcr.RepositoryException;
10
11 import org.argeo.ArgeoException;
12 import org.argeo.cms.util.CmsUtils;
13 import org.argeo.cms.viewers.EditablePart;
14 import org.argeo.cms.viewers.NodePart;
15 import org.argeo.cms.widgets.StyledControl;
16 import org.argeo.eclipse.ui.EclipseUiUtils;
17 import org.argeo.jcr.ArgeoNames;
18 import org.argeo.security.UserAdminService;
19 import org.argeo.security.jcr.JcrUserDetails;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.FocusEvent;
23 import org.eclipse.swt.events.FocusListener;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Link;
32 import org.eclipse.swt.widgets.Text;
33
34 /** Display a single user main info once it has been created. */
35 public class UserPart extends StyledControl implements EditablePart, NodePart,
36 FocusListener {
37 private static final long serialVersionUID = -2883661960366940505L;
38 // private final static Log log = LogFactory.getLog(UserPart.class);
39
40 // A static list of supported properties.
41 private List<Text> texts;
42 private final static String KEY_PROP_NAME = "jcr:propertyName";
43
44 // the 2 password fields
45 private Text pwd1, pwd2;
46
47 private UserAdminService userAdminService;
48
49 // TODO implement to provide user creation ability for anonymous user?
50 // public UserPart(Composite parent, int swtStyle) {
51 // super(parent, swtStyle);
52 // }
53
54 public UserPart(Composite parent, int style, Item item)
55 throws RepositoryException {
56 this(parent, style, item, true);
57 }
58
59 public UserPart(Composite parent, int style, Item item,
60 boolean cacheImmediately) throws RepositoryException {
61 super(parent, style, item, cacheImmediately);
62 }
63
64 @Override
65 public Item getItem() throws RepositoryException {
66 return getNode();
67 }
68
69 @Override
70 protected Control createControl(Composite box, String style) {
71 Composite body = new Composite(box, SWT.NO_FOCUS);
72 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
73 CmsUtils.style(body, UserStyles.USER_FORM_TEXT);
74
75 body.setLayout(new GridLayout(2, false));
76
77 // Header
78 Label headerLbl = new Label(body, SWT.NONE);
79 headerLbl.setText(" Main user information");
80 headerLbl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
81 2, 1));
82 CmsUtils.style(headerLbl, UserStyles.USER_FORM_TITLE);
83
84 // Form field
85 createTexts(body, UserStyles.USER_FORM_TEXT);
86
87 if (isEditing())
88 for (Text txt : texts)
89 txt.addFocusListener(this);
90
91 // Change password link
92 headerLbl = new Label(body, SWT.NONE);
93 headerLbl.setText(" Reset password");
94 headerLbl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
95 2, 1));
96 CmsUtils.style(headerLbl, UserStyles.USER_FORM_TITLE);
97
98 pwd1 = createLP(body, UserStyles.USER_FORM_TEXT, "Enter password");
99 pwd2 = createLP(body, UserStyles.USER_FORM_TEXT, "Re-Enter");
100
101 final Link link = new Link(body, SWT.NONE);
102 link.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 2,
103 1));
104 link.setText("<a>Change password</a>");
105 link.addSelectionListener(new SelectionAdapter() {
106 private static final long serialVersionUID = 8348668888548451776L;
107
108 @Override
109 public void widgetSelected(SelectionEvent e) {
110 String msg = null;
111 if ("".equals(pwd1.getText().trim()))
112 msg = "Passwords cannot be blank";
113 else if (!pwd1.getText().equals(pwd2.getText()))
114 msg = "Passwords do not match, please try again.";
115
116 if (msg != null) {
117 MessageDialog.openError(link.getShell(), "Error", msg);
118 } else {
119 try {
120 String username = getNode().getProperty(
121 ArgeoNames.ARGEO_USER_ID).getString();
122 if (userAdminService.userExists(username)) {
123 JcrUserDetails userDetails = (JcrUserDetails) userAdminService
124 .loadUserByUsername(username);
125 userDetails = userDetails.cloneWithNewPassword(pwd1
126 .getText());
127 userAdminService.updateUser(userDetails);
128 MessageDialog.openInformation(link.getShell(),
129 "Password changed", "Password changed.");
130 }
131 } catch (Exception re) {
132 throw new ArgeoException(
133 "unable to reset password for user "
134 + getNode(), re);
135 }
136 }
137
138 pwd1.setText("");
139 pwd2.setText("");
140
141 }
142 });
143 return body;
144 }
145
146 private void createTexts(Composite parent, String style) {
147 texts = new ArrayList<Text>();
148 texts.add(createLT(parent, style, "Displayed Name", Property.JCR_TITLE));
149 texts.add(createLT(parent, style, "First name",
150 ArgeoNames.ARGEO_FIRST_NAME));
151 texts.add(createLT(parent, style, "Last name",
152 ArgeoNames.ARGEO_LAST_NAME));
153 texts.add(createLT(parent, style, "Email",
154 ArgeoNames.ARGEO_PRIMARY_EMAIL));
155 texts.add(createLMT(parent, style, "Description",
156 Property.JCR_DESCRIPTION));
157 }
158
159 void refresh() {
160 for (Text txt : texts) {
161 txt.setText(get(getNode(), (String) txt.getData(KEY_PROP_NAME)));
162 txt.setEditable(isEditing());
163 }
164 }
165
166 // his.listener methods
167 @Override
168 public void focusGained(FocusEvent e) {
169 // Do nothing
170 }
171
172 @Override
173 public void focusLost(FocusEvent e) {
174 // Save change if needed
175 Text text = (Text) e.getSource();
176 set(getNode(), (String) text.getData(KEY_PROP_NAME), text.getText());
177 }
178
179 // HELPERS
180 /** Creates label and text. */
181 protected Text createLT(Composite body, String style, String label,
182 String propName) {
183 Label lbl = new Label(body, SWT.NONE);
184 lbl.setText(label);
185 lbl.setFont(EclipseUiUtils.getBoldFont(body));
186 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
187 Text text = new Text(body, SWT.BORDER);
188 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
189 CmsUtils.style(text, style);
190 text.setData(KEY_PROP_NAME, propName);
191 return text;
192 }
193
194 // HELPERS
195 /** Creates label and password text. */
196 protected Text createLP(Composite body, String style, String label) {
197 Label lbl = new Label(body, SWT.NONE);
198 lbl.setText(label);
199 lbl.setFont(EclipseUiUtils.getBoldFont(body));
200 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
201 Text text = new Text(body, SWT.BORDER | SWT.PASSWORD);
202 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
203 CmsUtils.style(text, style);
204 return text;
205 }
206
207 /** Creates label and multiline text. */
208 protected Text createLMT(Composite body, String style, String label,
209 String propName) {
210 Label lbl = new Label(body, SWT.NONE);
211 lbl.setText(label);
212 lbl.setFont(EclipseUiUtils.getBoldFont(body));
213 GridData gd = new GridData(SWT.RIGHT, SWT.TOP, false, false);
214 gd.verticalIndent = 0;
215 lbl.setLayoutData(gd);
216 Text text = new Text(body, SWT.BORDER | SWT.MULTI | SWT.WRAP);
217 gd = new GridData(SWT.FILL, SWT.CENTER, true, true);
218 gd.heightHint = 100;
219 text.setLayoutData(gd);
220 CmsUtils.style(text, style);
221 text.setData(KEY_PROP_NAME, propName);
222 return text;
223 }
224
225 /**
226 * Concisely get the string value of a property. Returns an empty String
227 * rather than null if this node doesn't have this property or if the
228 * corresponding property is an empty string.
229 */
230 private String get(Node node, String propertyName) {
231 try {
232 if (!node.hasProperty(propertyName))
233 return "";
234 else
235 return node.getProperty(propertyName).getString();
236 } catch (RepositoryException e) {
237 throw new ArgeoException("Cannot get property " + propertyName
238 + " of " + node, e);
239 }
240 }
241
242 private boolean set(Node node, String propName, String value) {
243 try {
244 if ("".equals(value)
245 && (!node.hasProperty(propName) || node
246 .hasProperty(propName)
247 && "".equals(node.getProperty(propName).getString())))
248 return false;
249 else if (node.hasProperty(propName)
250 && node.getProperty(propName).getString()
251 .equals((String) value))
252 return false;
253 else {
254 node.setProperty(propName, (String) value);
255 node.getSession().save();
256 return true;
257 }
258 } catch (RepositoryException e) {
259 throw new ArgeoException("Cannot property " + propName + " on "
260 + node + " with value " + value, e);
261 }
262 }
263
264 public void setUserAdminService(UserAdminService userAdminService) {
265 this.userAdminService = userAdminService;
266 }
267 }