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
.model
.common
.User
;
24 * <p>LoginManager class.</p>
30 public class LoginManager
extends Observable
{
35 * @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
37 public void authenticate(String username
, String password
){
39 UsernamePasswordAuthenticationToken token
= new UsernamePasswordAuthenticationToken(username
, password
);
41 SecurityContextHolder
.clearContext();
42 Authentication authentication
= CdmStore
.getAuthenticationManager().authenticate(token
);
43 SecurityContextHolder
.getContext().setAuthentication(authentication
);
45 catch(BadCredentialsException e
){
46 StoreUtil
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
48 catch(LockedException e
){
49 StoreUtil
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
51 catch(IllegalArgumentException e
){
52 StoreUtil
.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
56 this.notifyObservers();
61 * <p>getAuthenticatedUser</p>
63 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
65 public User
getAuthenticatedUser(){
66 Authentication authentication
= SecurityContextHolder
.getContext().getAuthentication();
68 if(authentication
!= null
69 && authentication
.getPrincipal() != null
70 && authentication
.getPrincipal() instanceof User
){
71 return (User
)authentication
.getPrincipal();
76 public void logoutAll(){
77 SecurityContextHolder
.clearContext();