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 // TODO implement to provide user creation ability for anonymous user?
50 // public UserPart(Composite parent, int swtStyle) {
51 // super(parent, swtStyle);
54 public UserPart(Composite parent
, int style
, Item item
)
55 throws RepositoryException
{
56 this(parent
, style
, item
, true);
59 public UserPart(Composite parent
, int style
, Item item
,
60 boolean cacheImmediately
) throws RepositoryException
{
61 super(parent
, style
, item
, cacheImmediately
);
65 public Item
getItem() throws RepositoryException
{
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
);
75 body
.setLayout(new GridLayout(2, false));
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,
82 CmsUtils
.style(headerLbl
, UserStyles
.USER_FORM_TITLE
);
85 createTexts(body
, UserStyles
.USER_FORM_TEXT
);
88 for (Text txt
: texts
)
89 txt
.addFocusListener(this);
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,
96 CmsUtils
.style(headerLbl
, UserStyles
.USER_FORM_TITLE
);
98 pwd1
= createLP(body
, UserStyles
.USER_FORM_TEXT
, "Enter password");
99 pwd2
= createLP(body
, UserStyles
.USER_FORM_TEXT
, "Re-Enter");
101 final Link link
= new Link(body
, SWT
.NONE
);
102 link
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, true, false, 2,
104 link
.setText("<a>Change password</a>");
105 link
.addSelectionListener(new SelectionAdapter() {
106 private static final long serialVersionUID
= 8348668888548451776L;
109 public void widgetSelected(SelectionEvent e
) {
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.";
117 MessageDialog
.openError(link
.getShell(), "Error", msg
);
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
127 userAdminService
.updateUser(userDetails
);
128 MessageDialog
.openInformation(link
.getShell(),
129 "Password changed", "Password changed.");
131 } catch (Exception re
) {
132 throw new ArgeoException(
133 "unable to reset password for user "
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
));
160 for (Text txt
: texts
) {
161 txt
.setText(get(getNode(), (String
) txt
.getData(KEY_PROP_NAME
)));
162 txt
.setEditable(isEditing());
166 // his.listener methods
168 public void focusGained(FocusEvent e
) {
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());
180 /** Creates label and text. */
181 protected Text
createLT(Composite body
, String style
, String label
,
183 Label lbl
= new Label(body
, SWT
.NONE
);
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
);
195 /** Creates label and password text. */
196 protected Text
createLP(Composite body
, String style
, String label
) {
197 Label lbl
= new Label(body
, SWT
.NONE
);
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
);
207 /** Creates label and multiline text. */
208 protected Text
createLMT(Composite body
, String style
, String label
,
210 Label lbl
= new Label(body
, SWT
.NONE
);
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);
219 text
.setLayoutData(gd
);
220 CmsUtils
.style(text
, style
);
221 text
.setData(KEY_PROP_NAME
, propName
);
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.
230 private String
get(Node node
, String propertyName
) {
232 if (!node
.hasProperty(propertyName
))
235 return node
.getProperty(propertyName
).getString();
236 } catch (RepositoryException e
) {
237 throw new ArgeoException("Cannot get property " + propertyName
242 private boolean set(Node node
, String propName
, String value
) {
245 && (!node
.hasProperty(propName
) || node
246 .hasProperty(propName
)
247 && "".equals(node
.getProperty(propName
).getString())))
249 else if (node
.hasProperty(propName
)
250 && node
.getProperty(propName
).getString()
251 .equals((String
) value
))
254 node
.setProperty(propName
, (String
) value
);
255 node
.getSession().save();
258 } catch (RepositoryException e
) {
259 throw new ArgeoException("Cannot property " + propName
+ " on "
260 + node
+ " with value " + value
, e
);
264 public void setUserAdminService(UserAdminService userAdminService
) {
265 this.userAdminService
= userAdminService
;