Project

General

Profile

Download (3.54 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.store;
12

    
13
import java.util.Observable;
14

    
15
import org.springframework.security.authentication.BadCredentialsException;
16
import org.springframework.security.authentication.LockedException;
17
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
18
import org.springframework.security.core.Authentication;
19
import org.springframework.security.core.context.SecurityContextHolder;
20

    
21
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
22
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
23
import eu.etaxonomy.cdm.model.common.User;
24
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
25

    
26
/**
27
 * <p>LoginManager class.</p>
28
 *
29
 * @author n.hoffmann
30
 * @created 03.07.2009
31
 * @version 1.0
32
 */
33
public class LoginManager extends Observable implements IConversationEnabled{
34
	
35
	private ConversationHolder conversation;
36
	
37
	/**
38
	 * <p>authenticate</p>
39
	 *
40
	 * @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
41
	 */
42
	public boolean authenticate(String username, String password){		
43
		try{
44
			getConversationHolder().bind();
45
			UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password); 
46
			
47
			SecurityContextHolder.clearContext();
48
			Authentication authentication = CdmStore.getAuthenticationManager().authenticate(token);		
49
			SecurityContextHolder.getContext().setAuthentication(authentication);
50
			this.setChanged();
51
			this.notifyObservers();
52
			return true;
53
		}
54
		catch(BadCredentialsException e){
55
			StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
56
		}
57
		catch(LockedException e){
58
			StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
59
		}
60
		catch(IllegalArgumentException e){
61
			StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
62
		}
63
		return false;
64
	}
65
	
66
	/**
67
	 * <p>getAuthenticatedUser</p>
68
	 *
69
	 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
70
	 */
71
	public User getAuthenticatedUser(){
72
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
73
		
74
		if(authentication != null 
75
				&& authentication.getPrincipal() != null 
76
				&& authentication.getPrincipal() instanceof User){
77
			return (User)authentication.getPrincipal();
78
		}
79
		return null;
80
	}
81
	
82
	public void logoutAll(){
83
		SecurityContextHolder.clearContext();
84
		notifyObservers();		
85
	}
86

    
87
	/* (non-Javadoc)
88
	 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
89
	 */
90
	@Override
91
	public void update(CdmDataChangeMap arg) {}
92

    
93
	/* (non-Javadoc)
94
	 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
95
	 */
96
	@Override
97
	public ConversationHolder getConversationHolder() {
98
		if(conversation == null){
99
			conversation = CdmStore.createConversation();
100
		}
101
		return conversation;
102
	}
103

    
104
	/**
105
	 * Whether the current user has the role admin
106
	 * 
107
	 * @return
108
	 */
109
	public boolean isAdmin() {
110
		// FIXME until we have rights implemented properly we do this
111
		// by a simple string check. This has to change 
112
		
113
		return "admin".equals(getAuthenticatedUser().getUsername());
114
	}
115
}
(5-5/9)