2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.store
;
12 import java
.util
.Observable
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
17 import org
.eclipse
.ui
.IMemento
;
18 import org
.springframework
.security
.authentication
.BadCredentialsException
;
19 import org
.springframework
.security
.authentication
.LockedException
;
20 import org
.springframework
.security
.authentication
.UsernamePasswordAuthenticationToken
;
21 import org
.springframework
.security
.core
.Authentication
;
22 import org
.springframework
.security
.core
.GrantedAuthority
;
23 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
25 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
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
.IContextListener
;
32 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
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 static final String INCORRECT_CREDENTIALS_MESSAGE
= "Login and/or Password incorrect";
48 public static final String ACCOUNT_LOCKED_MESSAGE
= "Account is locked";
49 public static final String EMPTY_CREDENTIALS_MESSAGE
= "Login and/or Password empty";
51 public LoginManager(){
52 CdmStore
.getContextManager().addContextListener(this);
58 * @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
59 * @return true if the login attempt was successful even if the authentication has changed or not
61 public boolean authenticate(String username
, String password
){
63 doAuthenticate(username
, password
);
64 } catch (CdmAuthenticationException e
) {
65 MessagingUtils
.warningDialog("Could not authenticate", this, e
.getMessage());
71 public void doAuthenticate(String username
, String password
) throws CdmAuthenticationException
{
73 SecurityContextHolder
.clearContext();
74 Authentication lastAuthentication
= CdmStore
.getCurrentAuthentiation();
76 UsernamePasswordAuthenticationToken token
= new UsernamePasswordAuthenticationToken(username
, password
);
77 Authentication authentication
= CdmStore
.getAuthenticationManager().authenticate(token
);
79 User user
= (User
) authentication
.getPrincipal();
80 /* circumventing problem with hibernate not refreshing the transient collection authorities in this case,
81 * see http://dev.e-taxonomy.eu/trac/ticket/4053 */
82 user
.initAuthorities();
84 if(logger
.isDebugEnabled()){
85 StringBuilder gaText
= new StringBuilder();
87 Set
<GrantedAuthority
> gaSet
= user
.getGrantedAuthorities();
88 _logGrantedAuthotities(gaText
, indent
, gaSet
);
89 for(Group gr
: user
.getGroups()){
90 gaText
.append(indent
).append("gr[").append(gr
.hashCode()).append("] \"").append(gr
.getName()).append("\" ").append(gr
.toString()).append("\n");
91 _logGrantedAuthotities(gaText
, indent
+ indent
, gr
.getGrantedAuthorities());
93 logger
.debug("User authenticated: " + user
.getUsername() + "\n" + gaText
.toString());
96 authentication
= new UsernamePasswordAuthenticationToken(user
,password
, authentication
.getAuthorities());
97 SecurityContextHolder
.getContext().setAuthentication(authentication
);
98 CdmApplicationState
.setCurrentSecurityContext(SecurityContextHolder
.getContext());
100 if(!authentication
.equals(lastAuthentication
)){
102 this.notifyObservers();
104 } catch(BadCredentialsException e
){
105 throw new CdmAuthenticationException(INCORRECT_CREDENTIALS_MESSAGE
, e
);
106 } catch(LockedException e
){
107 throw new CdmAuthenticationException(ACCOUNT_LOCKED_MESSAGE
, e
);
108 } catch(IllegalArgumentException e
){
109 throw new CdmAuthenticationException(EMPTY_CREDENTIALS_MESSAGE
, e
);
115 private void _logGrantedAuthotities(StringBuilder gaText
, String indent
,
116 Set
<GrantedAuthority
> gaSet
) {
117 for(GrantedAuthority ga
: gaSet
){
118 gaText
.append(indent
).append("ga[").append(ga
.hashCode()).append("] ").append(ga
.toString()).append("\n");
123 * <p>getAuthenticatedUser</p>
125 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
127 public User
getAuthenticatedUser(){
128 Authentication authentication
= SecurityContextHolder
.getContext().getAuthentication();
130 if(authentication
!= null
131 && authentication
.getPrincipal() != null
132 && authentication
.getPrincipal() instanceof User
){
133 return (User
)authentication
.getPrincipal();
138 public void logoutAll(){
139 SecurityContextHolder
.clearContext();
144 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
147 public void update(CdmDataChangeMap arg
) {}
150 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
153 public ConversationHolder
getConversationHolder() {
154 if(conversation
== null){
155 conversation
= CdmStore
.createConversation();
161 * Whether the current user has the role admin
165 public boolean isAdmin() {
166 // FIXME until we have rights implemented properly we do this
167 // by a simple string check. This has to change
169 return "admin".equals(getAuthenticatedUser().getUsername());
173 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
178 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
183 public void contextStart(IMemento memento
, IProgressMonitor monitor
){
184 conversation
= CdmStore
.createConversation();
188 public void contextRefresh(IProgressMonitor monitor
) {
189 conversation
= CdmStore
.createConversation();
193 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {