merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / store / LoginManager.java
index 43d703e49f925c2c898745238254876baf4612e3..42c6e74930cf78578a5035d6f33747be26b750c0 100644 (file)
@@ -12,6 +12,8 @@ package eu.etaxonomy.taxeditor.store;
 
 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;
@@ -22,6 +24,7 @@ import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 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>
@@ -30,23 +33,40 @@ import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
  * @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.");
@@ -57,10 +77,7 @@ public class LoginManager extends Observable implements IConversationEnabled{
                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;
        }
        
        /**
@@ -100,4 +117,41 @@ public class LoginManager extends Observable implements IConversationEnabled{
                }
                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) {
+               
+       }
 }