Project

General

Profile

Revision b92f1605

IDb92f16053635888d588d64427c1c38027b7f1742
Parent 1c29bd52
Child d9d04b04

Added by Andreas Kohlbecker over 5 years ago

no longer using a conversation for the login dialog and thus fixing problems after putting wrong credentials

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/store/LoginManager.java
1 1
// $Id$
2 2
/**
3 3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
4
* European Distributed Institute of Taxonomy
5 5
* http://www.e-taxonomy.eu
6
* 
6
*
7 7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8 8
* See LICENSE.TXT at the top of this package for the full license terms.
9 9
*/
......
28 28
import eu.etaxonomy.cdm.model.common.Group;
29 29
import eu.etaxonomy.cdm.model.common.User;
30 30
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
31
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
31
import eu.etaxonomy.taxeditor.model.AbstractUtility;
32 32
import eu.etaxonomy.taxeditor.model.IContextListener;
33 33

  
34 34
/**
......
39 39
 * @version 1.0
40 40
 */
41 41
public class LoginManager extends Observable implements IConversationEnabled, IContextListener{
42
	
42

  
43 43
	public static final Logger logger = Logger.getLogger(LoginManager.class);
44
	
44

  
45 45
	private ConversationHolder conversation;
46
	
46

  
47 47
	public LoginManager(){
48 48
		CdmStore.getContextManager().addContextListener(this);
49 49
	}
50
	
50

  
51 51
	/**
52 52
	 * <p>authenticate</p>
53 53
	 *
......
57 57
	public boolean authenticate(String username, String password){
58 58

  
59 59
		// close all open editors
60
		if(!StoreUtil.closeAll()){
60
		if(!AbstractUtility.closeAll()){
61 61
			return false;
62 62
		}
63
			
64
		
63

  
64

  
65 65
		try{
66
			getConversationHolder().bind();
67
			getConversationHolder().commit();
68
			
69 66
			SecurityContextHolder.clearContext();
70
			
67

  
71 68
			Authentication lastAuthentication = CdmStore.getCurrentAuthentiation();
72
			
73
			UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password); 			
69

  
70
			UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
74 71
			Authentication authentication = CdmStore.getAuthenticationManager().authenticate(token);
75
			
72

  
76 73
			User user = (User) authentication.getPrincipal();
77 74
			/* circumventing problem with hibernate not refreshing the transient collection authorities in this case,
78 75
			 * see http://dev.e-taxonomy.eu/trac/ticket/4053 */
79
			user.initAuthorities(); 
80
			
76
			user.initAuthorities();
77

  
81 78
			if(logger.isDebugEnabled()){
82 79
				StringBuilder gaText = new StringBuilder();
83 80
				String indent = "    ";
......
87 84
					gaText.append(indent).append("gr[").append(gr.hashCode()).append("] \"").append(gr.getName()).append("\" ").append(gr.toString()).append("\n");
88 85
					_logGrantedAuthotities(gaText, indent + indent, gr.getGrantedAuthorities());
89 86
				}
90
				logger.debug("User authenticated: " + user.getUsername() + "\n" + gaText.toString());				
87
				logger.debug("User authenticated: " + user.getUsername() + "\n" + gaText.toString());
91 88
			}
92
		
93
			SecurityContextHolder.getContext().setAuthentication(authentication);			
89

  
90
			SecurityContextHolder.getContext().setAuthentication(authentication);
94 91

  
95 92
			if(!authentication.equals(lastAuthentication)){
96 93
				this.setChanged();
......
99 96
			return true;
100 97
		}
101 98
		catch(BadCredentialsException e){
102
			StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
99
			AbstractUtility.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
103 100
		}
104 101
		catch(LockedException e){
105
			StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
102
			AbstractUtility.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
106 103
		}
107 104
		catch(IllegalArgumentException e){
108
			StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
105
			AbstractUtility.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
109 106
		}
110 107
		return false;
111 108
	}
......
116 113
			gaText.append(indent).append("ga[").append(ga.hashCode()).append("] ").append(ga.toString()).append("\n");
117 114
		}
118 115
	}
119
	
116

  
120 117
	/**
121 118
	 * <p>getAuthenticatedUser</p>
122 119
	 *
......
124 121
	 */
125 122
	public User getAuthenticatedUser(){
126 123
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
127
		
128
		if(authentication != null 
129
				&& authentication.getPrincipal() != null 
124

  
125
		if(authentication != null
126
				&& authentication.getPrincipal() != null
130 127
				&& authentication.getPrincipal() instanceof User){
131 128
			return (User)authentication.getPrincipal();
132 129
		}
133 130
		return null;
134 131
	}
135
	
132

  
136 133
	public void logoutAll(){
137 134
		SecurityContextHolder.clearContext();
138
		notifyObservers();		
135
		notifyObservers();
139 136
	}
140 137

  
141 138
	/* (non-Javadoc)
......
157 154

  
158 155
	/**
159 156
	 * Whether the current user has the role admin
160
	 * 
157
	 *
161 158
	 * @return
162 159
	 */
163 160
	public boolean isAdmin() {
164 161
		// FIXME until we have rights implemented properly we do this
165
		// by a simple string check. This has to change 
166
		
162
		// by a simple string check. This has to change
163

  
167 164
		return "admin".equals(getAuthenticatedUser().getUsername());
168 165
	}
169 166

  
170 167
	@Override
171 168
	public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
172
		
169

  
173 170
	}
174 171

  
175 172
	@Override
176 173
	public void contextStop(IMemento memento, IProgressMonitor monitor) {
177
		
174

  
178 175
	}
179 176

  
180 177
	@Override
......
189 186

  
190 187
	@Override
191 188
	public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
192
		
189

  
193 190
	}
194 191
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)