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
;
15 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
16 import org
.eclipse
.ui
.IMemento
;
17 import org
.springframework
.security
.authentication
.BadCredentialsException
;
18 import org
.springframework
.security
.authentication
.LockedException
;
19 import org
.springframework
.security
.authentication
.UsernamePasswordAuthenticationToken
;
20 import org
.springframework
.security
.core
.Authentication
;
21 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
23 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
24 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
25 import eu
.etaxonomy
.cdm
.model
.common
.User
;
26 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
27 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
30 * <p>LoginManager class.</p>
36 public class LoginManager
extends Observable
implements IConversationEnabled
, IContextListener
{
38 private ConversationHolder conversation
;
40 public LoginManager(){
41 CdmStore
.getContextManager().addContextListener(this);
47 * @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
49 public boolean authenticate(String username
, String password
){
51 // close all open editors
52 if(!StoreUtil
.closeAll()){
58 getConversationHolder().bind();
60 SecurityContextHolder
.clearContext();
62 UsernamePasswordAuthenticationToken token
= new UsernamePasswordAuthenticationToken(username
, password
);
63 Authentication authentication
= CdmStore
.getAuthenticationManager().authenticate(token
);
65 SecurityContextHolder
.getContext().setAuthentication(authentication
);
68 this.notifyObservers();
71 catch(BadCredentialsException e
){
72 StoreUtil
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
74 catch(LockedException e
){
75 StoreUtil
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
77 catch(IllegalArgumentException e
){
78 StoreUtil
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
84 * <p>getAuthenticatedUser</p>
86 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
88 public User
getAuthenticatedUser(){
89 Authentication authentication
= SecurityContextHolder
.getContext().getAuthentication();
91 if(authentication
!= null
92 && authentication
.getPrincipal() != null
93 && authentication
.getPrincipal() instanceof User
){
94 return (User
)authentication
.getPrincipal();
99 public void logoutAll(){
100 SecurityContextHolder
.clearContext();
105 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
108 public void update(CdmDataChangeMap arg
) {}
111 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
114 public ConversationHolder
getConversationHolder() {
115 if(conversation
== null){
116 conversation
= CdmStore
.createConversation();
122 * Whether the current user has the role admin
126 public boolean isAdmin() {
127 // FIXME until we have rights implemented properly we do this
128 // by a simple string check. This has to change
130 return "admin".equals(getAuthenticatedUser().getUsername());
134 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
139 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
144 public void contextStart(IMemento memento
, IProgressMonitor monitor
){
145 conversation
= CdmStore
.createConversation();
149 public void contextRefresh(IProgressMonitor monitor
) {
150 conversation
= CdmStore
.createConversation();
154 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {