Project

General

Profile

« Previous | Next » 

Revision 6410237a

Added by Andreas Kohlbecker about 10 years ago

taxeditor part of fix for #4121 (Changing password does not work)

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/handler/OpenPasswordWizzardHandler.java
16 16
import org.eclipse.core.commands.IHandler;
17 17
import org.eclipse.jface.wizard.WizardDialog;
18 18

  
19
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
20 19
import eu.etaxonomy.cdm.model.common.User;
21 20
import eu.etaxonomy.taxeditor.model.AbstractUtility;
22 21
import eu.etaxonomy.taxeditor.store.CdmStore;
......
41 40
	    Object principal = CdmStore.getCurrentAuthentiation().getPrincipal();
42 41

  
43 42
	    if(principal instanceof User){
44
	        ConversationHolder conversation = CdmStore.createConversation();
45 43

  
46
	        PasswordWizard wizard = new PasswordWizard(conversation, (User)principal);
44
	        PasswordWizard wizard = new PasswordWizard((User)principal, null);
47 45
	        WizardDialog dialog = new WizardDialog(AbstractUtility.getShell(), wizard);
48 46

  
49 47
	        dialog.open();
50 48

  
51
	        // clean up
52
	        conversation.commit();
53
	        conversation.unbind();
54
	        conversation.close();
55 49
	    } else {
56 50
	        // should never happen, log an error
57 51
	        AbstractUtility.error(OpenPasswordWizzardHandler.class, "The principal currently authenticated is not a eu.etaxonomy.cdm.model.common.User", null);
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/password/EditPasswordElement.java
66 66
	 */
67 67
	@Override
68 68
	public void widgetSelected(SelectionEvent e) {
69
		PasswordWizard wizard = new PasswordWizard(conversation, user);
69
		PasswordWizard wizard = new PasswordWizard(user, conversation);
70 70
		WizardDialog dialog = new WizardDialog(getLayoutComposite().getShell(), wizard);
71 71
		
72 72
		dialog.open();
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/password/PasswordWizard.java
1 1
// $Id$
2 2
/**
3 3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
4
* European Distributed Institute of Taxonomy
5 5
* http://www.e-taxonomy.eu
6
* 
6
*
7 7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8 8
* See LICENSE.TXT at the top of this package for the full license terms.
9 9
*/
......
30 30
public class PasswordWizard extends Wizard implements IConversationEnabled{
31 31

  
32 32
	private PasswordWizardPage passwordPage;
33
	private User user;
34
	private ConversationHolder conversation;
35
	
36
	public PasswordWizard(ConversationHolder conversation, User user){
33
	private final User user;
34
	private final ConversationHolder conversation;
35

  
36
	/**
37
	 * @param user the user to change the password for
38
	 * @param conversation this optional parameter can be null. Only supply conversation if you
39
	 * need to run this wizard is a nested conversation
40
	 */
41
	public PasswordWizard(User user, ConversationHolder conversation){
37 42
		this.conversation = conversation;
38 43
		this.user = user;
39 44
	}
40
	
45

  
41 46
	/* (non-Javadoc)
42 47
	 * @see org.eclipse.jface.wizard.Wizard#addPages()
43 48
	 */
44 49
	@Override
45 50
	public void addPages() {
46 51
		CdmFormFactory formFactory = new CdmFormFactory(Display.getDefault());
47
		
52

  
48 53
		passwordPage = new PasswordWizardPage(formFactory, getConversationHolder(), user);
49 54
		addPage(passwordPage);
50 55
	}
51
	
56

  
52 57
	/* (non-Javadoc)
53 58
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
54 59
	 */
55 60
	@Override
56 61
	public boolean performFinish() {
57
		String userName = user.getUsername();
58
		ConversationHolder internalConversation = CdmStore.getLoginManager().getConversationHolder();
59
		internalConversation.bind();
62

  
63
		ConversationHolder internalConversation = CdmStore.getCurrentApplicationConfiguration().NewConversation();
64
	    internalConversation.bind();
65
	    internalConversation.startTransaction();
60 66
		try{
61
			if(CdmStore.getLoginManager().isAdmin()){
62
				CdmStore.getService(IUserService.class).changePasswordForUser(userName, passwordPage.getNewPassword());
63
			}else{
67
			if(passwordPage.isChangingOwnPassword()){
68
			    // change own password with validating of old one
64 69
				CdmStore.getService(IUserService.class).changePassword(passwordPage.getOldPassword(), passwordPage.getNewPassword());
70
			}else{
71
			    // change others passwords
72
			    CdmStore.getService(IUserService.class).changePasswordForUser(user.getUsername(), passwordPage.getNewPassword());
65 73
			}
66
			internalConversation.commit();
74
		    internalConversation.commit(false);
75
		    internalConversation.unbind();
76
		    internalConversation.close();
67 77
			return true;
68 78
		}catch(Exception e){
69 79
			StoreUtil.errorDialog("Error while changing password", getClass(), "There was a problem changing the password.", e);
70 80
			return false;
71 81
		}finally{
72
			conversation.bind();
82
		    if(conversation != null){
83
		        conversation.bind();
84
		    }
73 85
		}
74 86
	}
75 87

  
......
79 91
	@Override
80 92
	public void update(CdmDataChangeMap arg0) {
81 93
		// TODO Auto-generated method stub
82
		
94

  
83 95
	}
84 96

  
85 97
	/* (non-Javadoc)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/password/PasswordWizardPage.java
1 1
// $Id$
2 2
/**
3 3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
4
* European Distributed Institute of Taxonomy
5 5
* http://www.e-taxonomy.eu
6
* 
6
*
7 7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8 8
* See LICENSE.TXT at the top of this package for the full license terms.
9 9
*/
......
39 39

  
40 40
	/** Constant <code>NAME="USER_WIZARD_PAGE"</code> */
41 41
	public static final String NAME = "USER_WIZARD_PAGE";
42
	
42

  
43 43
	private TextWithLabelElement text_oldPassword;
44 44
	private TextWithLabelElement text_password;
45 45
	private TextWithLabelElement text_passwordRepeat;
46
	private PasswordValidator passwordValidator;
46
	private final PasswordValidator passwordValidator;
47 47

  
48
	private UniqueUserNameValidator uniqueUsernameValidator;
48
	private final UniqueUserNameValidator uniqueUsernameValidator;
49 49

  
50 50
	/**
51 51
	 * @param formFactory
......
55 55
	protected PasswordWizardPage(CdmFormFactory formFactory,
56 56
			ConversationHolder conversation, User entity) {
57 57
		super(formFactory, conversation, entity);
58
		
58

  
59 59
		passwordValidator = new PasswordValidator();
60 60
		uniqueUsernameValidator = new UniqueUserNameValidator();
61 61
	}
......
67 67
	public void createControl(Composite parent) {
68 68
		Composite control = formFactory.createComposite(parent);
69 69
		control.setLayoutData(LayoutConstants.FILL());
70
		
70

  
71 71
		setPageComplete(false);
72
		
72

  
73 73
		control.setLayout(LayoutConstants.LAYOUT(2, false));
74 74
		WizardPageRootElement rootElement = new WizardPageRootElement(formFactory, control, getConversationHolder());
75
		
76
		if(!CdmStore.getLoginManager().isAdmin()){
77
			text_oldPassword = formFactory.createTextWithLabelElement(rootElement, "Old Password", null, SWT.PASSWORD);
75

  
76
		if(isChangingOwnPassword()) {
77
		    text_oldPassword = formFactory.createTextWithLabelElement(rootElement, "Old Password", null, SWT.PASSWORD);
78 78
		}
79 79
		text_password =  formFactory.createTextWithLabelElement(rootElement, "New Password", null, SWT.PASSWORD);
80 80
		text_passwordRepeat =  formFactory.createTextWithLabelElement(rootElement, "Repeat Password", null, SWT.PASSWORD);
81
		
81

  
82 82
		((Text)text_passwordRepeat.getMainControl()).addModifyListener(this);
83
		
83

  
84 84
		setControl(control);
85 85
	}
86
	
87
	
86

  
87
    protected boolean isChangingOwnPassword() {
88
        return getEntity() != null && getEntity().getUsername().equals(CdmStore.getCurrentAuthentiation().getName());
89
    }
90

  
91

  
88 92
	/* (non-Javadoc)
89 93
	 * @see eu.etaxonomy.taxeditor.ui.forms.AbstractCdmEntityWizardPage#createElement(eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement)
90 94
	 */
......
94 98
		// not used
95 99
		return null;
96 100
	}
97
	
101

  
98 102
	/* (non-Javadoc)
99 103
	 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
100 104
	 */
101 105
	/** {@inheritDoc} */
102
	public void modifyText(ModifyEvent e) {
106
	@Override
107
    public void modifyText(ModifyEvent e) {
103 108
		validate();
104 109
	}
105
	
110

  
106 111
	private void validate(){
107 112
		String message;
108
		
113

  
109 114
		if((message = passwordValidator.passwordsMatch(text_password.getText(), text_passwordRepeat.getText())) != null){
110 115
			// pass
111 116
		}
112 117
		else if((message = passwordValidator.isValid(text_password.getText())) != null){
113 118
			// pass
114 119
		}
115
		
116
		setErrorMessage(message);		
120

  
121
		setErrorMessage(message);
117 122
	}
118
	
123

  
119 124
	private class UniqueUserNameValidator implements IInputValidator{
120 125

  
121 126
		private static final String USER_EXISTS = "Username already exists";
122 127
		private static final String NAME_TO_SHORT = "Username is empty";
123
		
128

  
124 129
		/* (non-Javadoc)
125 130
		 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
126 131
		 */
127
		public String isValid(String newText) {
132
		@Override
133
        public String isValid(String newText) {
128 134
			// query for username
129 135
			if(newText.length() < 1){
130 136
				setPageComplete(false);
......
134 140
				setPageComplete(false);
135 141
				return USER_EXISTS;
136 142
			}
137
			
143

  
138 144
			setPageComplete(true);
139 145
			return null;
140 146
		}
141 147
	}
142
	
148

  
143 149
	private class PasswordValidator implements IInputValidator{
144 150

  
145 151
		private static final int PW_MIN_LENGTH = 5;
146
		
152

  
147 153
		private static final String TO_SHORT = "Password has to have at least " + PW_MIN_LENGTH + " characters";
148 154
		private static final String NO_MATCH = "The passwords do not match";
149
		
155

  
150 156
		/* (non-Javadoc)
151 157
		 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
152 158
		 */
153
		public String isValid(String newText) {
159
		@Override
160
        public String isValid(String newText) {
154 161
			if(newText.length() < PW_MIN_LENGTH){
155 162
				setPageComplete(false);
156 163
				return TO_SHORT;
157 164
			}
158
			
165

  
159 166
			setPageComplete(true);
160 167
			return null;
161 168
		}
162
		
169

  
163 170
		public String passwordsMatch(String password1, String password2){
164 171

  
165 172
			if(! password1.equals(password2)){
166 173
				setPageComplete(false);
167 174
				return NO_MATCH;
168 175
			}
169
			
176

  
170 177
			setPageComplete(true);
171 178
			return null;
172 179
		}
173
		
180

  
174 181
	}
175 182

  
176 183
	/**

Also available in: Unified diff