Project

General

Profile

Download (4.3 KB) Statistics
| Branch: | Tag: | Revision:
1 329facb4 n.hoffmann
// $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.store;
12
13
import java.util.Observable;
14
15 3d3773ab n.hoffmann
import org.eclipse.core.runtime.IProgressMonitor;
16
import org.eclipse.ui.IMemento;
17 bcfe7309 n.hoffmann
import org.springframework.security.authentication.BadCredentialsException;
18
import org.springframework.security.authentication.LockedException;
19 eca18c8b n.hoffmann
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
20
import org.springframework.security.core.Authentication;
21
import org.springframework.security.core.context.SecurityContextHolder;
22 329facb4 n.hoffmann
23 a60842d7 n.hoffmann
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
24
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
25 329facb4 n.hoffmann
import eu.etaxonomy.cdm.model.common.User;
26 a60842d7 n.hoffmann
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
27 3d3773ab n.hoffmann
import eu.etaxonomy.taxeditor.model.IContextListener;
28 329facb4 n.hoffmann
29
/**
30 3be6ef3e n.hoffmann
 * <p>LoginManager class.</p>
31
 *
32 329facb4 n.hoffmann
 * @author n.hoffmann
33
 * @created 03.07.2009
34
 * @version 1.0
35
 */
36 3d3773ab n.hoffmann
public class LoginManager extends Observable implements IConversationEnabled, IContextListener{
37 a60842d7 n.hoffmann
	
38
	private ConversationHolder conversation;
39 329facb4 n.hoffmann
	
40 3d3773ab n.hoffmann
	public LoginManager(){
41
		CdmStore.getContextManager().addContextListener(this);
42
	}
43
	
44 329facb4 n.hoffmann
	/**
45 3be6ef3e n.hoffmann
	 * <p>authenticate</p>
46
	 *
47
	 * @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
48 329facb4 n.hoffmann
	 */
49 8754b7c4 n.hoffmann
	public boolean authenticate(String username, String password){		
50 bcfe7309 n.hoffmann
		try{
51 a60842d7 n.hoffmann
			getConversationHolder().bind();
52 bcfe7309 n.hoffmann
			UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password); 
53
			
54
			SecurityContextHolder.clearContext();
55
			Authentication authentication = CdmStore.getAuthenticationManager().authenticate(token);		
56
			SecurityContextHolder.getContext().setAuthentication(authentication);
57 8754b7c4 n.hoffmann
			this.setChanged();
58
			this.notifyObservers();
59
			return true;
60 bcfe7309 n.hoffmann
		}
61
		catch(BadCredentialsException e){
62
			StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
63
		}
64
		catch(LockedException e){
65
			StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
66
		}
67
		catch(IllegalArgumentException e){
68
			StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
69
		}
70 8754b7c4 n.hoffmann
		return false;
71 329facb4 n.hoffmann
	}
72
	
73
	/**
74 3be6ef3e n.hoffmann
	 * <p>getAuthenticatedUser</p>
75
	 *
76
	 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
77 329facb4 n.hoffmann
	 */
78
	public User getAuthenticatedUser(){
79
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
80
		
81
		if(authentication != null 
82
				&& authentication.getPrincipal() != null 
83
				&& authentication.getPrincipal() instanceof User){
84
			return (User)authentication.getPrincipal();
85
		}
86
		return null;
87
	}
88 bcfe7309 n.hoffmann
	
89
	public void logoutAll(){
90
		SecurityContextHolder.clearContext();
91
		notifyObservers();		
92
	}
93 a60842d7 n.hoffmann
94
	/* (non-Javadoc)
95
	 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
96
	 */
97
	@Override
98
	public void update(CdmDataChangeMap arg) {}
99
100
	/* (non-Javadoc)
101
	 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
102
	 */
103
	@Override
104
	public ConversationHolder getConversationHolder() {
105
		if(conversation == null){
106
			conversation = CdmStore.createConversation();
107
		}
108
		return conversation;
109
	}
110 59351073 n.hoffmann
111
	/**
112
	 * Whether the current user has the role admin
113
	 * 
114
	 * @return
115
	 */
116
	public boolean isAdmin() {
117
		// FIXME until we have rights implemented properly we do this
118
		// by a simple string check. This has to change 
119
		
120
		return "admin".equals(getAuthenticatedUser().getUsername());
121
	}
122 3d3773ab n.hoffmann
123
	@Override
124
	public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
125
		
126
	}
127
128
	@Override
129
	public void contextStop(IMemento memento, IProgressMonitor monitor) {
130
		
131
	}
132
133
	@Override
134 c15a26dc n.hoffmann
	public void contextStart(IMemento memento, IProgressMonitor monitor){
135 3d3773ab n.hoffmann
		conversation = CdmStore.createConversation();
136
	}
137
138
	@Override
139
	public void contextRefresh(IProgressMonitor monitor) {
140
		conversation = CdmStore.createConversation();
141
	}
142
143
	@Override
144
	public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
145
		
146
	}
147 329facb4 n.hoffmann
}