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
.IEditorPart
;
17 import org
.eclipse
.ui
.IMemento
;
18 import org
.eclipse
.ui
.IWorkbenchPage
;
19 import org
.eclipse
.ui
.IWorkbenchPart
;
20 import org
.eclipse
.ui
.IWorkbenchWindow
;
21 import org
.eclipse
.ui
.editors
.text
.EditorsUI
;
22 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
23 import org
.springframework
.security
.authentication
.BadCredentialsException
;
24 import org
.springframework
.security
.authentication
.LockedException
;
25 import org
.springframework
.security
.authentication
.UsernamePasswordAuthenticationToken
;
26 import org
.springframework
.security
.core
.Authentication
;
27 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
29 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
30 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
31 import eu
.etaxonomy
.cdm
.model
.common
.User
;
32 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
33 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
36 * <p>LoginManager class.</p>
42 public class LoginManager
extends Observable
implements IConversationEnabled
, IContextListener
{
44 private ConversationHolder conversation
;
46 public LoginManager(){
47 CdmStore
.getContextManager().addContextListener(this);
53 * @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
55 public boolean authenticate(String username
, String password
){
57 // close all open editors, this will close all open conversations
58 // otherwise the users granted authorities might not be up to date
59 if(!StoreUtil
.closeAll()){
65 getConversationHolder().bind();
67 SecurityContextHolder
.clearContext();
69 UsernamePasswordAuthenticationToken token
= new UsernamePasswordAuthenticationToken(username
, password
);
70 Authentication authentication
= CdmStore
.getAuthenticationManager().authenticate(token
);
72 SecurityContextHolder
.getContext().setAuthentication(authentication
);
75 this.notifyObservers();
78 catch(BadCredentialsException e
){
79 StoreUtil
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
81 catch(LockedException e
){
82 StoreUtil
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
84 catch(IllegalArgumentException e
){
85 StoreUtil
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
91 * <p>getAuthenticatedUser</p>
93 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
95 public User
getAuthenticatedUser(){
96 Authentication authentication
= SecurityContextHolder
.getContext().getAuthentication();
98 if(authentication
!= null
99 && authentication
.getPrincipal() != null
100 && authentication
.getPrincipal() instanceof User
){
101 return (User
)authentication
.getPrincipal();
106 public void logoutAll(){
107 SecurityContextHolder
.clearContext();
112 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
115 public void update(CdmDataChangeMap arg
) {}
118 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
121 public ConversationHolder
getConversationHolder() {
122 if(conversation
== null){
123 conversation
= CdmStore
.createConversation();
129 * Whether the current user has the role admin
133 public boolean isAdmin() {
134 // FIXME until we have rights implemented properly we do this
135 // by a simple string check. This has to change
137 return "admin".equals(getAuthenticatedUser().getUsername());
141 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
146 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
151 public void contextStart(IMemento memento
, IProgressMonitor monitor
){
152 conversation
= CdmStore
.createConversation();
156 public void contextRefresh(IProgressMonitor monitor
) {
157 conversation
= CdmStore
.createConversation();
161 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {