1 package org
.argeo
.cms
.users
;
3 import java
.util
.ArrayList
;
8 import javax
.jcr
.Property
;
9 import javax
.jcr
.RepositoryException
;
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
;
34 /** Display a single user main info once it has been created. */
35 public class UserPart
extends StyledControl
implements EditablePart
, NodePart
,
37 private static final long serialVersionUID
= -2883661960366940505L;
38 // private final static Log log = LogFactory.getLog(UserPart.class);
40 // A static list of supported properties.
41 private List
<Text
> texts
;
42 private final static String KEY_PROP_NAME
= "jcr:propertyName";
44 // the 2 password fields
45 private Text pwd1
, pwd2
;
47 private UserAdminService userAdminService
;
49 // private UserDetailsManager userDetailsManager;
51 // TODO implement to provide user creation ability for anonymous user?
52 // public UserPart(Composite parent, int swtStyle) {
53 // super(parent, swtStyle);
56 public UserPart(Composite parent
, int style
, Item item
)
57 throws RepositoryException
{
58 this(parent
, style
, item
, true);
61 public UserPart(Composite parent
, int style
, Item item
,
62 boolean cacheImmediately
) throws RepositoryException
{
63 super(parent
, style
, item
, cacheImmediately
);
67 public Item
getItem() throws RepositoryException
{
71 // Experimental, remove
72 // public void setMouseListener(MouseListener mouseListener) {
73 // super.setMouseListener(mouseListener);
75 // for (Text txt : texts)
76 // txt.addMouseListener(mouseListener);
81 protected Control
createControl(Composite box
, String style
) {
82 Composite body
= new Composite(box
, SWT
.NO_FOCUS
);
83 body
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
84 GridLayout layout
= new GridLayout(2, false);
85 // GridLayout layout = CmsUtils.noSpaceGridLayout(new GridLayout(2,
88 body
.setLayout(layout
);
89 CmsUtils
.style(body
, UserStyles
.USER_FORM_TEXT
);
92 Label header
= new Label(body
, SWT
.NONE
);
93 header
.setText(" Main user information");
94 header
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 2,
96 CmsUtils
.style(header
, UserStyles
.USER_FORM_TITLE
);
99 createTexts(body
, UserStyles
.USER_FORM_TEXT
);
102 for (Text txt
: texts
)
103 txt
.addFocusListener(this);
105 // Change password link
107 header
= new Label(body
, SWT
.NONE
);
108 header
.setText(" Reset password");
109 header
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 2,
111 CmsUtils
.style(header
, UserStyles
.USER_FORM_TITLE
);
113 pwd1
= createLP(body
, UserStyles
.USER_FORM_TEXT
, "Enter password");
114 pwd2
= createLP(body
, UserStyles
.USER_FORM_TEXT
, "Re-Enter");
116 final Link link
= new Link(body
, SWT
.NONE
);
117 link
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, true, false, 2, 1));
118 link
.setText("<a>Change password</a>");
119 link
.addSelectionListener(new SelectionAdapter() {
120 private static final long serialVersionUID
= 8348668888548451776L;
123 public void widgetSelected(SelectionEvent e
) {
125 if ("".equals(pwd1
.getText().trim()))
126 msg
= "Passwords cannot be blank";
127 else if (!pwd1
.getText().equals(pwd2
.getText()))
128 msg
= "Passwords do not match, please try again.";
131 MessageDialog
.openError(link
.getShell(), "Error", msg
);
134 String username
= getNode().getProperty(
135 ArgeoNames
.ARGEO_USER_ID
).getString();
136 if (userAdminService
.userExists(username
)) {
137 JcrUserDetails userDetails
= (JcrUserDetails
) userAdminService
138 .loadUserByUsername(username
);
139 userDetails
= userDetails
.cloneWithNewPassword(pwd1
141 userAdminService
.updateUser(userDetails
);
142 MessageDialog
.openInformation(link
.getShell(),
143 "Password changed", "Password changed.");
145 } catch (Exception re
) {
146 throw new ArgeoException(
147 "unable to reset password for user "
160 private void createTexts(Composite parent
, String style
) {
161 texts
= new ArrayList
<Text
>();
162 texts
.add(createLT(parent
, style
, "Displayed Name", Property
.JCR_TITLE
));
163 texts
.add(createLT(parent
, style
, "First name",
164 ArgeoNames
.ARGEO_FIRST_NAME
));
165 texts
.add(createLT(parent
, style
, "Last name",
166 ArgeoNames
.ARGEO_LAST_NAME
));
167 texts
.add(createLT(parent
, style
, "Email",
168 ArgeoNames
.ARGEO_PRIMARY_EMAIL
));
169 texts
.add(createLMT(parent
, style
, "Description",
170 Property
.JCR_DESCRIPTION
));
173 // protected Composite createEditLayout(Composite parent, String style) {
174 // Composite body = new Composite(parent, SWT.NO_FOCUS);
175 // GridLayout layout = new GridLayout(2, false);
176 // body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
177 // body.setLayout(layout);
179 // createTexts(body, UserStyles.USER_FORM_TEXT);
185 for (Text txt
: texts
) {
186 txt
.setText(get(getNode(), (String
) txt
.getData(KEY_PROP_NAME
)));
187 txt
.setEditable(isEditing());
188 // txt.setEnabled(isEditing());
192 // his.listener methods
194 public void focusGained(FocusEvent e
) {
199 public void focusLost(FocusEvent e
) {
200 // Save change if needed
201 Text text
= (Text
) e
.getSource();
202 set(getNode(), (String
) text
.getData(KEY_PROP_NAME
), text
.getText());
206 /** Creates label and text. */
207 protected Text
createLT(Composite body
, String style
, String label
,
209 Label lbl
= new Label(body
, SWT
.NONE
);
211 lbl
.setFont(EclipseUiUtils
.getBoldFont(body
));
212 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
213 Text text
= new Text(body
, SWT
.BORDER
);
214 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
215 CmsUtils
.style(text
, style
);
216 text
.setData(KEY_PROP_NAME
, propName
);
221 /** Creates label and password text. */
222 protected Text
createLP(Composite body
, String style
, String label
) {
223 Label lbl
= new Label(body
, SWT
.NONE
);
225 lbl
.setFont(EclipseUiUtils
.getBoldFont(body
));
226 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
227 Text text
= new Text(body
, SWT
.BORDER
| SWT
.PASSWORD
);
228 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
229 CmsUtils
.style(text
, style
);
233 /** Creates label and multiline text. */
234 protected Text
createLMT(Composite body
, String style
, String label
,
236 Label lbl
= new Label(body
, SWT
.NONE
);
238 lbl
.setFont(EclipseUiUtils
.getBoldFont(body
));
239 GridData gd
= new GridData(SWT
.RIGHT
, SWT
.TOP
, false, false);
240 gd
.verticalIndent
= 0;
241 lbl
.setLayoutData(gd
);
242 Text text
= new Text(body
, SWT
.BORDER
| SWT
.MULTI
| SWT
.WRAP
);
243 gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, true);
245 text
.setLayoutData(gd
);
246 CmsUtils
.style(text
, style
);
247 text
.setData(KEY_PROP_NAME
, propName
);
252 * Concisely get the string value of a property. Returns an empty String
253 * rather than null if this node doesn't have this property or if the
254 * corresponding property is an empty string.
256 private String
get(Node node
, String propertyName
) {
258 if (!node
.hasProperty(propertyName
))
261 return node
.getProperty(propertyName
).getString();
262 } catch (RepositoryException e
) {
263 throw new ArgeoException("Cannot get property " + propertyName
268 private boolean set(Node node
, String propName
, String value
) {
271 && (!node
.hasProperty(propName
) || node
272 .hasProperty(propName
)
273 && "".equals(node
.getProperty(propName
).getString())))
275 else if (node
.hasProperty(propName
)
276 && node
.getProperty(propName
).getString()
277 .equals((String
) value
))
280 node
.setProperty(propName
, (String
) value
);
281 node
.getSession().save();
284 } catch (RepositoryException e
) {
285 throw new ArgeoException("Cannot property " + propName
+ " on "
286 + node
+ " with value " + value
, e
);
290 public void setUserAdminService(UserAdminService userAdminService
) {
291 this.userAdminService
= userAdminService
;