From 6410237a3a03436e8903a86da67110cf5fca1a44 Mon Sep 17 00:00:00 2001 From: Andreas Kohlbecker Date: Mon, 17 Mar 2014 22:03:05 +0000 Subject: [PATCH] taxeditor part of fix for #4121 (Changing password does not work) --- .../handler/OpenPasswordWizzardHandler.java | 8 +- .../ui/password/EditPasswordElement.java | 2 +- .../taxeditor/ui/password/PasswordWizard.java | 48 +++++++----- .../ui/password/PasswordWizardPage.java | 73 ++++++++++--------- 4 files changed, 72 insertions(+), 59 deletions(-) diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/handler/OpenPasswordWizzardHandler.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/handler/OpenPasswordWizzardHandler.java index e61f23cde..05273d42d 100644 --- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/handler/OpenPasswordWizzardHandler.java +++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/handler/OpenPasswordWizzardHandler.java @@ -16,7 +16,6 @@ import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.commands.IHandler; import org.eclipse.jface.wizard.WizardDialog; -import eu.etaxonomy.cdm.api.conversation.ConversationHolder; import eu.etaxonomy.cdm.model.common.User; import eu.etaxonomy.taxeditor.model.AbstractUtility; import eu.etaxonomy.taxeditor.store.CdmStore; @@ -41,17 +40,12 @@ public class OpenPasswordWizzardHandler extends AbstractHandler implements IHand Object principal = CdmStore.getCurrentAuthentiation().getPrincipal(); if(principal instanceof User){ - ConversationHolder conversation = CdmStore.createConversation(); - PasswordWizard wizard = new PasswordWizard(conversation, (User)principal); + PasswordWizard wizard = new PasswordWizard((User)principal, null); WizardDialog dialog = new WizardDialog(AbstractUtility.getShell(), wizard); dialog.open(); - // clean up - conversation.commit(); - conversation.unbind(); - conversation.close(); } else { // should never happen, log an error AbstractUtility.error(OpenPasswordWizzardHandler.class, "The principal currently authenticated is not a eu.etaxonomy.cdm.model.common.User", null); diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/password/EditPasswordElement.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/password/EditPasswordElement.java index 2f91f4a5b..63145b223 100644 --- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/password/EditPasswordElement.java +++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/password/EditPasswordElement.java @@ -66,7 +66,7 @@ public class EditPasswordElement extends AbstractCdmFormElement implements Selec */ @Override public void widgetSelected(SelectionEvent e) { - PasswordWizard wizard = new PasswordWizard(conversation, user); + PasswordWizard wizard = new PasswordWizard(user, conversation); WizardDialog dialog = new WizardDialog(getLayoutComposite().getShell(), wizard); dialog.open(); diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/password/PasswordWizard.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/password/PasswordWizard.java index 552388243..8240bfe52 100644 --- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/password/PasswordWizard.java +++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/password/PasswordWizard.java @@ -1,9 +1,9 @@ // $Id$ /** * Copyright (C) 2007 EDIT -* European Distributed Institute of Taxonomy +* European Distributed Institute of Taxonomy * http://www.e-taxonomy.eu -* +* * The contents of this file are subject to the Mozilla Public License Version 1.1 * See LICENSE.TXT at the top of this package for the full license terms. */ @@ -30,46 +30,58 @@ import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory; public class PasswordWizard extends Wizard implements IConversationEnabled{ private PasswordWizardPage passwordPage; - private User user; - private ConversationHolder conversation; - - public PasswordWizard(ConversationHolder conversation, User user){ + private final User user; + private final ConversationHolder conversation; + + /** + * @param user the user to change the password for + * @param conversation this optional parameter can be null. Only supply conversation if you + * need to run this wizard is a nested conversation + */ + public PasswordWizard(User user, ConversationHolder conversation){ this.conversation = conversation; this.user = user; } - + /* (non-Javadoc) * @see org.eclipse.jface.wizard.Wizard#addPages() */ @Override public void addPages() { CdmFormFactory formFactory = new CdmFormFactory(Display.getDefault()); - + passwordPage = new PasswordWizardPage(formFactory, getConversationHolder(), user); addPage(passwordPage); } - + /* (non-Javadoc) * @see org.eclipse.jface.wizard.Wizard#performFinish() */ @Override public boolean performFinish() { - String userName = user.getUsername(); - ConversationHolder internalConversation = CdmStore.getLoginManager().getConversationHolder(); - internalConversation.bind(); + + ConversationHolder internalConversation = CdmStore.getCurrentApplicationConfiguration().NewConversation(); + internalConversation.bind(); + internalConversation.startTransaction(); try{ - if(CdmStore.getLoginManager().isAdmin()){ - CdmStore.getService(IUserService.class).changePasswordForUser(userName, passwordPage.getNewPassword()); - }else{ + if(passwordPage.isChangingOwnPassword()){ + // change own password with validating of old one CdmStore.getService(IUserService.class).changePassword(passwordPage.getOldPassword(), passwordPage.getNewPassword()); + }else{ + // change others passwords + CdmStore.getService(IUserService.class).changePasswordForUser(user.getUsername(), passwordPage.getNewPassword()); } - internalConversation.commit(); + internalConversation.commit(false); + internalConversation.unbind(); + internalConversation.close(); return true; }catch(Exception e){ StoreUtil.errorDialog("Error while changing password", getClass(), "There was a problem changing the password.", e); return false; }finally{ - conversation.bind(); + if(conversation != null){ + conversation.bind(); + } } } @@ -79,7 +91,7 @@ public class PasswordWizard extends Wizard implements IConversationEnabled{ @Override public void update(CdmDataChangeMap arg0) { // TODO Auto-generated method stub - + } /* (non-Javadoc) diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/password/PasswordWizardPage.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/password/PasswordWizardPage.java index 1fa087abd..1e5ce2f62 100644 --- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/password/PasswordWizardPage.java +++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/password/PasswordWizardPage.java @@ -1,9 +1,9 @@ // $Id$ /** * Copyright (C) 2007 EDIT -* European Distributed Institute of Taxonomy +* European Distributed Institute of Taxonomy * http://www.e-taxonomy.eu -* +* * The contents of this file are subject to the Mozilla Public License Version 1.1 * See LICENSE.TXT at the top of this package for the full license terms. */ @@ -39,13 +39,13 @@ public class PasswordWizardPage extends AbstractCdmEntityWizardPage implem /** Constant NAME="USER_WIZARD_PAGE" */ public static final String NAME = "USER_WIZARD_PAGE"; - + private TextWithLabelElement text_oldPassword; private TextWithLabelElement text_password; private TextWithLabelElement text_passwordRepeat; - private PasswordValidator passwordValidator; + private final PasswordValidator passwordValidator; - private UniqueUserNameValidator uniqueUsernameValidator; + private final UniqueUserNameValidator uniqueUsernameValidator; /** * @param formFactory @@ -55,7 +55,7 @@ public class PasswordWizardPage extends AbstractCdmEntityWizardPage implem protected PasswordWizardPage(CdmFormFactory formFactory, ConversationHolder conversation, User entity) { super(formFactory, conversation, entity); - + passwordValidator = new PasswordValidator(); uniqueUsernameValidator = new UniqueUserNameValidator(); } @@ -67,24 +67,28 @@ public class PasswordWizardPage extends AbstractCdmEntityWizardPage implem public void createControl(Composite parent) { Composite control = formFactory.createComposite(parent); control.setLayoutData(LayoutConstants.FILL()); - + setPageComplete(false); - + control.setLayout(LayoutConstants.LAYOUT(2, false)); WizardPageRootElement rootElement = new WizardPageRootElement(formFactory, control, getConversationHolder()); - - if(!CdmStore.getLoginManager().isAdmin()){ - text_oldPassword = formFactory.createTextWithLabelElement(rootElement, "Old Password", null, SWT.PASSWORD); + + if(isChangingOwnPassword()) { + text_oldPassword = formFactory.createTextWithLabelElement(rootElement, "Old Password", null, SWT.PASSWORD); } text_password = formFactory.createTextWithLabelElement(rootElement, "New Password", null, SWT.PASSWORD); text_passwordRepeat = formFactory.createTextWithLabelElement(rootElement, "Repeat Password", null, SWT.PASSWORD); - + ((Text)text_passwordRepeat.getMainControl()).addModifyListener(this); - + setControl(control); } - - + + protected boolean isChangingOwnPassword() { + return getEntity() != null && getEntity().getUsername().equals(CdmStore.getCurrentAuthentiation().getName()); + } + + /* (non-Javadoc) * @see eu.etaxonomy.taxeditor.ui.forms.AbstractCdmEntityWizardPage#createElement(eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement) */ @@ -94,37 +98,39 @@ public class PasswordWizardPage extends AbstractCdmEntityWizardPage implem // not used return null; } - + /* (non-Javadoc) * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent) */ /** {@inheritDoc} */ - public void modifyText(ModifyEvent e) { + @Override + public void modifyText(ModifyEvent e) { validate(); } - + private void validate(){ String message; - + if((message = passwordValidator.passwordsMatch(text_password.getText(), text_passwordRepeat.getText())) != null){ // pass } else if((message = passwordValidator.isValid(text_password.getText())) != null){ // pass } - - setErrorMessage(message); + + setErrorMessage(message); } - + private class UniqueUserNameValidator implements IInputValidator{ private static final String USER_EXISTS = "Username already exists"; private static final String NAME_TO_SHORT = "Username is empty"; - + /* (non-Javadoc) * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String) */ - public String isValid(String newText) { + @Override + public String isValid(String newText) { // query for username if(newText.length() < 1){ setPageComplete(false); @@ -134,43 +140,44 @@ public class PasswordWizardPage extends AbstractCdmEntityWizardPage implem setPageComplete(false); return USER_EXISTS; } - + setPageComplete(true); return null; } } - + private class PasswordValidator implements IInputValidator{ private static final int PW_MIN_LENGTH = 5; - + private static final String TO_SHORT = "Password has to have at least " + PW_MIN_LENGTH + " characters"; private static final String NO_MATCH = "The passwords do not match"; - + /* (non-Javadoc) * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String) */ - public String isValid(String newText) { + @Override + public String isValid(String newText) { if(newText.length() < PW_MIN_LENGTH){ setPageComplete(false); return TO_SHORT; } - + setPageComplete(true); return null; } - + public String passwordsMatch(String password1, String password2){ if(! password1.equals(password2)){ setPageComplete(false); return NO_MATCH; } - + setPageComplete(true); return null; } - + } /** -- 2.34.1