import javax.jcr.RepositoryException;
import org.argeo.ArgeoException;
-import org.argeo.cms.CmsUtils;
+import org.argeo.cms.util.CmsUtils;
import org.argeo.cms.viewers.EditablePart;
import org.argeo.cms.viewers.NodePart;
import org.argeo.cms.widgets.StyledControl;
import org.argeo.eclipse.ui.EclipseUiUtils;
import org.argeo.jcr.ArgeoNames;
+import org.argeo.security.UserAdminService;
+import org.argeo.security.jcr.JcrUserDetails;
+import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
-import org.eclipse.swt.events.MouseListener;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Text;
/** Display a single user main info once it has been created. */
private List<Text> texts;
private final static String KEY_PROP_NAME = "jcr:propertyName";
+ // the 2 password fields
+ private Text pwd1, pwd2;
+
+ private UserAdminService userAdminService;
+
// TODO implement to provide user creation ability for anonymous user?
// public UserPart(Composite parent, int swtStyle) {
// super(parent, swtStyle);
return getNode();
}
- // Experimental, remove
- public void setMouseListener(MouseListener mouseListener) {
- super.setMouseListener(mouseListener);
+ @Override
+ protected Control createControl(Composite box, String style) {
+ Composite body = new Composite(box, SWT.NO_FOCUS);
+ body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
+ CmsUtils.style(body, UserStyles.USER_FORM_TEXT);
- for (Text txt : texts)
- txt.addMouseListener(mouseListener);
+ body.setLayout(new GridLayout(2, false));
- }
+ // Header
+ Label headerLbl = new Label(body, SWT.NONE);
+ headerLbl.setText(" Main user information");
+ headerLbl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
+ 2, 1));
+ CmsUtils.style(headerLbl, UserStyles.USER_FORM_TITLE);
+
+ // Form field
+ createTexts(body, UserStyles.USER_FORM_TEXT);
- @Override
- protected Control createControl(Composite box, String style) {
if (isEditing())
- return createEditLayout(box, style);
- else
- return createROLayout(box, style);
- }
+ for (Text txt : texts)
+ txt.addFocusListener(this);
- protected Composite createROLayout(Composite parent, String style) {
- Composite body = new Composite(parent, SWT.NO_FOCUS);
- body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
- GridLayout layout = new GridLayout(2, false);
- body.setLayout(layout);
+ // Change password link
+ headerLbl = new Label(body, SWT.NONE);
+ headerLbl.setText(" Reset password");
+ headerLbl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
+ 2, 1));
+ CmsUtils.style(headerLbl, UserStyles.USER_FORM_TITLE);
- createTexts(body, UserStyles.USER_FORM_TEXT);
- CmsUtils.style(body, UserStyles.USER_FORM_TEXT);
+ pwd1 = createLP(body, UserStyles.USER_FORM_TEXT, "Enter password");
+ pwd2 = createLP(body, UserStyles.USER_FORM_TEXT, "Re-Enter");
+
+ final Link link = new Link(body, SWT.NONE);
+ link.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 2,
+ 1));
+ link.setText("<a>Change password</a>");
+ link.addSelectionListener(new SelectionAdapter() {
+ private static final long serialVersionUID = 8348668888548451776L;
+
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ String msg = null;
+ if ("".equals(pwd1.getText().trim()))
+ msg = "Passwords cannot be blank";
+ else if (!pwd1.getText().equals(pwd2.getText()))
+ msg = "Passwords do not match, please try again.";
+
+ if (msg != null) {
+ MessageDialog.openError(link.getShell(), "Error", msg);
+ } else {
+ try {
+ String username = getNode().getProperty(
+ ArgeoNames.ARGEO_USER_ID).getString();
+ if (userAdminService.userExists(username)) {
+ JcrUserDetails userDetails = (JcrUserDetails) userAdminService
+ .loadUserByUsername(username);
+ userDetails = userDetails.cloneWithNewPassword(pwd1
+ .getText());
+ userAdminService.updateUser(userDetails);
+ MessageDialog.openInformation(link.getShell(),
+ "Password changed", "Password changed.");
+ }
+ } catch (Exception re) {
+ throw new ArgeoException(
+ "unable to reset password for user "
+ + getNode(), re);
+ }
+ }
+
+ pwd1.setText("");
+ pwd2.setText("");
+
+ }
+ });
return body;
}
Property.JCR_DESCRIPTION));
}
- protected Composite createEditLayout(Composite parent, String style) {
- Composite body = new Composite(parent, SWT.NO_FOCUS);
- GridLayout layout = new GridLayout(2, false);
- body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
- body.setLayout(layout);
-
- createTexts(body, UserStyles.USER_FORM_TEXT);
-
- for (Text txt : texts)
- txt.addFocusListener(this);
- CmsUtils.style(body, UserStyles.USER_FORM_TEXT);
- return body;
- }
-
void refresh() {
for (Text txt : texts) {
txt.setText(get(getNode(), (String) txt.getData(KEY_PROP_NAME)));
}
}
- // THE LISTENER
+ // his.listener methods
@Override
public void focusGained(FocusEvent e) {
// Do nothing
return text;
}
+ // HELPERS
+ /** Creates label and password text. */
+ protected Text createLP(Composite body, String style, String label) {
+ Label lbl = new Label(body, SWT.NONE);
+ lbl.setText(label);
+ lbl.setFont(EclipseUiUtils.getBoldFont(body));
+ lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
+ Text text = new Text(body, SWT.BORDER | SWT.PASSWORD);
+ text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
+ CmsUtils.style(text, style);
+ return text;
+ }
+
/** Creates label and multiline text. */
protected Text createLMT(Composite body, String style, String label,
String propName) {
+ node + " with value " + value, e);
}
}
+
+ public void setUserAdminService(UserAdminService userAdminService) {
+ this.userAdminService = userAdminService;
+ }
}
\ No newline at end of file