part 2 of fixing #2414 (Group updating doesn't work) by committing and restarting...
[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.conversation.ConversationHolder;
27 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
28 import eu.etaxonomy.cdm.model.common.Group;
29 import eu.etaxonomy.cdm.model.common.User;
30 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
31 import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
32 import eu.etaxonomy.taxeditor.model.IContextListener;
33
34 /**
35 * <p>LoginManager class.</p>
36 *
37 * @author n.hoffmann
38 * @created 03.07.2009
39 * @version 1.0
40 */
41 public class LoginManager extends Observable implements IConversationEnabled, IContextListener{
42
43 public static final Logger logger = Logger.getLogger(LoginManager.class);
44
45 private ConversationHolder conversation;
46
47 public LoginManager(){
48 CdmStore.getContextManager().addContextListener(this);
49 }
50
51 /**
52 * <p>authenticate</p>
53 *
54 * @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
55 */
56 public boolean authenticate(String username, String password){
57
58 // close all open editors
59 if(!StoreUtil.closeAll()){
60 return false;
61 }
62
63
64 try{
65 getConversationHolder().bind();
66 getConversationHolder().commit();
67
68 SecurityContextHolder.clearContext();
69
70 UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
71 Authentication authentication = CdmStore.getAuthenticationManager().authenticate(token);
72
73 if(logger.isDebugEnabled()){
74 User user = (User) authentication.getPrincipal();
75 StringBuilder gaText = new StringBuilder();
76 String indent = " ";
77 Set<GrantedAuthority> gaSet = user.getGrantedAuthorities();
78 _logGrantedAuthotities(gaText, indent, gaSet);
79 for(Group gr : user.getGroups()){
80 gaText.append(indent).append("gr[").append(gr.hashCode()).append("] ").append(gr.getName()).append(gr.toString()).append("\n");
81 _logGrantedAuthotities(gaText, indent + indent, gr.getGrantedAuthorities());
82 }
83 logger.debug("User authenticated: " + user.getUsername() + "\n" + gaText.toString());
84 }
85
86 SecurityContextHolder.getContext().setAuthentication(authentication);
87
88 this.setChanged();
89 this.notifyObservers();
90 return true;
91 }
92 catch(BadCredentialsException e){
93 StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
94 }
95 catch(LockedException e){
96 StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
97 }
98 catch(IllegalArgumentException e){
99 StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
100 }
101 return false;
102 }
103
104 private void _logGrantedAuthotities(StringBuilder gaText, String indent,
105 Set<GrantedAuthority> gaSet) {
106 for(GrantedAuthority ga : gaSet){
107 gaText.append(indent).append("ga[").append(ga.hashCode()).append("] ").append(ga.toString()).append("\n");
108 }
109 }
110
111 /**
112 * <p>getAuthenticatedUser</p>
113 *
114 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
115 */
116 public User getAuthenticatedUser(){
117 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
118
119 if(authentication != null
120 && authentication.getPrincipal() != null
121 && authentication.getPrincipal() instanceof User){
122 return (User)authentication.getPrincipal();
123 }
124 return null;
125 }
126
127 public void logoutAll(){
128 SecurityContextHolder.clearContext();
129 notifyObservers();
130 }
131
132 /* (non-Javadoc)
133 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
134 */
135 @Override
136 public void update(CdmDataChangeMap arg) {}
137
138 /* (non-Javadoc)
139 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
140 */
141 @Override
142 public ConversationHolder getConversationHolder() {
143 if(conversation == null){
144 conversation = CdmStore.createConversation();
145 }
146 return conversation;
147 }
148
149 /**
150 * Whether the current user has the role admin
151 *
152 * @return
153 */
154 public boolean isAdmin() {
155 // FIXME until we have rights implemented properly we do this
156 // by a simple string check. This has to change
157
158 return "admin".equals(getAuthenticatedUser().getUsername());
159 }
160
161 @Override
162 public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
163
164 }
165
166 @Override
167 public void contextStop(IMemento memento, IProgressMonitor monitor) {
168
169 }
170
171 @Override
172 public void contextStart(IMemento memento, IProgressMonitor monitor){
173 conversation = CdmStore.createConversation();
174 }
175
176 @Override
177 public void contextRefresh(IProgressMonitor monitor) {
178 conversation = CdmStore.createConversation();
179 }
180
181 @Override
182 public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
183
184 }
185 }