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.eclipse.core.runtime.IProgressMonitor;
|
16
|
import org.eclipse.ui.IMemento;
|
17
|
import org.springframework.security.authentication.BadCredentialsException;
|
18
|
import org.springframework.security.authentication.LockedException;
|
19
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
20
|
import org.springframework.security.core.Authentication;
|
21
|
import org.springframework.security.core.context.SecurityContextHolder;
|
22
|
|
23
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
24
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
25
|
import eu.etaxonomy.cdm.model.common.User;
|
26
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
27
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
28
|
|
29
|
/**
|
30
|
* <p>LoginManager class.</p>
|
31
|
*
|
32
|
* @author n.hoffmann
|
33
|
* @created 03.07.2009
|
34
|
* @version 1.0
|
35
|
*/
|
36
|
public class LoginManager extends Observable implements IConversationEnabled, IContextListener{
|
37
|
|
38
|
private ConversationHolder conversation;
|
39
|
|
40
|
public LoginManager(){
|
41
|
CdmStore.getContextManager().addContextListener(this);
|
42
|
}
|
43
|
|
44
|
/**
|
45
|
* <p>authenticate</p>
|
46
|
*
|
47
|
* @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
|
48
|
*/
|
49
|
public boolean authenticate(String username, String password){
|
50
|
try{
|
51
|
getConversationHolder().bind();
|
52
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
|
53
|
|
54
|
SecurityContextHolder.clearContext();
|
55
|
Authentication authentication = CdmStore.getAuthenticationManager().authenticate(token);
|
56
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
57
|
this.setChanged();
|
58
|
this.notifyObservers();
|
59
|
return true;
|
60
|
}
|
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
|
return false;
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* <p>getAuthenticatedUser</p>
|
75
|
*
|
76
|
* @return a {@link eu.etaxonomy.cdm.model.common.User} object.
|
77
|
*/
|
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
|
|
89
|
public void logoutAll(){
|
90
|
SecurityContextHolder.clearContext();
|
91
|
notifyObservers();
|
92
|
}
|
93
|
|
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
|
|
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
|
|
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
|
public void contextStart(IMemento memento, IProgressMonitor monitor){
|
135
|
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
|
}
|