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
.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 static final String INCORRECT_CREDENTIALS_MESSAGE
= "Login and/or Password incorrect";
49 public static final String ACCOUNT_LOCKED_MESSAGE
= "Account is locked";
50 public static final String EMPTY_CREDENTIALS_MESSAGE
= "Login and/or Password empty";
52 public LoginManager(){
53 CdmStore
.getContextManager().addContextListener(this);
59 * @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
60 * @return true if the login attempt was successful even if the authentication has changed or not
62 public boolean authenticate(String username
, String password
){
64 // close all open editors
65 if(!AbstractUtility
.closeAll()){
71 doAuthenticate(username
, password
);
72 } catch (CdmAuthenticationException e
) {
73 MessagingUtils
.warningDialog("Could not authenticate", this, e
.getMessage());
79 public void doAuthenticate(String username
, String password
) throws CdmAuthenticationException
{
81 SecurityContextHolder
.clearContext();
82 Authentication lastAuthentication
= CdmStore
.getCurrentAuthentiation();
84 UsernamePasswordAuthenticationToken token
= new UsernamePasswordAuthenticationToken(username
, password
);
85 Authentication authentication
= CdmStore
.getAuthenticationManager().authenticate(token
);
87 User user
= (User
) authentication
.getPrincipal();
88 /* circumventing problem with hibernate not refreshing the transient collection authorities in this case,
89 * see http://dev.e-taxonomy.eu/trac/ticket/4053 */
90 user
.initAuthorities();
92 if(logger
.isDebugEnabled()){
93 StringBuilder gaText
= new StringBuilder();
95 Set
<GrantedAuthority
> gaSet
= user
.getGrantedAuthorities();
96 _logGrantedAuthotities(gaText
, indent
, gaSet
);
97 for(Group gr
: user
.getGroups()){
98 gaText
.append(indent
).append("gr[").append(gr
.hashCode()).append("] \"").append(gr
.getName()).append("\" ").append(gr
.toString()).append("\n");
99 _logGrantedAuthotities(gaText
, indent
+ indent
, gr
.getGrantedAuthorities());
101 logger
.debug("User authenticated: " + user
.getUsername() + "\n" + gaText
.toString());
104 authentication
= new UsernamePasswordAuthenticationToken(user
,password
, authentication
.getAuthorities());
105 SecurityContextHolder
.getContext().setAuthentication(authentication
);
106 CdmApplicationState
.setCurrentSecurityContext(SecurityContextHolder
.getContext());
108 if(!authentication
.equals(lastAuthentication
)){
110 this.notifyObservers();
112 } catch(BadCredentialsException e
){
113 throw new CdmAuthenticationException(INCORRECT_CREDENTIALS_MESSAGE
, e
);
114 } catch(LockedException e
){
115 throw new CdmAuthenticationException(ACCOUNT_LOCKED_MESSAGE
, e
);
116 } catch(IllegalArgumentException e
){
117 throw new CdmAuthenticationException(EMPTY_CREDENTIALS_MESSAGE
, e
);
123 private void _logGrantedAuthotities(StringBuilder gaText
, String indent
,
124 Set
<GrantedAuthority
> gaSet
) {
125 for(GrantedAuthority ga
: gaSet
){
126 gaText
.append(indent
).append("ga[").append(ga
.hashCode()).append("] ").append(ga
.toString()).append("\n");
131 * <p>getAuthenticatedUser</p>
133 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
135 public User
getAuthenticatedUser(){
136 Authentication authentication
= SecurityContextHolder
.getContext().getAuthentication();
138 if(authentication
!= null
139 && authentication
.getPrincipal() != null
140 && authentication
.getPrincipal() instanceof User
){
141 return (User
)authentication
.getPrincipal();
146 public void logoutAll(){
147 SecurityContextHolder
.clearContext();
152 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
155 public void update(CdmDataChangeMap arg
) {}
158 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
161 public ConversationHolder
getConversationHolder() {
162 if(conversation
== null){
163 conversation
= CdmStore
.createConversation();
169 * Whether the current user has the role admin
173 public boolean isAdmin() {
174 // FIXME until we have rights implemented properly we do this
175 // by a simple string check. This has to change
177 return "admin".equals(getAuthenticatedUser().getUsername());
181 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
186 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
191 public void contextStart(IMemento memento
, IProgressMonitor monitor
){
192 conversation
= CdmStore
.createConversation();
196 public void contextRefresh(IProgressMonitor monitor
) {
197 conversation
= CdmStore
.createConversation();
201 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {