3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.store
;
13 import java
.util
.Observable
;
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
;
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
;
35 * <p>LoginManager class.</p>
41 public class LoginManager
extends Observable
implements IConversationEnabled
, IContextListener
{
43 public static final Logger logger
= Logger
.getLogger(LoginManager
.class);
45 private ConversationHolder conversation
;
47 public LoginManager(){
48 CdmStore
.getContextManager().addContextListener(this);
54 * @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
55 * @return true if the login attempt was successful even if the authentication has changed or not
57 public boolean authenticate(String username
, String password
){
59 // close all open editors
60 if(!AbstractUtility
.closeAll()){
66 SecurityContextHolder
.clearContext();
68 Authentication lastAuthentication
= CdmStore
.getCurrentAuthentiation();
70 UsernamePasswordAuthenticationToken token
= new UsernamePasswordAuthenticationToken(username
, password
);
71 Authentication authentication
= CdmStore
.getAuthenticationManager().authenticate(token
);
73 User user
= (User
) authentication
.getPrincipal();
74 /* circumventing problem with hibernate not refreshing the transient collection authorities in this case,
75 * see http://dev.e-taxonomy.eu/trac/ticket/4053 */
76 user
.initAuthorities();
78 if(logger
.isDebugEnabled()){
79 StringBuilder gaText
= new StringBuilder();
81 Set
<GrantedAuthority
> gaSet
= user
.getGrantedAuthorities();
82 _logGrantedAuthotities(gaText
, indent
, gaSet
);
83 for(Group gr
: user
.getGroups()){
84 gaText
.append(indent
).append("gr[").append(gr
.hashCode()).append("] \"").append(gr
.getName()).append("\" ").append(gr
.toString()).append("\n");
85 _logGrantedAuthotities(gaText
, indent
+ indent
, gr
.getGrantedAuthorities());
87 logger
.debug("User authenticated: " + user
.getUsername() + "\n" + gaText
.toString());
90 SecurityContextHolder
.getContext().setAuthentication(authentication
);
92 if(!authentication
.equals(lastAuthentication
)){
94 this.notifyObservers();
98 catch(BadCredentialsException e
){
99 AbstractUtility
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
101 catch(LockedException e
){
102 AbstractUtility
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
104 catch(IllegalArgumentException e
){
105 AbstractUtility
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
110 private void _logGrantedAuthotities(StringBuilder gaText
, String indent
,
111 Set
<GrantedAuthority
> gaSet
) {
112 for(GrantedAuthority ga
: gaSet
){
113 gaText
.append(indent
).append("ga[").append(ga
.hashCode()).append("] ").append(ga
.toString()).append("\n");
118 * <p>getAuthenticatedUser</p>
120 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
122 public User
getAuthenticatedUser(){
123 Authentication authentication
= SecurityContextHolder
.getContext().getAuthentication();
125 if(authentication
!= null
126 && authentication
.getPrincipal() != null
127 && authentication
.getPrincipal() instanceof User
){
128 return (User
)authentication
.getPrincipal();
133 public void logoutAll(){
134 SecurityContextHolder
.clearContext();
139 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
142 public void update(CdmDataChangeMap arg
) {}
145 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
148 public ConversationHolder
getConversationHolder() {
149 if(conversation
== null){
150 conversation
= CdmStore
.createConversation();
156 * Whether the current user has the role admin
160 public boolean isAdmin() {
161 // FIXME until we have rights implemented properly we do this
162 // by a simple string check. This has to change
164 return "admin".equals(getAuthenticatedUser().getUsername());
168 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
173 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
178 public void contextStart(IMemento memento
, IProgressMonitor monitor
){
179 conversation
= CdmStore
.createConversation();
183 public void contextRefresh(IProgressMonitor monitor
) {
184 conversation
= CdmStore
.createConversation();
188 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {