fixed bug 17142: Classic mode window when showing an error when login
authorAsier Lostalé <asier.lostale@openbravo.com>
Tue, 26 Jul 2011 14:37:12 +0200
changeset 13273 c83d61e490f2
parent 13272 b00b4f79005e
child 13274 b4f549d970cf
fixed bug 17142: Classic mode window when showing an error when login
src/org/openbravo/base/secureApp/HttpSecureAppServlet.java
src/org/openbravo/base/secureApp/LoginHandler.java
src/org/openbravo/base/secureApp/LoginUtils.java
--- a/src/org/openbravo/base/secureApp/HttpSecureAppServlet.java	Tue Jul 26 13:49:29 2011 +0200
+++ b/src/org/openbravo/base/secureApp/HttpSecureAppServlet.java	Tue Jul 26 14:37:12 2011 +0200
@@ -55,6 +55,7 @@
 import org.openbravo.authentication.basic.DefaultAuthenticationManager;
 import org.openbravo.base.HttpBaseServlet;
 import org.openbravo.base.exception.OBException;
+import org.openbravo.base.secureApp.LoginUtils.RoleDefaults;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
@@ -279,42 +280,12 @@
             strOrg = "0";
             strWarehouse = "";
           } else {
-            strRole = variables.getRole();
-
-            if (strRole.equals("")) {
-              // use default role
-              strRole = DefaultOptionsData.defaultRole(this, strUserAuth);
-              if (strRole == null || !LoginUtils.validUserRole(this, strUserAuth, strRole)) {
-                // if default not set or not valid take any one
-                strRole = DefaultOptionsData.getDefaultRole(this, strUserAuth);
-              }
-            }
-            validateDefault(strRole, strUserAuth, "Role");
-
-            strOrg = DefaultOptionsData.defaultOrg(this, strUserAuth);
-            // use default org
-            if (strOrg == null || !LoginUtils.validRoleOrg(this, strRole, strOrg)) {
-              // if default not set or not valid take any one
-              strOrg = DefaultOptionsData.getDefaultOrg(this, strRole);
-            }
-            validateDefault(strOrg, strRole, "Org");
-
-            strClient = DefaultOptionsData.defaultClient(this, strUserAuth);
-            // use default client
-            if (strClient == null || !LoginUtils.validRoleClient(this, strRole, strClient)) {
-              // if default not set or not valid take any one
-              strClient = DefaultOptionsData.getDefaultClient(this, strRole);
-            }
-            validateDefault(strClient, strRole, "Client");
-
-            strWarehouse = DefaultOptionsData.defaultWarehouse(this, strUserAuth);
-            if (strWarehouse == null) {
-              if (!strRole.equals("0")) {
-                strWarehouse = DefaultOptionsData.getDefaultWarehouse(this, strClient, new OrgTree(
-                    this, strClient).getAccessibleTree(this, strRole).toString());
-              } else
-                strWarehouse = "";
-            }
+            RoleDefaults defaults = LoginUtils.getLoginDefaults(strUserAuth, variables.getRole(),
+                this);
+            strRole = defaults.role;
+            strClient = defaults.client;
+            strOrg = defaults.org;
+            strWarehouse = defaults.warehouse;
           }
 
           DefaultOptionsData dataLanguage[] = DefaultOptionsData.defaultLanguage(this, strUserAuth);
@@ -352,11 +323,15 @@
     } catch (final DefaultValidationException d) {
       // Added DefaultValidationException class to catch user login
       // without a valid role
+
+      String title = Utility.messageBD(myPool, "InvalidDefaultLoginTitle", variables.getLanguage())
+          .replace("%0", d.getDefaultField());
+      String msg = Utility.messageBD(myPool, "InvalidDefaultLoginMsg", variables.getLanguage())
+          .replace("%0", d.getDefaultField());
       final OBError roleError = new OBError();
-      roleError.setTitle("Invalid " + d.getDefaultField());
+      roleError.setTitle(title);
       roleError.setType("Error");
-      roleError.setMessage("No valid " + d.getDefaultField()
-          + " identified. Please contact your system administrator for access.");
+      roleError.setMessage(msg);
       invalidLogin(request, response, roleError);
 
       return;
@@ -574,20 +549,6 @@
     return retValue;
   }
 
-  /**
-   * Validates if a selected default value is null or empty String
-   * 
-   * @param strValue
-   * @param strKey
-   * @param strError
-   * @throws Exeption
-   * */
-  private void validateDefault(String strValue, String strKey, String strError) throws Exception {
-    if (strValue == null || strValue.equals(""))
-      throw new DefaultValidationException("Unable to read default " + strError + " for:" + strKey,
-          strError);
-  }
-
   protected void logout(HttpServletRequest request, HttpServletResponse response)
       throws IOException, ServletException {
 
--- a/src/org/openbravo/base/secureApp/LoginHandler.java	Tue Jul 26 13:49:29 2011 +0200
+++ b/src/org/openbravo/base/secureApp/LoginHandler.java	Tue Jul 26 14:37:12 2011 +0200
@@ -240,6 +240,18 @@
         return;
       }
 
+      try {
+        LoginUtils.getLoginDefaults(strUserAuth, "", myPool);
+      } catch (DefaultValidationException e) {
+        updateDBSession(sessionId, false, "F");
+        String title = Utility.messageBD(myPool, "InvalidDefaultLoginTitle", vars.getLanguage())
+            .replace("%0", e.getDefaultField());
+        String msg = Utility.messageBD(myPool, "InvalidDefaultLoginMsg", vars.getLanguage())
+            .replace("%0", e.getDefaultField());
+        goToRetry(res, vars, msg, title, "Error", "../security/Menu.html", doRedirect);
+        return;
+      }
+
       // All checks passed successfully, continue logging in
       goToTarget(res, vars, doRedirect);
     } finally {
--- a/src/org/openbravo/base/secureApp/LoginUtils.java	Tue Jul 26 13:49:29 2011 +0200
+++ b/src/org/openbravo/base/secureApp/LoginUtils.java	Tue Jul 26 14:37:12 2011 +0200
@@ -1,6 +1,6 @@
 /*
  ************************************************************************************
- * Copyright (C) 2001-2010 Openbravo S.L.U.
+ * Copyright (C) 2001-2011 Openbravo S.L.U.
  * Licensed under the Apache Software License version 2.0
  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  * Unless required by applicable law or agreed to  in writing,  software  distributed
@@ -313,4 +313,78 @@
     return true;
   }
 
+  /**
+   * Obtains defaults defined for a user and throws DefaultValidationException in case they are not
+   * correct.
+   */
+  static RoleDefaults getLoginDefaults(String strUserAuth, String role, ConnectionProvider cp)
+      throws ServletException, DefaultValidationException {
+    String strRole = role;
+    if (strRole.equals("")) {
+      // use default role
+      strRole = DefaultOptionsData.defaultRole(cp, strUserAuth);
+      if (strRole == null || !LoginUtils.validUserRole(cp, strUserAuth, strRole)) {
+        // if default not set or not valid take any one
+        strRole = DefaultOptionsData.getDefaultRole(cp, strUserAuth);
+      }
+    }
+    validateDefault(strRole, strUserAuth, "Role");
+
+    String strOrg = DefaultOptionsData.defaultOrg(cp, strUserAuth);
+    // use default org
+    if (strOrg == null || !LoginUtils.validRoleOrg(cp, strRole, strOrg)) {
+      // if default not set or not valid take any one
+      strOrg = DefaultOptionsData.getDefaultOrg(cp, strRole);
+    }
+    validateDefault(strOrg, strRole, "Org");
+
+    String strClient = DefaultOptionsData.defaultClient(cp, strUserAuth);
+    // use default client
+    if (strClient == null || !LoginUtils.validRoleClient(cp, strRole, strClient)) {
+      // if default not set or not valid take any one
+      strClient = DefaultOptionsData.getDefaultClient(cp, strRole);
+    }
+    validateDefault(strClient, strRole, "Client");
+
+    String strWarehouse = DefaultOptionsData.defaultWarehouse(cp, strUserAuth);
+    if (strWarehouse == null) {
+      if (!strRole.equals("0")) {
+        strWarehouse = DefaultOptionsData.getDefaultWarehouse(cp, strClient, new OrgTree(cp,
+            strClient).getAccessibleTree(cp, strRole).toString());
+      } else
+        strWarehouse = "";
+    }
+    RoleDefaults defaults = new RoleDefaults();
+    defaults.role = strRole;
+    defaults.client = strClient;
+    defaults.org = strOrg;
+    defaults.warehouse = strWarehouse;
+    return defaults;
+  }
+
+  /**
+   * Validates if a selected default value is null or empty String
+   * 
+   * @param strValue
+   * @param strKey
+   * @param strError
+   * @throws Exeption
+   * */
+  private static void validateDefault(String strValue, String strKey, String strError)
+      throws DefaultValidationException {
+    if (strValue == null || strValue.equals(""))
+      throw new DefaultValidationException("Unable to read default " + strError + " for:" + strKey,
+          strError);
+  }
+
+  /**
+   * Utility class to store login defaults
+   * 
+   */
+  static class RoleDefaults {
+    String role;
+    String client;
+    String org;
+    String warehouse;
+  }
 }