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 SecurityContextHolder
.getContext().setAuthentication(authentication
);
93 if(!authentication
.equals(lastAuthentication
)){
95 this.notifyObservers();
99 catch(BadCredentialsException e
){
100 MessagingUtils
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
102 catch(LockedException e
){
103 MessagingUtils
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
105 catch(IllegalArgumentException e
){
106 MessagingUtils
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
111 private void _logGrantedAuthotities(StringBuilder gaText
, String indent
,
112 Set
<GrantedAuthority
> gaSet
) {
113 for(GrantedAuthority ga
: gaSet
){
114 gaText
.append(indent
).append("ga[").append(ga
.hashCode()).append("] ").append(ga
.toString()).append("\n");
119 * <p>getAuthenticatedUser</p>
121 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
123 public User
getAuthenticatedUser(){
124 Authentication authentication
= SecurityContextHolder
.getContext().getAuthentication();
126 if(authentication
!= null
127 && authentication
.getPrincipal() != null
128 && authentication
.getPrincipal() instanceof User
){
129 return (User
)authentication
.getPrincipal();
134 public void logoutAll(){
135 SecurityContextHolder
.clearContext();
140 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
143 public void update(CdmDataChangeMap arg
) {}
146 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
149 public ConversationHolder
getConversationHolder() {
150 if(conversation
== null){
151 conversation
= CdmStore
.createConversation();
157 * Whether the current user has the role admin
161 public boolean isAdmin() {
162 // FIXME until we have rights implemented properly we do this
163 // by a simple string check. This has to change
165 return "admin".equals(getAuthenticatedUser().getUsername());
169 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
174 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
179 public void contextStart(IMemento memento
, IProgressMonitor monitor
){
180 conversation
= CdmStore
.createConversation();
184 public void contextRefresh(IProgressMonitor monitor
) {
185 conversation
= CdmStore
.createConversation();
189 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {