]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/users/UserPart.java
Use message instead of label in user menu
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / users / UserPart.java
index a422521e17e17bf550f2b9459106c0420fab4d57..b3e7c9cf2377d19c8621a9e5fef820c1231f0fb4 100644 (file)
@@ -9,21 +9,26 @@ import javax.jcr.Property;
 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. */
@@ -36,6 +41,11 @@ public class UserPart extends StyledControl implements EditablePart, NodePart,
        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);
@@ -56,31 +66,80 @@ public class UserPart extends StyledControl implements EditablePart, NodePart,
                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;
        }
 
@@ -97,20 +156,6 @@ public class UserPart extends StyledControl implements EditablePart, NodePart,
                                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)));
@@ -118,7 +163,7 @@ public class UserPart extends StyledControl implements EditablePart, NodePart,
                }
        }
 
-       // THE LISTENER
+       // his.listener methods
        @Override
        public void focusGained(FocusEvent e) {
                // Do nothing
@@ -146,6 +191,19 @@ public class UserPart extends StyledControl implements EditablePart, NodePart,
                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) {
@@ -202,4 +260,8 @@ public class UserPart extends StyledControl implements EditablePart, NodePart,
                                        + node + " with value " + value, e);
                }
        }
+
+       public void setUserAdminService(UserAdminService userAdminService) {
+               this.userAdminService = userAdminService;
+       }
 }
\ No newline at end of file