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.IEditorPart;
|
17
|
import org.eclipse.ui.IMemento;
|
18
|
import org.eclipse.ui.IWorkbenchPage;
|
19
|
import org.eclipse.ui.IWorkbenchPart;
|
20
|
import org.eclipse.ui.IWorkbenchWindow;
|
21
|
import org.eclipse.ui.editors.text.EditorsUI;
|
22
|
import org.eclipse.ui.handlers.HandlerUtil;
|
23
|
import org.springframework.security.authentication.BadCredentialsException;
|
24
|
import org.springframework.security.authentication.LockedException;
|
25
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
26
|
import org.springframework.security.core.Authentication;
|
27
|
import org.springframework.security.core.context.SecurityContextHolder;
|
28
|
|
29
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
30
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
31
|
import eu.etaxonomy.cdm.model.common.User;
|
32
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
33
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
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
|
private ConversationHolder conversation;
|
45
|
|
46
|
public LoginManager(){
|
47
|
CdmStore.getContextManager().addContextListener(this);
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* <p>authenticate</p>
|
52
|
*
|
53
|
* @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
|
54
|
*/
|
55
|
public boolean authenticate(String username, String password){
|
56
|
|
57
|
// close all open editors, this will close all open conversations
|
58
|
// otherwise the users granted authorities might not be up to date
|
59
|
if(!StoreUtil.closeAll()){
|
60
|
return false;
|
61
|
}
|
62
|
|
63
|
|
64
|
try{
|
65
|
getConversationHolder().bind();
|
66
|
|
67
|
SecurityContextHolder.clearContext();
|
68
|
|
69
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
|
70
|
Authentication authentication = CdmStore.getAuthenticationManager().authenticate(token);
|
71
|
|
72
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
73
|
|
74
|
this.setChanged();
|
75
|
this.notifyObservers();
|
76
|
return true;
|
77
|
}
|
78
|
catch(BadCredentialsException e){
|
79
|
StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
|
80
|
}
|
81
|
catch(LockedException e){
|
82
|
StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
|
83
|
}
|
84
|
catch(IllegalArgumentException e){
|
85
|
StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
|
86
|
}
|
87
|
return false;
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* <p>getAuthenticatedUser</p>
|
92
|
*
|
93
|
* @return a {@link eu.etaxonomy.cdm.model.common.User} object.
|
94
|
*/
|
95
|
public User getAuthenticatedUser(){
|
96
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
97
|
|
98
|
if(authentication != null
|
99
|
&& authentication.getPrincipal() != null
|
100
|
&& authentication.getPrincipal() instanceof User){
|
101
|
return (User)authentication.getPrincipal();
|
102
|
}
|
103
|
return null;
|
104
|
}
|
105
|
|
106
|
public void logoutAll(){
|
107
|
SecurityContextHolder.clearContext();
|
108
|
notifyObservers();
|
109
|
}
|
110
|
|
111
|
/* (non-Javadoc)
|
112
|
* @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
|
113
|
*/
|
114
|
@Override
|
115
|
public void update(CdmDataChangeMap arg) {}
|
116
|
|
117
|
/* (non-Javadoc)
|
118
|
* @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
|
119
|
*/
|
120
|
@Override
|
121
|
public ConversationHolder getConversationHolder() {
|
122
|
if(conversation == null){
|
123
|
conversation = CdmStore.createConversation();
|
124
|
}
|
125
|
return conversation;
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* Whether the current user has the role admin
|
130
|
*
|
131
|
* @return
|
132
|
*/
|
133
|
public boolean isAdmin() {
|
134
|
// FIXME until we have rights implemented properly we do this
|
135
|
// by a simple string check. This has to change
|
136
|
|
137
|
return "admin".equals(getAuthenticatedUser().getUsername());
|
138
|
}
|
139
|
|
140
|
@Override
|
141
|
public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
|
142
|
|
143
|
}
|
144
|
|
145
|
@Override
|
146
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
147
|
|
148
|
}
|
149
|
|
150
|
@Override
|
151
|
public void contextStart(IMemento memento, IProgressMonitor monitor){
|
152
|
conversation = CdmStore.createConversation();
|
153
|
}
|
154
|
|
155
|
@Override
|
156
|
public void contextRefresh(IProgressMonitor monitor) {
|
157
|
conversation = CdmStore.createConversation();
|
158
|
}
|
159
|
|
160
|
@Override
|
161
|
public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
|
162
|
|
163
|
}
|
164
|
}
|