Project

General

Profile

Download (3.49 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.taxeditor.ui.password;
12

    
13
import org.eclipse.jface.wizard.Wizard;
14
import org.eclipse.swt.widgets.Display;
15
import org.springframework.security.access.AccessDeniedException;
16
import org.springframework.security.core.AuthenticationException;
17

    
18
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
19
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
20
import eu.etaxonomy.cdm.api.service.IUserService;
21
import eu.etaxonomy.cdm.model.common.User;
22
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
23
import eu.etaxonomy.taxeditor.Messages;
24
import eu.etaxonomy.taxeditor.model.MessagingUtils;
25
import eu.etaxonomy.taxeditor.store.CdmStore;
26
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
27

    
28
/**
29
 * @author n.hoffmann
30
 * @created Mar 10, 2011
31
 * @version 1.0
32
 */
33
public class PasswordWizard extends Wizard implements IConversationEnabled{
34

    
35
	private PasswordWizardPage passwordPage;
36
	private final User user;
37
	private final ConversationHolder conversation;
38

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

    
50
	@Override
51
	public void addPages() {
52
		CdmFormFactory formFactory = new CdmFormFactory(Display.getDefault());
53

    
54
		passwordPage = new PasswordWizardPage(formFactory, getConversationHolder(), user);
55
		addPage(passwordPage);
56
	}
57

    
58
	@Override
59
	public boolean performFinish() {
60

    
61
		ConversationHolder internalConversation = CdmStore.getCurrentApplicationConfiguration().NewConversation();
62
	    internalConversation.bind();
63
	    internalConversation.startTransaction();
64
		String warningTitle = Messages.PasswordWizard_COULD_NOT_CHANGE_PWD;
65
        String warningMessage = Messages.PasswordWizard_OLD_PWD_INCORRECT;
66
        try{
67
			if(passwordPage.isChangingOwnPassword()){
68
			    // change own password with validating of old one
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());
73
			}
74
			CdmStore.getService(IUserService.class).loadWithUpdate(user.getUuid());
75
		    internalConversation.commit(false);
76
		    internalConversation.unbind();
77
		    internalConversation.close();
78
			return true;
79
		}catch(AccessDeniedException e){
80
			MessagingUtils.warningDialog(warningTitle, this, warningMessage);
81
			return false;
82
		}catch (AuthenticationException e){
83
			MessagingUtils.warningDialog(warningTitle, this, warningMessage);
84

    
85
			return false;
86
		}catch(Exception e){
87
			MessagingUtils.warningDialog(Messages.PasswordWizard_PROBLEM_WITH_CHANGING_PWD, this, Messages.PasswordWizard_PWD_COULD_NOT_BE_CHANGED + e.getMessage());
88
			return false;
89
		}finally{
90
		    if(conversation != null){
91
		        conversation.bind();
92
		    }
93
		}
94
	}
95

    
96
	@Override
97
	public void update(CdmDataChangeMap arg0) {
98
	}
99

    
100
	@Override
101
	public ConversationHolder getConversationHolder() {
102
		return conversation;
103
	}
104

    
105
}
(2-2/3)