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
.application
.CdmApplicationState
;
27 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
28 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
29 import eu
.etaxonomy
.cdm
.model
.common
.Group
;
30 import eu
.etaxonomy
.cdm
.model
.common
.User
;
31 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
32 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
33 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
34 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
37 * <p>LoginManager class.</p>
43 public class LoginManager
extends Observable
implements IConversationEnabled
, IContextListener
{
45 public static final Logger logger
= Logger
.getLogger(LoginManager
.class);
47 private ConversationHolder conversation
;
49 public static final String INCORRECT_CREDENTIALS_MESSAGE
= "Login and/or Password incorrect";
50 public static final String ACCOUNT_LOCKED_MESSAGE
= "Account is locked";
51 public static final String EMPTY_CREDENTIALS_MESSAGE
= "Login and/or Password empty";
53 public LoginManager(){
54 CdmStore
.getContextManager().addContextListener(this);
60 * @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
61 * @return true if the login attempt was successful even if the authentication has changed or not
63 public boolean authenticate(String username
, String password
){
65 // close all open editors
66 if(!AbstractUtility
.closeAll()){
72 doAuthenticate(username
, password
);
73 } catch (CdmAuthenticationException e
) {
74 MessagingUtils
.warningDialog("Could not authenticate", this, e
.getMessage());
80 public void doAuthenticate(String username
, String password
) throws CdmAuthenticationException
{
82 SecurityContextHolder
.clearContext();
83 Authentication lastAuthentication
= CdmStore
.getCurrentAuthentiation();
85 UsernamePasswordAuthenticationToken token
= new UsernamePasswordAuthenticationToken(username
, password
);
86 Authentication authentication
= CdmStore
.getAuthenticationManager().authenticate(token
);
88 User user
= (User
) authentication
.getPrincipal();
89 /* circumventing problem with hibernate not refreshing the transient collection authorities in this case,
90 * see http://dev.e-taxonomy.eu/trac/ticket/4053 */
91 user
.initAuthorities();
93 if(logger
.isDebugEnabled()){
94 StringBuilder gaText
= new StringBuilder();
96 Set
<GrantedAuthority
> gaSet
= user
.getGrantedAuthorities();
97 _logGrantedAuthotities(gaText
, indent
, gaSet
);
98 for(Group gr
: user
.getGroups()){
99 gaText
.append(indent
).append("gr[").append(gr
.hashCode()).append("] \"").append(gr
.getName()).append("\" ").append(gr
.toString()).append("\n");
100 _logGrantedAuthotities(gaText
, indent
+ indent
, gr
.getGrantedAuthorities());
102 logger
.debug("User authenticated: " + user
.getUsername() + "\n" + gaText
.toString());
105 authentication
= new UsernamePasswordAuthenticationToken(user
,password
, authentication
.getAuthorities());
106 SecurityContextHolder
.getContext().setAuthentication(authentication
);
107 CdmApplicationState
.setCurrentSecurityContext(SecurityContextHolder
.getContext());
109 if(!authentication
.equals(lastAuthentication
)){
111 this.notifyObservers();
113 } catch(BadCredentialsException e
){
114 throw new CdmAuthenticationException(INCORRECT_CREDENTIALS_MESSAGE
, e
);
115 } catch(LockedException e
){
116 throw new CdmAuthenticationException(ACCOUNT_LOCKED_MESSAGE
, e
);
117 } catch(IllegalArgumentException e
){
118 throw new CdmAuthenticationException(EMPTY_CREDENTIALS_MESSAGE
, e
);
124 private void _logGrantedAuthotities(StringBuilder gaText
, String indent
,
125 Set
<GrantedAuthority
> gaSet
) {
126 for(GrantedAuthority ga
: gaSet
){
127 gaText
.append(indent
).append("ga[").append(ga
.hashCode()).append("] ").append(ga
.toString()).append("\n");
132 * <p>getAuthenticatedUser</p>
134 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
136 public User
getAuthenticatedUser(){
137 Authentication authentication
= SecurityContextHolder
.getContext().getAuthentication();
139 if(authentication
!= null
140 && authentication
.getPrincipal() != null
141 && authentication
.getPrincipal() instanceof User
){
142 return (User
)authentication
.getPrincipal();
147 public void logoutAll(){
148 SecurityContextHolder
.clearContext();
153 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
156 public void update(CdmDataChangeMap arg
) {}
159 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
162 public ConversationHolder
getConversationHolder() {
163 if(conversation
== null){
164 conversation
= CdmStore
.createConversation();
170 * Whether the current user has the role admin
174 public boolean isAdmin() {
175 // FIXME until we have rights implemented properly we do this
176 // by a simple string check. This has to change
178 return "admin".equals(getAuthenticatedUser().getUsername());
182 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
187 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
192 public void contextStart(IMemento memento
, IProgressMonitor monitor
){
193 conversation
= CdmStore
.createConversation();
197 public void contextRefresh(IProgressMonitor monitor
) {
198 conversation
= CdmStore
.createConversation();
202 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {