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
58 if(!StoreUtil
.closeAll()){
64 getConversationHolder().bind();
66 SecurityContextHolder
.clearContext();
68 UsernamePasswordAuthenticationToken token
= new UsernamePasswordAuthenticationToken(username
, password
);
69 Authentication authentication
= CdmStore
.getAuthenticationManager().authenticate(token
);
71 SecurityContextHolder
.getContext().setAuthentication(authentication
);
74 this.notifyObservers();
77 catch(BadCredentialsException e
){
78 StoreUtil
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
80 catch(LockedException e
){
81 StoreUtil
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
83 catch(IllegalArgumentException e
){
84 StoreUtil
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
90 * <p>getAuthenticatedUser</p>
92 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
94 public User
getAuthenticatedUser(){
95 Authentication authentication
= SecurityContextHolder
.getContext().getAuthentication();
97 if(authentication
!= null
98 && authentication
.getPrincipal() != null
99 && authentication
.getPrincipal() instanceof User
){
100 return (User
)authentication
.getPrincipal();
105 public void logoutAll(){
106 SecurityContextHolder
.clearContext();
111 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
114 public void update(CdmDataChangeMap arg
) {}
117 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
120 public ConversationHolder
getConversationHolder() {
121 if(conversation
== null){
122 conversation
= CdmStore
.createConversation();
128 * Whether the current user has the role admin
132 public boolean isAdmin() {
133 // FIXME until we have rights implemented properly we do this
134 // by a simple string check. This has to change
136 return "admin".equals(getAuthenticatedUser().getUsername());
140 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
145 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
150 public void contextStart(IMemento memento
, IProgressMonitor monitor
){
151 conversation
= CdmStore
.createConversation();
155 public void contextRefresh(IProgressMonitor monitor
) {
156 conversation
= CdmStore
.createConversation();
160 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {