Project

General

Profile

Download (5.97 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
import java.util.Set;
15

    
16
import org.apache.log4j.Logger;
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.ui.IMemento;
19
import org.springframework.security.authentication.BadCredentialsException;
20
import org.springframework.security.authentication.LockedException;
21
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
22
import org.springframework.security.core.Authentication;
23
import org.springframework.security.core.GrantedAuthority;
24
import org.springframework.security.core.context.SecurityContextHolder;
25

    
26
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
27
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
28
import eu.etaxonomy.cdm.model.common.Group;
29
import eu.etaxonomy.cdm.model.common.User;
30
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
31
import eu.etaxonomy.taxeditor.model.AbstractUtility;
32
import eu.etaxonomy.taxeditor.model.IContextListener;
33
import eu.etaxonomy.taxeditor.model.MessagingUtils;
34

    
35
/**
36
 * <p>LoginManager class.</p>
37
 *
38
 * @author n.hoffmann
39
 * @created 03.07.2009
40
 * @version 1.0
41
 */
42
public class LoginManager extends Observable implements IConversationEnabled, IContextListener{
43

    
44
	public static final Logger logger = Logger.getLogger(LoginManager.class);
45

    
46
	private ConversationHolder conversation;
47

    
48
	public LoginManager(){
49
		CdmStore.getContextManager().addContextListener(this);
50
	}
51

    
52
	/**
53
	 * <p>authenticate</p>
54
	 *
55
	 * @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
56
	 * @return true if the login attempt was successful even if the authentication has changed or not
57
	 */
58
	public boolean authenticate(String username, String password){
59

    
60
		// close all open editors
61
		if(!AbstractUtility.closeAll()){
62
			return false;
63
		}
64

    
65

    
66
		try{
67
			SecurityContextHolder.clearContext();
68

    
69
			Authentication lastAuthentication = CdmStore.getCurrentAuthentiation();
70

    
71
			UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
72
			Authentication authentication = CdmStore.getAuthenticationManager().authenticate(token);
73

    
74
			User user = (User) authentication.getPrincipal();
75
			/* circumventing problem with hibernate not refreshing the transient collection authorities in this case,
76
			 * see http://dev.e-taxonomy.eu/trac/ticket/4053 */
77
			user.initAuthorities();
78

    
79
			if(logger.isDebugEnabled()){
80
				StringBuilder gaText = new StringBuilder();
81
				String indent = "    ";
82
				Set<GrantedAuthority> gaSet = user.getGrantedAuthorities();
83
				_logGrantedAuthotities(gaText, indent, gaSet);
84
				for(Group gr : user.getGroups()){
85
					gaText.append(indent).append("gr[").append(gr.hashCode()).append("] \"").append(gr.getName()).append("\" ").append(gr.toString()).append("\n");
86
					_logGrantedAuthotities(gaText, indent + indent, gr.getGrantedAuthorities());
87
				}
88
				logger.debug("User authenticated: " + user.getUsername() + "\n" + gaText.toString());
89
			}
90

    
91
			SecurityContextHolder.getContext().setAuthentication(authentication);
92

    
93
			if(!authentication.equals(lastAuthentication)){
94
				this.setChanged();
95
				this.notifyObservers();
96
			}
97
			return true;
98
		}
99
		catch(BadCredentialsException e){
100
			MessagingUtils.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
101
		}
102
		catch(LockedException e){
103
			MessagingUtils.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
104
		}
105
		catch(IllegalArgumentException e){
106
			MessagingUtils.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
107
		}
108
		return false;
109
	}
110

    
111
	private void _logGrantedAuthotities(StringBuilder gaText, String indent,
112
			Set<GrantedAuthority> gaSet) {
113
		for(GrantedAuthority ga : gaSet){
114
			gaText.append(indent).append("ga[").append(ga.hashCode()).append("] ").append(ga.toString()).append("\n");
115
		}
116
	}
117

    
118
	/**
119
	 * <p>getAuthenticatedUser</p>
120
	 *
121
	 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
122
	 */
123
	public User getAuthenticatedUser(){
124
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
125

    
126
		if(authentication != null
127
				&& authentication.getPrincipal() != null
128
				&& authentication.getPrincipal() instanceof User){
129
			return (User)authentication.getPrincipal();
130
		}
131
		return null;
132
	}
133

    
134
	public void logoutAll(){
135
		SecurityContextHolder.clearContext();
136
		notifyObservers();
137
	}
138

    
139
	/* (non-Javadoc)
140
	 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
141
	 */
142
	@Override
143
	public void update(CdmDataChangeMap arg) {}
144

    
145
	/* (non-Javadoc)
146
	 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
147
	 */
148
	@Override
149
	public ConversationHolder getConversationHolder() {
150
		if(conversation == null){
151
			conversation = CdmStore.createConversation();
152
		}
153
		return conversation;
154
	}
155

    
156
	/**
157
	 * Whether the current user has the role admin
158
	 *
159
	 * @return
160
	 */
161
	public boolean isAdmin() {
162
		// FIXME until we have rights implemented properly we do this
163
		// by a simple string check. This has to change
164

    
165
		return "admin".equals(getAuthenticatedUser().getUsername());
166
	}
167

    
168
	@Override
169
	public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
170

    
171
	}
172

    
173
	@Override
174
	public void contextStop(IMemento memento, IProgressMonitor monitor) {
175

    
176
	}
177

    
178
	@Override
179
	public void contextStart(IMemento memento, IProgressMonitor monitor){
180
		conversation = CdmStore.createConversation();
181
	}
182

    
183
	@Override
184
	public void contextRefresh(IProgressMonitor monitor) {
185
		conversation = CdmStore.createConversation();
186
	}
187

    
188
	@Override
189
	public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
190

    
191
	}
192
}
(5-5/9)