list specimen-taxon associations separately in specimen details view
[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 import java.util.Set;
15
16 import org.apache.log4j.Logger;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.ui.IMemento;
19 import org.springframework.security.authentication.BadCredentialsException;
20 import org.springframework.security.authentication.LockedException;
21 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
22 import org.springframework.security.core.Authentication;
23 import org.springframework.security.core.GrantedAuthority;
24 import org.springframework.security.core.context.SecurityContextHolder;
25
26 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
27 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
28 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
29 import eu.etaxonomy.cdm.model.common.Group;
30 import eu.etaxonomy.cdm.model.common.User;
31 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
32 import eu.etaxonomy.taxeditor.model.AbstractUtility;
33 import eu.etaxonomy.taxeditor.model.IContextListener;
34 import eu.etaxonomy.taxeditor.model.MessagingUtils;
35
36 /**
37 * <p>LoginManager class.</p>
38 *
39 * @author n.hoffmann
40 * @created 03.07.2009
41 * @version 1.0
42 */
43 public class LoginManager extends Observable implements IConversationEnabled, IContextListener{
44
45 public static final Logger logger = Logger.getLogger(LoginManager.class);
46
47 private ConversationHolder conversation;
48
49 public LoginManager(){
50 CdmStore.getContextManager().addContextListener(this);
51 }
52
53 /**
54 * <p>authenticate</p>
55 *
56 * @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
57 * @return true if the login attempt was successful even if the authentication has changed or not
58 */
59 public boolean authenticate(String username, String password){
60
61 // close all open editors
62 if(!AbstractUtility.closeAll()){
63 return false;
64 }
65
66
67 try{
68 doAuthenticate(username, password);
69 } catch (CdmAuthenticationException e) {
70 MessagingUtils.warningDialog("Could not authenticate", this, e.getMessage());
71 return false;
72 }
73 return true;
74 }
75
76 public void doAuthenticate(String username, String password) throws CdmAuthenticationException {
77 try {
78 SecurityContextHolder.clearContext();
79 Authentication lastAuthentication = CdmStore.getCurrentAuthentiation();
80
81 UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
82 Authentication authentication = CdmStore.getAuthenticationManager().authenticate(token);
83
84 User user = (User) authentication.getPrincipal();
85 /* circumventing problem with hibernate not refreshing the transient collection authorities in this case,
86 * see http://dev.e-taxonomy.eu/trac/ticket/4053 */
87 user.initAuthorities();
88
89 if(logger.isDebugEnabled()){
90 StringBuilder gaText = new StringBuilder();
91 String indent = " ";
92 Set<GrantedAuthority> gaSet = user.getGrantedAuthorities();
93 _logGrantedAuthotities(gaText, indent, gaSet);
94 for(Group gr : user.getGroups()){
95 gaText.append(indent).append("gr[").append(gr.hashCode()).append("] \"").append(gr.getName()).append("\" ").append(gr.toString()).append("\n");
96 _logGrantedAuthotities(gaText, indent + indent, gr.getGrantedAuthorities());
97 }
98 logger.debug("User authenticated: " + user.getUsername() + "\n" + gaText.toString());
99 }
100
101 authentication = new UsernamePasswordAuthenticationToken(user,password, authentication.getAuthorities());
102 SecurityContextHolder.getContext().setAuthentication(authentication);
103 CdmApplicationState.setCurrentSecurityContext(SecurityContextHolder.getContext());
104
105 if(!authentication.equals(lastAuthentication)){
106 this.setChanged();
107 this.notifyObservers();
108 }
109 } catch(BadCredentialsException e){
110 throw new CdmAuthenticationException("Login and/or Password incorrect", e);
111 } catch(LockedException e){
112 throw new CdmAuthenticationException("Account is locked", e);
113 } catch(IllegalArgumentException e){
114 throw new CdmAuthenticationException("Login and/or Password empty", e);
115 }
116
117 }
118
119 private void _logGrantedAuthotities(StringBuilder gaText, String indent,
120 Set<GrantedAuthority> gaSet) {
121 for(GrantedAuthority ga : gaSet){
122 gaText.append(indent).append("ga[").append(ga.hashCode()).append("] ").append(ga.toString()).append("\n");
123 }
124 }
125
126 /**
127 * <p>getAuthenticatedUser</p>
128 *
129 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
130 */
131 public User getAuthenticatedUser(){
132 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
133
134 if(authentication != null
135 && authentication.getPrincipal() != null
136 && authentication.getPrincipal() instanceof User){
137 return (User)authentication.getPrincipal();
138 }
139 return null;
140 }
141
142 public void logoutAll(){
143 SecurityContextHolder.clearContext();
144 notifyObservers();
145 }
146
147 /* (non-Javadoc)
148 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
149 */
150 @Override
151 public void update(CdmDataChangeMap arg) {}
152
153 /* (non-Javadoc)
154 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
155 */
156 @Override
157 public ConversationHolder getConversationHolder() {
158 if(conversation == null){
159 conversation = CdmStore.createConversation();
160 }
161 return conversation;
162 }
163
164 /**
165 * Whether the current user has the role admin
166 *
167 * @return
168 */
169 public boolean isAdmin() {
170 // FIXME until we have rights implemented properly we do this
171 // by a simple string check. This has to change
172
173 return "admin".equals(getAuthenticatedUser().getUsername());
174 }
175
176 @Override
177 public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
178
179 }
180
181 @Override
182 public void contextStop(IMemento memento, IProgressMonitor monitor) {
183
184 }
185
186 @Override
187 public void contextStart(IMemento memento, IProgressMonitor monitor){
188 conversation = CdmStore.createConversation();
189 }
190
191 @Override
192 public void contextRefresh(IProgressMonitor monitor) {
193 conversation = CdmStore.createConversation();
194 }
195
196 @Override
197 public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
198
199 }
200 }