Using on Ofbiz instance with multiple databases
-----------------------------------------------

                 Key: OFBIZ-2020
                 URL: https://issues.apache.org/jira/browse/OFBIZ-2020
             Project: OFBiz
          Issue Type: New Feature
    Affects Versions: SVN trunk
            Reporter: youssef khaye
             Fix For: SVN trunk


I want to share my work with you implementing the use of one OFBiz instance 
with multiple databases, by defining multiple delegators, in the 
entityengine.xml, one for each databases. this is useful when we implementing 
ofbiz for semi-independent subsidiaries of one company having users allowed to 
use two or more databases.
This involve mainly the user authentication procedure by asking for a company 
name in the login form. This company name represents a delegator name that 
describe a specific subsidiary database. After a successful user login 
operation, the passed company name is used to retrieve the corresponding 
delegator. The method CoreEvents.chageDelegator is modified to change the 
delegator of related Dispatcher and JobManager.
Of course I needed to store the delegator name in the GenericValue UserLogin to 
navigate among different ofbiz applications keeping the same original database.
I also provided a kind of mechanism to activate or deactivates the use of 
multi-delegator by adding a "multi.delegator" property in the 
security.properties configuration file that when set true, cause the ofbiz to 
display he company field in the login form and do the necessary work to switch 
from default database to the provided one.
I will be open to discuss any suggestion for improving this issue.
Following is a patch for current ofbiz trunk version:
Index: 
/home/youssef/workspace/ofbiz/applications/party/entitydef/entitymodel.xml

===================================================================

--- /home/youssef/workspace/ofbiz/applications/party/entitydef/entitymodel.xml  
(revision 705872)

+++ /home/youssef/workspace/ofbiz/applications/party/entitydef/entitymodel.xml  
(working copy)

@@ -2611,6 +2611,9 @@

     </extend-entity>

     <extend-entity entity-name="UserLogin">

         <field name="partyId" type="id"></field>

+        <!--Begin specific : field used to store the delegator name along  
theuse session -->

+        <field name="delegator" type="id"></field>

+        <!-- End Specific  -->

         <relation type="one" fk-name="USER_PARTY" rel-entity-name="Party">

             <key-map field-name="partyId"/>

         </relation>

Index: /home/youssef/workspace/ofbiz/framework/common/config/CommonUiLabels.xml

===================================================================

--- /home/youssef/workspace/ofbiz/framework/common/config/CommonUiLabels.xml    
(revision 705872)

+++ /home/youssef/workspace/ofbiz/framework/common/config/CommonUiLabels.xml    
(working copy)

@@ -1617,6 +1617,23 @@

         <value xml:lang="zh_CN">十二月</value>

         <value xml:lang="zh">十二月</value>

     </property>

+    <property key="CommonCompany">

+        <value xml:lang="ar">المؤسسة</value>

+        <value xml:lang="en">Company</value>

+        <value xml:lang="fr">Entreprise</value>

+    </property>

     <property key="CommonDelete">

         <value xml:lang="ar">حذف</value>

         <value xml:lang="cs">Smazat</value>

Index: 
/home/youssef/workspace/ofbiz/framework/common/src/org/ofbiz/common/login/LoginServices.java

===================================================================

--- 
/home/youssef/workspace/ofbiz/framework/common/src/org/ofbiz/common/login/LoginServices.java
        (revision 705872)

+++ 
/home/youssef/workspace/ofbiz/framework/common/src/org/ofbiz/common/login/LoginServices.java
        (working copy)

@@ -90,7 +90,7 @@

         if (username == null) username = (String) context.get("username");

         String password = (String) context.get("login.password");

         if (password == null) password = (String) context.get("password");

-

+        String userDelegatorName = (String) context.get("userDelegatorName");

         // get the visitId for the history entity

         String visitId = (String) context.get("visitId");

 

@@ -193,7 +193,9 @@

                                 // successful login & no loggout flag, no need 
to change anything, so don't do the store

                                 doStore = false;

                             }

-

+                            
if("true".equals(UtilProperties.getPropertyValue("security.properties", 
"multi.delegator"))){

+                                userLogin.set("delegator", userDelegatorName);

+                            }

                             successfulLogin = "Y";

 

                             if (!isServiceAuth) {

Index: 
/home/youssef/workspace/ofbiz/framework/common/webcommon/includes/header.ftl

===================================================================

--- 
/home/youssef/workspace/ofbiz/framework/common/webcommon/includes/header.ftl    
    (revision 705872)

+++ 
/home/youssef/workspace/ofbiz/framework/common/webcommon/includes/header.ftl    
    (working copy)

@@ -96,6 +96,7 @@

   </div>

   <div id="masthead">

     <ul>

+      <li> Delegator : ${delegator.getDelegatorName()}</li>

       <#if (userPreferences.COMPACT_HEADER)?default("N") == "Y">

         <li class="logo-area">

           <#if shortcutIcon?has_content>

Index: /home/youssef/workspace/ofbiz/framework/common/webcommon/login.ftl

===================================================================

--- /home/youssef/workspace/ofbiz/framework/common/webcommon/login.ftl  
(revision 705872)

+++ /home/youssef/workspace/ofbiz/framework/common/webcommon/login.ftl  
(working copy)

@@ -18,7 +18,7 @@

 -->

 

 <#if requestAttributes.uiLabelMap?exists><#assign uiLabelMap = 
requestAttributes.uiLabelMap></#if>

-

+<#assign multidelegator = 
Static["org.ofbiz.base.util.UtilProperties"].getPropertyValue("security.properties",
 "multi.delegator")>

 <#assign previousParams = sessionAttributes._PREVIOUS_PARAMS_?if_exists>

 <#if previousParams?has_content>

   <#assign previousParams = "?" + previousParams>

@@ -46,6 +46,12 @@

             <td class="label">${uiLabelMap.CommonPassword}</td>

             <td><input type="password" name="PASSWORD" value="" 
size="20"/></td>

           </tr>

+          <#if multidelegator=="true">

+          <tr>

+           <td class="label">${uiLabelMap.CommonCompany}</td>

+           <td><input type="text" class="inputBox" name="DELEGATOR" value="" 
size="20"/></td>

+          </tr>

+          </#if>

           <tr>

             <td colspan="2" align="center">

               <input type="submit" value="${uiLabelMap.CommonLogin}"/>

Index: /home/youssef/workspace/ofbiz/framework/entity/config/entityengine.xml

===================================================================

--- /home/youssef/workspace/ofbiz/framework/entity/config/entityengine.xml      
(revision 705872)

+++ /home/youssef/workspace/ofbiz/framework/entity/config/entityengine.xml      
(working copy)

@@ -53,6 +53,9 @@

         <group-map group-name="org.ofbiz" datasource-name="localderby"/>

         <group-map group-name="org.ofbiz.olap" 
datasource-name="localderbyolap"/>

     </delegator>

+    <delegator name="entreprise1" entity-model-reader="main" 
entity-group-reader="main" entity-eca-reader="main">

+        <group-map group-name="org.ofbiz" datasource-name="localpostgres1"/>   
     

+    </delegator>

     <delegator name="default-no-eca" entity-model-reader="main" 
entity-group-reader="main" entity-eca-reader="main" entity-eca-enabled="false" 
distributed-cache-clear-enabled="false">

         <group-map group-name="org.ofbiz" datasource-name="localderby"/>

         <group-map group-name="org.ofbiz.olap" 
datasource-name="localderbyolap"/>

@@ -327,6 +330,33 @@

                 pool-maxsize="250"/>

         <!-- <jndi-jdbc jndi-server-name="localjndi" 
jndi-name="java:/MySqlDataSource" isolation-level="Serializable"/> -->

     </datasource>

+    <datasource name="localpostgres1"

+            helper-class="org.ofbiz.entity.datasource.GenericHelperDAO"

+            schema-name="public"

+            field-type-name="postgres"

+            check-on-start="true"

+            add-missing-on-start="true"

+            use-fk-initially-deferred="false"

+            alias-view-columns="false"

+            join-style="ansi"

+            use-binary-type-for-blob="true">

+            <!-- use this attribute to make the EntityListIterator more 
effective for pgjdbc 7.5devel and later: 

+                result-fetch-size="50"

+            -->

+        <read-data reader-name="seed"/>

+        <read-data reader-name="seed-initial"/>

+        <read-data reader-name="demo"/>

+        <read-data reader-name="ext"/>

+        <inline-jdbc

+                jdbc-driver="org.postgresql.Driver"

+                jdbc-uri="jdbc:postgresql://127.0.0.1/entreprise1"

+                jdbc-username="ofbiz"

+                jdbc-password="ofbiz"

+                isolation-level="ReadCommitted"

+                pool-minsize="2"

+                pool-maxsize="250"/>

+        <!-- <jndi-jdbc jndi-server-name="localjndi" 
jndi-name="java:/MySqlDataSource" isolation-level="Serializable"/> -->

+    </datasource>

 

     <datasource name="localpostgres"

             helper-class="org.ofbiz.entity.datasource.GenericHelperDAO"

Index: 
/home/youssef/workspace/ofbiz/framework/security/config/security.properties

===================================================================

--- /home/youssef/workspace/ofbiz/framework/security/config/security.properties 
(revision 705872)

+++ /home/youssef/workspace/ofbiz/framework/security/config/security.properties 
(working copy)

@@ -77,3 +77,6 @@

 

 # -- Hours after which EmailAdressVerification should expire

 email_verification.expire.hours=48

+

+# -- are we using multi delegator

+multi.delegator=false

Index: 
/home/youssef/workspace/ofbiz/framework/service/src/org/ofbiz/service/GenericAbstractDispatcher.java

===================================================================

--- 
/home/youssef/workspace/ofbiz/framework/service/src/org/ofbiz/service/GenericAbstractDispatcher.java
        (revision 705872)

+++ 
/home/youssef/workspace/ofbiz/framework/service/src/org/ofbiz/service/GenericAbstractDispatcher.java
        (working copy)

@@ -182,7 +182,14 @@

     public GenericDelegator getDelegator() {

         return dispatcher.getDelegator();

     }

-  

+        

+    /**

+     * @see org.ofbiz.service.LocalDispatcher#setDelegator()

+     */

+    public void setDelegator(GenericDelegator delegator) {

+            dispatcher.setDelegator(delegator);

+    }

+

     /**

      * @see org.ofbiz.service.LocalDispatcher#getSecurity()

      */

Index: 
/home/youssef/workspace/ofbiz/framework/service/src/org/ofbiz/service/job/JobManager.java

===================================================================

--- 
/home/youssef/workspace/ofbiz/framework/service/src/org/ofbiz/service/job/JobManager.java
   (revision 705872)

+++ 
/home/youssef/workspace/ofbiz/framework/service/src/org/ofbiz/service/job/JobManager.java
   (working copy)

@@ -100,7 +100,9 @@

     public GenericDelegator getDelegator() {

         return this.delegator;

     }

-

+    public void setDelegator(GenericDelegator delegator) {

+        this.delegator=delegator;

+    }

     public synchronized List<Job> poll() {

         List<Job> poll = FastList.newInstance();

 

Index: 
/home/youssef/workspace/ofbiz/framework/service/src/org/ofbiz/service/LocalDispatcher.java

===================================================================

--- 
/home/youssef/workspace/ofbiz/framework/service/src/org/ofbiz/service/LocalDispatcher.java
  (revision 705872)

+++ 
/home/youssef/workspace/ofbiz/framework/service/src/org/ofbiz/service/LocalDispatcher.java
  (working copy)

@@ -317,7 +317,7 @@

      * @return GenericEntityDelegator associated with this dispatcher

      */

     public GenericDelegator getDelegator();

-

+    public void setDelegator(GenericDelegator delegator);

     /**

      * Gets the Security object associated with this dispatcher

      * @return Security object associated with this dispatcher

Index: 
/home/youssef/workspace/ofbiz/framework/service/src/org/ofbiz/service/ServiceDispatcher.java

===================================================================

--- 
/home/youssef/workspace/ofbiz/framework/service/src/org/ofbiz/service/ServiceDispatcher.java
        (revision 705872)

+++ 
/home/youssef/workspace/ofbiz/framework/service/src/org/ofbiz/service/ServiceDispatcher.java
        (working copy)

@@ -789,7 +789,14 @@

     public GenericDelegator getDelegator() {

         return this.delegator;

     }

-

+    

+    /**

+     * Gets the GenericDelegator associated with this dispatcher

+     * @return GenericDelegator associated with this dispatcher

+     */

+    public void setDelegator(GenericDelegator delegator) {

+        this.delegator = delegator;

+    }

     /**

      * Gets the Security object associated with this dispatcher

      * @return Security object associated with this dispatcher

Index: /home/youssef/workspace/ofbiz/framework/webapp/config/WebappUiLabels.xml

===================================================================

--- /home/youssef/workspace/ofbiz/framework/webapp/config/WebappUiLabels.xml    
(revision 705872)

+++ /home/youssef/workspace/ofbiz/framework/webapp/config/WebappUiLabels.xml    
(working copy)

@@ -271,6 +271,15 @@

         <value xml:lang="th">รหัสผ่านเป็นค่าว่าง กรุณากรอกอีกครั้ง</value>

         <value xml:lang="zh">密码是空的,请重新输入。</value>

     </property>

+    <property key="loginevents.delegator_not_found_reenter">

+        <value xml:lang="de">Company not found, please re-enter.</value>

+        <value xml:lang="en">Company not found, please re-enter.</value>

+        <value xml:lang="fr">Merci de v\u00E9rifiez l'entrepripse</value>

+        <value xml:lang="it">Company not found, please re-enter.</value>

+        <value xml:lang="ru">Company not found, please re-enter.</value>

+        <value xml:lang="th">Company not found, please re-enter.</value>

+        <value xml:lang="zh">Company not found, please re-enter.</value>

+    </property>

     <property key="loginevents.unable_to_login_this_application">

         <value xml:lang="de">Sie können sich nicht bei dieser Anwendung 
anmelden (benötigte Berechtigungen fehlen).</value>

         <value xml:lang="en">Login for this application couldn't be completed 
(required permissions missing).</value>

Index: 
/home/youssef/workspace/ofbiz/framework/webapp/src/org/ofbiz/webapp/control/LoginWorker.java

===================================================================

--- 
/home/youssef/workspace/ofbiz/framework/webapp/src/org/ofbiz/webapp/control/LoginWorker.java
        (revision 705872)

+++ 
/home/youssef/workspace/ofbiz/framework/webapp/src/org/ofbiz/webapp/control/LoginWorker.java
        (working copy)

@@ -18,6 +18,8 @@

  
*******************************************************************************/

 package org.ofbiz.webapp.control;

 

+import static org.ofbiz.base.util.UtilGenerics.checkMap;

+

 import java.io.UnsupportedEncodingException;

 import java.math.BigInteger;

 import java.net.URLEncoder;

@@ -23,7 +25,6 @@

 import java.net.URLEncoder;

 import java.security.cert.X509Certificate;

 import java.util.Enumeration;

-import java.util.HashMap;

 import java.util.List;

 import java.util.Map;

 import java.util.regex.Matcher;

@@ -47,7 +48,6 @@

 import org.ofbiz.base.util.GeneralException;

 import org.ofbiz.base.util.KeyStoreUtil;

 import org.ofbiz.base.util.UtilFormatOut;

-import static org.ofbiz.base.util.UtilGenerics.checkMap;

 import org.ofbiz.base.util.UtilHttp;

 import org.ofbiz.base.util.UtilMisc;

 import org.ofbiz.base.util.UtilProperties;

@@ -58,7 +58,6 @@

 import org.ofbiz.entity.GenericValue;

 import org.ofbiz.entity.condition.EntityCondition;

 import org.ofbiz.entity.condition.EntityConditionList;

-import org.ofbiz.entity.condition.EntityExpr;

 import org.ofbiz.entity.condition.EntityOperator;

 import org.ofbiz.entity.model.ModelEntity;

 import org.ofbiz.entity.transaction.GenericTransactionException;

@@ -68,6 +67,7 @@

 import org.ofbiz.service.LocalDispatcher;

 import org.ofbiz.service.ModelService;

 import org.ofbiz.service.ServiceUtil;

+import org.ofbiz.webapp.event.CoreEvents;

 import org.ofbiz.webapp.stats.VisitHandler;

 

 /**

@@ -192,6 +192,9 @@

                     Debug.logError("Could not find UserLogin record for 
setLoggedOut with userLoginId [" + userLoginId + "]", module);

                 } else {

                     userLogin.set("hasLoggedOut", "Y");

+                    
if("true".equals(UtilProperties.getPropertyValue("security.properties", 
"multi.delegator"))){

+                        userLogin.set("delegator", null);

+                    }

                     userLogin.store();

                 }

             } catch (GenericEntityException e) {

@@ -308,9 +311,11 @@

 

         String username = request.getParameter("USERNAME");

         String password = request.getParameter("PASSWORD");

-

+        String userDelegatorName = request.getParameter("DELEGATOR");

+        

         if (username == null) username = (String) 
session.getAttribute("USERNAME");

         if (password == null) password = (String) 
session.getAttribute("PASSWORD");

+        if (userDelegatorName == null) userDelegatorName = (String) 
session.getAttribute("DELEGATOR");

         

         // allow a username and/or password in a request attribute to override 
the request parameter or the session attribute; this way a preprocessor can 
play with these a bit...

         if (UtilValidate.isNotEmpty((String) 
request.getAttribute("USERNAME"))) {

@@ -319,6 +324,11 @@

         if (UtilValidate.isNotEmpty((String) 
request.getAttribute("PASSWORD"))) {

             password = (String) request.getAttribute("PASSWORD");

         }

+        if("true".equals(UtilProperties.getPropertyValue("securiy.properties", 
"multi.delegator"))){

+            if (UtilValidate.isNotEmpty((String) 
request.getAttribute("DELEGATOR"))) {

+                userDelegatorName = (String) request.getAttribute("DELEGATOR");

+            }

+        }

 

         List<String> unpwErrMsgList = FastList.newInstance();

         if (UtilValidate.isEmpty(username)) {

@@ -339,7 +349,21 @@

         if ((password != null) && 
("true".equalsIgnoreCase(UtilProperties.getPropertyValue("security.properties", 
"password.lowercase")))) {

             password = password.toLowerCase();

         }

-

+        if ((userDelegatorName == null) && 
("true".equalsIgnoreCase(UtilProperties.getPropertyValue("security.properties", 
"multi.delegator")))) {

+            String errMsg = UtilProperties.getMessage(resourceWebapp, 
"loginevents.delegator_not_found_reenter",  UtilHttp.getLocale(request));

+            request.setAttribute("_ERROR_MESSAGE_", errMsg);

+            return "error";

+        }

+        GenericDelegator userDelegator=null;

+        if 
("true".equalsIgnoreCase(UtilProperties.getPropertyValue("security.properties", 
"multi.delegator"))) {

+            
userDelegator=GenericDelegator.getGenericDelegator(userDelegatorName);

+            if (userDelegator==null){

+                String errMsg = UtilProperties.getMessage(resourceWebapp, 
"loginevents.delegator_not_found_reenter",  UtilHttp.getLocale(request));

+                request.setAttribute("_ERROR_MESSAGE_", errMsg);

+                return "error";

+            }

+        }

+        

         String requirePasswordChange = 
request.getParameter("requirePasswordChange");

 

         // get the visit id to pass to the userLogin for history

@@ -373,6 +397,10 @@

         }

 

         try {

+            if 
("true".equalsIgnoreCase(UtilProperties.getPropertyValue("security.properties", 
"multi.delegator"))) {

+                dispatcher.setDelegator(userDelegator);

+                request.getSession().setAttribute("delegatorName", 
userDelegatorName);

+            }

             result = dispatcher.runSync("userLogin", 
UtilMisc.toMap("login.username", username, "login.password", password, 
"visitId", visitId, "locale", UtilHttp.getLocale(request)));

         } catch (GenericServiceException e) {

             Debug.logError(e, "Error calling userLogin service", module);

@@ -385,6 +413,18 @@

         if 
(ModelService.RESPOND_SUCCESS.equals(result.get(ModelService.RESPONSE_MESSAGE)))
 {

             GenericValue userLogin = (GenericValue) result.get("userLogin");

             Map<String, Object> userLoginSession = 
checkMap(result.get("userLoginSession"), String.class, Object.class);

+           

+            try{

+                
if("true".equals(UtilProperties.getPropertyValue("security.properties", 
"multi.delegator"))){

+                    userLogin.set("delegator", userDelegatorName); 

+                }

+                userLogin.store();

+            }

+            catch(GenericEntityException e){

+                request.setAttribute("_ERROR_MESSAGE_", "Unable to store 
userLogin");

+                return "error";

+            }

+

             if (userLogin != null && 
"Y".equals(userLogin.getString("requirePasswordChange"))) {

                 return "requirePasswordChange";

             }

@@ -419,7 +459,15 @@

         if (userLoginSession != null) {

             session.setAttribute("userLoginSession", userLoginSession);

         }

-

+        

+            

+        

+        //Begin specific

+        if 
("true".equalsIgnoreCase(UtilProperties.getPropertyValue("security.properties", 
"multi.delegator"))) {

+        request.getSession().setAttribute("delegatorName", 
userLogin.getString("delegator"));

+        CoreEvents.changeDelegator(request, response);

+        }

+        //End specific        

         request.setAttribute("_LOGIN_PASSED_", "TRUE");

 

         // run the after-login events

@@ -499,6 +547,16 @@

         // set the logged out flag

         LoginWorker.setLoggedOut(userLogin.getString("userLoginId"), 
delegator);

 

+        //Begin specific :come back to default delegator for next login

+        
if("true".equals(UtilProperties.getPropertyValue("security.properties", 
"multi.delegator"))){

+            GenericDelegator 
defaultdelegator=GenericDelegator.getGenericDelegator("default");

+            request.getSession().setAttribute("delegatorName", 
defaultdelegator.getDelegatorName());  

+            HttpServletResponse response=null;

+            CoreEvents.changeDelegator(request, response);

+        }

+        //End specific   

+

+

         // this is a setting we don't want to lose, although it would be good 
to have a more general solution here...

         String currCatalog = (String) 
session.getAttribute("CURRENT_CATALOG_ID");

         // also make sure the delegatorName is preserved, especially so that a 
new Visit can be created

@@ -778,8 +836,14 @@

             GenericValue currentUserLogin = (GenericValue) 
session.getAttribute("userLogin");

             if (currentUserLogin != null) {

                 if 
(currentUserLogin.getString("userLoginId").equals(userLogin.getString("userLoginId")))
 {

-                    // is the same user, just carry on...

-                    return "success";

+                    GenericDelegator currentDelegator=(GenericDelegator) 
session.getAttribute("delegator");

+                    
if(("true".equals(UtilProperties.getPropertyValue("security.properties", 
"multi.delegator")))&&(currentDelegator.getDelegatorName().equals(userLogin.getString("delegator")))){

+                        // is the same user, just carry on...

+                        return "success";

+                    }

+                    else{

+                        return "success";

+                    }

                 }

 

                 // logout the current user and login the new user...

@@ -786,8 +850,19 @@

                 logout(request, response);

                 // ignore the return value; even if the operation failed we 
want to set the new UserLogin

             }

-

+            

             doBasicLogin(userLogin, request);

+            

+            //Begin specific

+            
if("true".equals(UtilProperties.getPropertyValue("security.properties", 
"multi.delegator"))){

+                String userDelegatorName = userLogin.getString("delegator");

+                request.getSession().setAttribute("delegatorName", 
userDelegatorName);

+                request.setAttribute("delegatorName", userDelegatorName);

+                CoreEvents.changeDelegator(request, response);

+            }

+            //End specific

+

+            

         } else {

             Debug.logWarning("Could not find userLogin for external login key: 
" + externalKey, module);

         }

Index: 
/home/youssef/workspace/ofbiz/framework/webapp/src/org/ofbiz/webapp/event/CoreEvents.java

===================================================================

--- 
/home/youssef/workspace/ofbiz/framework/webapp/src/org/ofbiz/webapp/event/CoreEvents.java
   (revision 705872)

+++ 
/home/youssef/workspace/ofbiz/framework/webapp/src/org/ofbiz/webapp/event/CoreEvents.java
   (working copy)

@@ -18,6 +18,9 @@

  
*******************************************************************************/

 package org.ofbiz.webapp.event;

 

+import static org.ofbiz.base.util.UtilGenerics.checkCollection;

+import static org.ofbiz.base.util.UtilGenerics.checkMap;

+

 import java.io.File;

 import java.io.FileInputStream;

 import java.io.FileNotFoundException;

@@ -28,7 +31,6 @@

 import java.util.Iterator;

 import java.util.Locale;

 import java.util.Map;

-import java.util.Set;

 import java.util.TimeZone;

 

 import javax.servlet.http.HttpServletRequest;

@@ -38,8 +40,6 @@

 import javolution.util.FastMap;

 

 import org.ofbiz.base.util.Debug;

-import static org.ofbiz.base.util.UtilGenerics.checkCollection;

-import static org.ofbiz.base.util.UtilGenerics.checkMap;

 import org.ofbiz.base.util.UtilHttp;

 import org.ofbiz.base.util.UtilProperties;

 import org.ofbiz.base.util.UtilValidate;

@@ -54,6 +54,7 @@

 import org.ofbiz.service.ModelService;

 import org.ofbiz.service.ServiceDispatcher;

 import org.ofbiz.service.calendar.RecurrenceRule;

+import org.ofbiz.webapp.control.LoginWorker;

 import org.ofbiz.webapp.control.RequestHandler;

 

 /**

@@ -104,12 +105,54 @@

         String delegatorName = request.getParameter("delegator");

         Security security = (Security) request.getAttribute("security");

         Locale locale = UtilHttp.getLocale(request);

+        GenericDelegator delegator=null;

+        //Begin multi-delegator Specific : search for delegatorName into 
session information

+        if("".equals(UtilProperties.getPropertyValue("security.properties", 
"multi.delegator"))){

+            String externalKey = 
request.getParameter(LoginWorker.EXTERNAL_LOGIN_KEY_ATTR);

+            if (externalKey != null) {

+                GenericValue userLogin = (GenericValue) 
LoginWorker.externalLoginKeys.get(externalKey);

+                if (userLogin != null && delegatorName==null) {

+                    delegatorName = userLogin.getString("delegator");

+                }

+            }

+

+            if(delegatorName==null){

+                delegatorName = (String) 
request.getSession().getAttribute("delegatorName");

+            }

+            if (delegatorName == null) {

+                String errMsg = 
UtilProperties.getMessage(CoreEvents.err_resource, 
"coreEvents.delegator_not_passed", locale);

+                request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg);

+                return "error";

+            }

 

-        if (!security.hasPermission("ENTITY_MAINT", request.getSession())) {

-            String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, 
"coreEvents.not_authorized_use_fct", locale);

-            request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg);

-            return "error";

+            delegator = GenericDelegator.getGenericDelegator(delegatorName);

+

+            if (delegator == null) {

+                String errMsg = 
UtilProperties.getMessage(CoreEvents.err_resource, 
"coreEvents.no_delegator_name_defined", locale);

+                request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg);

+                return "error";

+            }

+        //set the secruity delegator to precise the database against wich the 
user permissions are checked

+            security.setDelegator(delegator);

+            if (!security.hasPermission("ENTITY_MAINT", request.getSession())) 
{

+                String errMsg = 
UtilProperties.getMessage(CoreEvents.err_resource, 
"coreEvents.not_authorized_use_fct", locale);

+                request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg);

+                return "error";

+            }

+        }

+        //End multi-delegator specific

+        else{

+

+            if (!security.hasPermission("ENTITY_MAINT", request.getSession())) 
{

+                String errMsg = 
UtilProperties.getMessage(CoreEvents.err_resource, 
"coreEvents.not_authorized_use_fct", locale);

+                request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg);

+                return "error";

+            }

+

+

+           delegator = GenericDelegator.getGenericDelegator(delegatorName);

         }

+

         if (delegatorName == null) {

             String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, 
"coreEvents.delegator_not_passed", locale);

             request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg);

@@ -115,15 +158,6 @@

             request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg);

             return "error";

         }

-

-        GenericDelegator delegator = 
GenericDelegator.getGenericDelegator(delegatorName);

-

-        if (delegator == null) {

-            String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, 
"coreEvents.no_delegator_name_defined", locale);

-            request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg);

-            return "error";

-        }

-

         // now change the dispatcher to use this delegator

         LocalDispatcher dispatcher = (LocalDispatcher) 
request.getAttribute("dispatcher");

         DispatchContext dctx = dispatcher.getDispatchContext();

@@ -144,7 +178,14 @@

 

         request.getSession().setAttribute("delegator", delegator);

         request.getSession().setAttribute("dispatcher", dispatcher);

-

+        dispatcher.getJobManager().setDelegator(delegator);

+        dispatcher.setDelegator(delegator);

+        request.getSession().setAttribute("delegatorName", 
delegator.getDelegatorName());

+        request.setAttribute("delegator", delegator);

+        request.setAttribute("delegatorName", delegator.getDelegatorName());

+        request.setAttribute("dispatcher", dispatcher);

+        request.setAttribute("security", security);

+        

         return "success";

     }

 

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to