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
.springframework
.security
.authentication
.BadCredentialsException
;
16 import org
.springframework
.security
.authentication
.LockedException
;
17 import org
.springframework
.security
.authentication
.UsernamePasswordAuthenticationToken
;
18 import org
.springframework
.security
.core
.Authentication
;
19 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
21 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
22 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
23 import eu
.etaxonomy
.cdm
.model
.common
.User
;
24 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
27 * <p>LoginManager class.</p>
33 public class LoginManager
extends Observable
implements IConversationEnabled
{
35 private ConversationHolder conversation
;
40 * @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
42 public void authenticate(String username
, String password
){
44 getConversationHolder().bind();
45 UsernamePasswordAuthenticationToken token
= new UsernamePasswordAuthenticationToken(username
, password
);
47 SecurityContextHolder
.clearContext();
48 Authentication authentication
= CdmStore
.getAuthenticationManager().authenticate(token
);
49 SecurityContextHolder
.getContext().setAuthentication(authentication
);
51 catch(BadCredentialsException e
){
52 StoreUtil
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
54 catch(LockedException e
){
55 StoreUtil
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
57 catch(IllegalArgumentException e
){
58 StoreUtil
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
62 this.notifyObservers();
67 * <p>getAuthenticatedUser</p>
69 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
71 public User
getAuthenticatedUser(){
72 Authentication authentication
= SecurityContextHolder
.getContext().getAuthentication();
74 if(authentication
!= null
75 && authentication
.getPrincipal() != null
76 && authentication
.getPrincipal() instanceof User
){
77 return (User
)authentication
.getPrincipal();
82 public void logoutAll(){
83 SecurityContextHolder
.clearContext();
88 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
91 public void update(CdmDataChangeMap arg
) {}
94 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
97 public ConversationHolder
getConversationHolder() {
98 if(conversation
== null){
99 conversation
= CdmStore
.createConversation();