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