42c6e74930cf78578a5035d6f33747be26b750c0
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / store / LoginManager.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.store;
12
13 import java.util.Observable;
14
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;
22
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;
28
29 /**
30 * <p>LoginManager class.</p>
31 *
32 * @author n.hoffmann
33 * @created 03.07.2009
34 * @version 1.0
35 */
36 public class LoginManager extends Observable implements IConversationEnabled, IContextListener{
37
38 private ConversationHolder conversation;
39
40 public LoginManager(){
41 CdmStore.getContextManager().addContextListener(this);
42 }
43
44 /**
45 * <p>authenticate</p>
46 *
47 * @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
48 */
49 public boolean authenticate(String username, String password){
50
51 // close all open editors
52 if(!StoreUtil.closeAll()){
53 return false;
54 }
55
56
57 try{
58 getConversationHolder().bind();
59
60 SecurityContextHolder.clearContext();
61
62 UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
63 Authentication authentication = CdmStore.getAuthenticationManager().authenticate(token);
64
65 SecurityContextHolder.getContext().setAuthentication(authentication);
66
67 this.setChanged();
68 this.notifyObservers();
69 return true;
70 }
71 catch(BadCredentialsException e){
72 StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
73 }
74 catch(LockedException e){
75 StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
76 }
77 catch(IllegalArgumentException e){
78 StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
79 }
80 return false;
81 }
82
83 /**
84 * <p>getAuthenticatedUser</p>
85 *
86 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
87 */
88 public User getAuthenticatedUser(){
89 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
90
91 if(authentication != null
92 && authentication.getPrincipal() != null
93 && authentication.getPrincipal() instanceof User){
94 return (User)authentication.getPrincipal();
95 }
96 return null;
97 }
98
99 public void logoutAll(){
100 SecurityContextHolder.clearContext();
101 notifyObservers();
102 }
103
104 /* (non-Javadoc)
105 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
106 */
107 @Override
108 public void update(CdmDataChangeMap arg) {}
109
110 /* (non-Javadoc)
111 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
112 */
113 @Override
114 public ConversationHolder getConversationHolder() {
115 if(conversation == null){
116 conversation = CdmStore.createConversation();
117 }
118 return conversation;
119 }
120
121 /**
122 * Whether the current user has the role admin
123 *
124 * @return
125 */
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
129
130 return "admin".equals(getAuthenticatedUser().getUsername());
131 }
132
133 @Override
134 public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
135
136 }
137
138 @Override
139 public void contextStop(IMemento memento, IProgressMonitor monitor) {
140
141 }
142
143 @Override
144 public void contextStart(IMemento memento, IProgressMonitor monitor){
145 conversation = CdmStore.createConversation();
146 }
147
148 @Override
149 public void contextRefresh(IProgressMonitor monitor) {
150 conversation = CdmStore.createConversation();
151 }
152
153 @Override
154 public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
155
156 }
157 }