import java.util.Observable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.ui.IMemento;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
import eu.etaxonomy.cdm.model.common.User;
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
+import eu.etaxonomy.taxeditor.model.IContextListener;
/**
* <p>LoginManager class.</p>
* @created 03.07.2009
* @version 1.0
*/
-public class LoginManager extends Observable implements IConversationEnabled{
+public class LoginManager extends Observable implements IConversationEnabled, IContextListener{
private ConversationHolder conversation;
+ public LoginManager(){
+ CdmStore.getContextManager().addContextListener(this);
+ }
+
/**
* <p>authenticate</p>
*
* @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
*/
- public void authenticate(String username, String password){
+ public boolean authenticate(String username, String password){
+
+ // close all open editors
+ if(!StoreUtil.closeAll()){
+ return false;
+ }
+
+
try{
getConversationHolder().bind();
- UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
SecurityContextHolder.clearContext();
- Authentication authentication = CdmStore.getAuthenticationManager().authenticate(token);
- SecurityContextHolder.getContext().setAuthentication(authentication);
+
+ UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
+ Authentication authentication = CdmStore.getAuthenticationManager().authenticate(token);
+
+ SecurityContextHolder.getContext().setAuthentication(authentication);
+
+ this.setChanged();
+ this.notifyObservers();
+ return true;
}
catch(BadCredentialsException e){
StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
catch(IllegalArgumentException e){
StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
}
- finally{
- this.setChanged();
- this.notifyObservers();
- }
+ return false;
}
/**
}
return conversation;
}
+
+ /**
+ * Whether the current user has the role admin
+ *
+ * @return
+ */
+ public boolean isAdmin() {
+ // FIXME until we have rights implemented properly we do this
+ // by a simple string check. This has to change
+
+ return "admin".equals(getAuthenticatedUser().getUsername());
+ }
+
+ @Override
+ public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
+
+ }
+
+ @Override
+ public void contextStop(IMemento memento, IProgressMonitor monitor) {
+
+ }
+
+ @Override
+ public void contextStart(IMemento memento, IProgressMonitor monitor){
+ conversation = CdmStore.createConversation();
+ }
+
+ @Override
+ public void contextRefresh(IProgressMonitor monitor) {
+ conversation = CdmStore.createConversation();
+ }
+
+ @Override
+ public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
+
+ }
}