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
;
33 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
36 * <p>LoginManager class.</p>
42 public class LoginManager
extends Observable
implements IConversationEnabled
, IContextListener
{
44 public static final Logger logger
= Logger
.getLogger(LoginManager
.class);
46 private ConversationHolder conversation
;
48 public LoginManager(){
49 CdmStore
.getContextManager().addContextListener(this);
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
58 public boolean authenticate(String username
, String password
){
60 // close all open editors
61 if(!AbstractUtility
.closeAll()){
67 SecurityContextHolder
.clearContext();
69 Authentication lastAuthentication
= CdmStore
.getCurrentAuthentiation();
71 UsernamePasswordAuthenticationToken token
= new UsernamePasswordAuthenticationToken(username
, password
);
72 Authentication authentication
= CdmStore
.getAuthenticationManager().authenticate(token
);
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();
79 if(logger
.isDebugEnabled()){
80 StringBuilder gaText
= new StringBuilder();
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());
88 logger
.debug("User authenticated: " + user
.getUsername() + "\n" + gaText
.toString());
91 authentication
= new UsernamePasswordAuthenticationToken(username
,password
, authentication
.getAuthorities());
92 SecurityContextHolder
.getContext().setAuthentication(authentication
);
94 if(!authentication
.equals(lastAuthentication
)){
96 this.notifyObservers();
100 catch(BadCredentialsException e
){
101 MessagingUtils
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
103 catch(LockedException e
){
104 MessagingUtils
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
106 catch(IllegalArgumentException e
){
107 MessagingUtils
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
112 private void _logGrantedAuthotities(StringBuilder gaText
, String indent
,
113 Set
<GrantedAuthority
> gaSet
) {
114 for(GrantedAuthority ga
: gaSet
){
115 gaText
.append(indent
).append("ga[").append(ga
.hashCode()).append("] ").append(ga
.toString()).append("\n");
120 * <p>getAuthenticatedUser</p>
122 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
124 public User
getAuthenticatedUser(){
125 Authentication authentication
= SecurityContextHolder
.getContext().getAuthentication();
127 if(authentication
!= null
128 && authentication
.getPrincipal() != null
129 && authentication
.getPrincipal() instanceof User
){
130 return (User
)authentication
.getPrincipal();
135 public void logoutAll(){
136 SecurityContextHolder
.clearContext();
141 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
144 public void update(CdmDataChangeMap arg
) {}
147 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
150 public ConversationHolder
getConversationHolder() {
151 if(conversation
== null){
152 conversation
= CdmStore
.createConversation();
158 * Whether the current user has the role admin
162 public boolean isAdmin() {
163 // FIXME until we have rights implemented properly we do this
164 // by a simple string check. This has to change
166 return "admin".equals(getAuthenticatedUser().getUsername());
170 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
175 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
180 public void contextStart(IMemento memento
, IProgressMonitor monitor
){
181 conversation
= CdmStore
.createConversation();
185 public void contextRefresh(IProgressMonitor monitor
) {
186 conversation
= CdmStore
.createConversation();
190 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {