merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / store / LoginManager.java
index beb18fbc3289985b09fb7f512e40d07d99b61dfc..b01cd9a978f456ed467e020e79a3a4f890d9391e 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 /**
 * Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
+* European Distributed Institute of Taxonomy
 * http://www.e-taxonomy.eu
-* 
+*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
@@ -28,8 +28,9 @@ import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
 import eu.etaxonomy.cdm.model.common.Group;
 import eu.etaxonomy.cdm.model.common.User;
 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
-import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.IContextListener;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 
 /**
  * <p>LoginManager class.</p>
@@ -39,42 +40,42 @@ import eu.etaxonomy.taxeditor.model.IContextListener;
  * @version 1.0
  */
 public class LoginManager extends Observable implements IConversationEnabled, IContextListener{
-       
+
        public static final Logger logger = Logger.getLogger(LoginManager.class);
-       
+
        private ConversationHolder conversation;
-       
+
        public LoginManager(){
                CdmStore.getContextManager().addContextListener(this);
        }
-       
+
        /**
         * <p>authenticate</p>
         *
         * @param token a {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} object.
+        * @return true if the login attempt was successful even if the authentication has changed or not
         */
        public boolean authenticate(String username, String password){
 
                // close all open editors
-               if(!StoreUtil.closeAll()){
+               if(!AbstractUtility.closeAll()){
                        return false;
                }
-                       
-               
+
+
                try{
-                       getConversationHolder().bind();
-                       getConversationHolder().commit();
-                       
                        SecurityContextHolder.clearContext();
-                       
-                       UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);                        
+
+                       Authentication lastAuthentication = CdmStore.getCurrentAuthentiation();
+
+                       UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
                        Authentication authentication = CdmStore.getAuthenticationManager().authenticate(token);
-                       
+
                        User user = (User) authentication.getPrincipal();
                        /* circumventing problem with hibernate not refreshing the transient collection authorities in this case,
                         * see http://dev.e-taxonomy.eu/trac/ticket/4053 */
-                       user.initAuthorities(); 
-                       
+                       user.initAuthorities();
+
                        if(logger.isDebugEnabled()){
                                StringBuilder gaText = new StringBuilder();
                                String indent = "    ";
@@ -84,23 +85,25 @@ public class LoginManager extends Observable implements IConversationEnabled, IC
                                        gaText.append(indent).append("gr[").append(gr.hashCode()).append("] \"").append(gr.getName()).append("\" ").append(gr.toString()).append("\n");
                                        _logGrantedAuthotities(gaText, indent + indent, gr.getGrantedAuthorities());
                                }
-                               logger.debug("User authenticated: " + user.getUsername() + "\n" + gaText.toString());                           
+                               logger.debug("User authenticated: " + user.getUsername() + "\n" + gaText.toString());
                        }
-               
-                       SecurityContextHolder.getContext().setAuthentication(authentication);                   
 
-                       this.setChanged();
-                       this.notifyObservers();
+                       SecurityContextHolder.getContext().setAuthentication(authentication);
+
+                       if(!authentication.equals(lastAuthentication)){
+                               this.setChanged();
+                               this.notifyObservers();
+                       }
                        return true;
                }
                catch(BadCredentialsException e){
-                       StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
+                       MessagingUtils.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
                }
                catch(LockedException e){
-                       StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
+                       MessagingUtils.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
                }
                catch(IllegalArgumentException e){
-                       StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
+                       MessagingUtils.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
                }
                return false;
        }
@@ -111,7 +114,7 @@ public class LoginManager extends Observable implements IConversationEnabled, IC
                        gaText.append(indent).append("ga[").append(ga.hashCode()).append("] ").append(ga.toString()).append("\n");
                }
        }
-       
+
        /**
         * <p>getAuthenticatedUser</p>
         *
@@ -119,18 +122,18 @@ public class LoginManager extends Observable implements IConversationEnabled, IC
         */
        public User getAuthenticatedUser(){
                Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
-               
-               if(authentication != null 
-                               && authentication.getPrincipal() != null 
+
+               if(authentication != null
+                               && authentication.getPrincipal() != null
                                && authentication.getPrincipal() instanceof User){
                        return (User)authentication.getPrincipal();
                }
                return null;
        }
-       
+
        public void logoutAll(){
                SecurityContextHolder.clearContext();
-               notifyObservers();              
+               notifyObservers();
        }
 
        /* (non-Javadoc)
@@ -152,24 +155,24 @@ public class LoginManager extends Observable implements IConversationEnabled, IC
 
        /**
         * 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 
-               
+               // 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
@@ -184,6 +187,6 @@ public class LoginManager extends Observable implements IConversationEnabled, IC
 
        @Override
        public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
-               
+
        }
 }