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
.MouseListener
;
25 import org
.eclipse
.swt
.events
.SelectionAdapter
;
26 import org
.eclipse
.swt
.events
.SelectionEvent
;
27 import org
.eclipse
.swt
.layout
.GridData
;
28 import org
.eclipse
.swt
.layout
.GridLayout
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Control
;
31 import org
.eclipse
.swt
.widgets
.Label
;
32 import org
.eclipse
.swt
.widgets
.Link
;
33 import org
.eclipse
.swt
.widgets
.Text
;
35 /** Display a single user main info once it has been created. */
36 public class UserPart
extends StyledControl
implements EditablePart
, NodePart
,
38 private static final long serialVersionUID
= -2883661960366940505L;
39 // private final static Log log = LogFactory.getLog(UserPart.class);
41 // A static list of supported properties.
42 private List
<Text
> texts
;
43 private final static String KEY_PROP_NAME
= "jcr:propertyName";
45 // the 2 password fields
46 private Text pwd1
, pwd2
;
48 private UserAdminService userAdminService
;
50 // private UserDetailsManager userDetailsManager;
52 // TODO implement to provide user creation ability for anonymous user?
53 // public UserPart(Composite parent, int swtStyle) {
54 // super(parent, swtStyle);
57 public UserPart(Composite parent
, int style
, Item item
)
58 throws RepositoryException
{
59 this(parent
, style
, item
, true);
62 public UserPart(Composite parent
, int style
, Item item
,
63 boolean cacheImmediately
) throws RepositoryException
{
64 super(parent
, style
, item
, cacheImmediately
);
68 public Item
getItem() throws RepositoryException
{
72 // Experimental, remove
73 public void setMouseListener(MouseListener mouseListener
) {
74 super.setMouseListener(mouseListener
);
76 for (Text txt
: texts
)
77 txt
.addMouseListener(mouseListener
);
82 protected Control
createControl(Composite box
, String style
) {
83 Composite body
= new Composite(box
, SWT
.NO_FOCUS
);
84 body
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
85 GridLayout layout
= new GridLayout(2, false);
86 // GridLayout layout = CmsUtils.noSpaceGridLayout(new GridLayout(2,
89 body
.setLayout(layout
);
90 CmsUtils
.style(body
, UserStyles
.USER_FORM_TEXT
);
93 Label header
= new Label(body
, SWT
.NONE
);
94 header
.setText(" General");
95 header
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 2,
97 CmsUtils
.style(header
, UserStyles
.USER_FORM_TITLE
);
100 createTexts(body
, UserStyles
.USER_FORM_TEXT
);
103 for (Text txt
: texts
)
104 txt
.addFocusListener(this);
106 // Change password link
108 header
= new Label(body
, SWT
.NONE
);
109 header
.setText(" Change password");
110 header
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 2,
112 CmsUtils
.style(header
, UserStyles
.USER_FORM_TITLE
);
114 pwd1
= createLP(body
, UserStyles
.USER_FORM_TEXT
, "Enter password");
115 pwd2
= createLP(body
, UserStyles
.USER_FORM_TEXT
, "Re-Enter");
117 final Link link
= new Link(body
, SWT
.NONE
);
118 link
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, true, false, 2, 1));
119 link
.setText("<a>Change password</a>");
120 link
.addSelectionListener(new SelectionAdapter() {
121 private static final long serialVersionUID
= 8348668888548451776L;
124 public void widgetSelected(SelectionEvent e
) {
126 if ("".equals(pwd1
.getText().trim()))
127 msg
= "Passwords cannot be blank";
128 else if (!pwd1
.getText().equals(pwd2
.getText()))
129 msg
= "Passwords do not match, please try again.";
132 MessageDialog
.openError(link
.getShell(), "Error", msg
);
135 String username
= getNode().getProperty(
136 ArgeoNames
.ARGEO_USER_ID
).getString();
137 if (userAdminService
.userExists(username
)) {
138 JcrUserDetails userDetails
= (JcrUserDetails
) userAdminService
139 .loadUserByUsername(username
);
140 userDetails
= userDetails
.cloneWithNewPassword(pwd1
142 userAdminService
.updateUser(userDetails
);
143 MessageDialog
.openInformation(link
.getShell(),
144 "Password changed", "Password changed.");
146 } catch (Exception re
) {
147 throw new ArgeoException(
148 "unable to reset password for user "
161 private void createTexts(Composite parent
, String style
) {
162 texts
= new ArrayList
<Text
>();
163 texts
.add(createLT(parent
, style
, "Displayed Name", Property
.JCR_TITLE
));
164 texts
.add(createLT(parent
, style
, "First name",
165 ArgeoNames
.ARGEO_FIRST_NAME
));
166 texts
.add(createLT(parent
, style
, "Last name",
167 ArgeoNames
.ARGEO_LAST_NAME
));
168 texts
.add(createLT(parent
, style
, "Email",
169 ArgeoNames
.ARGEO_PRIMARY_EMAIL
));
170 texts
.add(createLMT(parent
, style
, "Description",
171 Property
.JCR_DESCRIPTION
));
174 // protected Composite createEditLayout(Composite parent, String style) {
175 // Composite body = new Composite(parent, SWT.NO_FOCUS);
176 // GridLayout layout = new GridLayout(2, false);
177 // body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
178 // body.setLayout(layout);
180 // createTexts(body, UserStyles.USER_FORM_TEXT);
186 for (Text txt
: texts
) {
187 txt
.setText(get(getNode(), (String
) txt
.getData(KEY_PROP_NAME
)));
188 txt
.setEditable(isEditing());
189 // txt.setEnabled(isEditing());
193 // his.listener methods
195 public void focusGained(FocusEvent e
) {
200 public void focusLost(FocusEvent e
) {
201 // Save change if needed
202 Text text
= (Text
) e
.getSource();
203 set(getNode(), (String
) text
.getData(KEY_PROP_NAME
), text
.getText());
207 /** Creates label and text. */
208 protected Text
createLT(Composite body
, String style
, String label
,
210 Label lbl
= new Label(body
, SWT
.NONE
);
212 lbl
.setFont(EclipseUiUtils
.getBoldFont(body
));
213 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
214 Text text
= new Text(body
, SWT
.BORDER
);
215 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
216 CmsUtils
.style(text
, style
);
217 text
.setData(KEY_PROP_NAME
, propName
);
222 /** Creates label and password text. */
223 protected Text
createLP(Composite body
, String style
, String label
) {
224 Label lbl
= new Label(body
, SWT
.NONE
);
226 lbl
.setFont(EclipseUiUtils
.getBoldFont(body
));
227 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
228 Text text
= new Text(body
, SWT
.BORDER
| SWT
.PASSWORD
);
229 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
230 CmsUtils
.style(text
, style
);
234 /** Creates label and multiline text. */
235 protected Text
createLMT(Composite body
, String style
, String label
,
237 Label lbl
= new Label(body
, SWT
.NONE
);
239 lbl
.setFont(EclipseUiUtils
.getBoldFont(body
));
240 GridData gd
= new GridData(SWT
.RIGHT
, SWT
.TOP
, false, false);
241 gd
.verticalIndent
= 0;
242 lbl
.setLayoutData(gd
);
243 Text text
= new Text(body
, SWT
.BORDER
| SWT
.MULTI
| SWT
.WRAP
);
244 gd
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, true);
246 text
.setLayoutData(gd
);
247 CmsUtils
.style(text
, style
);
248 text
.setData(KEY_PROP_NAME
, propName
);
253 * Concisely get the string value of a property. Returns an empty String
254 * rather than null if this node doesn't have this property or if the
255 * corresponding property is an empty string.
257 private String
get(Node node
, String propertyName
) {
259 if (!node
.hasProperty(propertyName
))
262 return node
.getProperty(propertyName
).getString();
263 } catch (RepositoryException e
) {
264 throw new ArgeoException("Cannot get property " + propertyName
269 private boolean set(Node node
, String propName
, String value
) {
272 && (!node
.hasProperty(propName
) || node
273 .hasProperty(propName
)
274 && "".equals(node
.getProperty(propName
).getString())))
276 else if (node
.hasProperty(propName
)
277 && node
.getProperty(propName
).getString()
278 .equals((String
) value
))
281 node
.setProperty(propName
, (String
) value
);
282 node
.getSession().save();
285 } catch (RepositoryException e
) {
286 throw new ArgeoException("Cannot property " + propName
+ " on "
287 + node
+ " with value " + value
, e
);
291 /* DEPENDENCY INJECTION */
292 // public void setUserDetailsManager(UserDetailsManager userDetailsManager)
294 // this.userDetailsManager = userDetailsManager;
297 public void setUserAdminService(UserAdminService userAdminService
) {
298 this.userAdminService
= userAdminService
;