Author: hansbak
Date: Mon Nov 8 02:04:27 2010 New Revision: 1032444 URL: http://svn.apache.org/viewvc?rev=1032444&view=rev Log: Optionally alow the setting of the multitenant id from the incoming domainname which is also stored in the tenant entity Modified: ofbiz/trunk/framework/common/webcommon/login.ftl ofbiz/trunk/framework/entity/entitydef/entitymodel.xml ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/LoginWorker.java Modified: ofbiz/trunk/framework/common/webcommon/login.ftl URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/webcommon/login.ftl?rev=1032444&r1=1032443&r2=1032444&view=diff ============================================================================== --- ofbiz/trunk/framework/common/webcommon/login.ftl (original) +++ ofbiz/trunk/framework/common/webcommon/login.ftl Mon Nov 8 02:04:27 2010 @@ -42,11 +42,13 @@ under the License. <td class="label">${uiLabelMap.CommonPassword}</td> <td><input type="password" name="PASSWORD" value="" size="20"/></td> </tr> - <#if ("Y" == useMultitenant)> + <#if ("Y" == useMultitenant && !sessionAttributes.tenantId?exists) > <tr> <td class="label">${uiLabelMap.CommonTenantId}</td> <td><input type="text" name="tenantId" value="${parameters.tenantId?if_exists}" size="20"/></td> </tr> + <#elseif ("Y" == useMultitenant && sessionAttributes.tenantId?exists) > + <input type="hidden" name="tenantId" value="${sessionAttributes.tenantId?if_exists}"/> </#if> <tr> <td colspan="2" align="center"> Modified: ofbiz/trunk/framework/entity/entitydef/entitymodel.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/entitydef/entitymodel.xml?rev=1032444&r1=1032443&r2=1032444&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/entitydef/entitymodel.xml (original) +++ ofbiz/trunk/framework/entity/entitydef/entitymodel.xml Mon Nov 8 02:04:27 2010 @@ -66,6 +66,7 @@ under the License. <entity entity-name="Tenant" package-name="org.ofbiz.entity.tenant"> <field name="tenantId" type="id-ne"/> <field name="tenantName" type="name"/> + <field name="domainName" type="long-varchar"/> <field name="disabled" type="indicator"><description>Disabled if 'Y', defaults to 'N' (not disabled).</description></field> <prim-key field="tenantId"/> </entity> Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java?rev=1032444&r1=1032443&r2=1032444&view=diff ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java (original) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java Mon Nov 8 02:04:27 2010 @@ -20,6 +20,7 @@ package org.ofbiz.webapp.control; import java.io.IOException; import java.util.Enumeration; +import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; @@ -36,14 +37,19 @@ import org.ofbiz.base.util.StringUtil; import org.ofbiz.base.util.UtilGenerics; import org.ofbiz.base.util.UtilHttp; import org.ofbiz.base.util.UtilJ2eeCompat; +import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.UtilProperties; import org.ofbiz.base.util.UtilTimer; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.entity.Delegator; import org.ofbiz.entity.DelegatorFactory; import org.ofbiz.entity.GenericDelegator; +import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericValue; +import org.ofbiz.entity.condition.EntityCondition; import org.ofbiz.entity.transaction.GenericTransactionException; import org.ofbiz.entity.transaction.TransactionUtil; +import org.ofbiz.entity.util.EntityUtil; import org.ofbiz.security.Security; import org.ofbiz.security.authz.Authorization; import org.ofbiz.service.LocalDispatcher; @@ -220,6 +226,39 @@ public class ControlServlet extends Http String errorPage = null; try { + String useMultitenant = UtilProperties.getPropertyValue("general.properties", "multitenant"); + if ("Y".equals(useMultitenant) && UtilValidate.isEmpty(delegator.getDelegatorTenantId())) { + // get tenant delegator by domain name + try { + // if a domain name was specified for tenant, replace delegator with the new per-tenant delegator and set tenantId to session attribute + List<GenericValue> tenants = delegator.findList("Tenant", EntityCondition.makeCondition("domainName", request.getServerName()), null, UtilMisc.toList("-createdStamp"), null, false); + if (UtilValidate.isNotEmpty(tenants)) { + GenericValue tenant = EntityUtil.getFirst(tenants); + String tenantId = tenant.getString("tenantId"); + + // make that tenant active, setup a new delegator and a new dispatcher + String tenantDelegatorName = delegator.getDelegatorBaseName() + "#" + tenantId; + + // after this line the delegator is replaced with the new per-tenant delegator + delegator = DelegatorFactory.getDelegator(tenantDelegatorName); + session.setAttribute("tenantId", tenantId); + session.setAttribute("delegatorName", tenantDelegatorName); + } + } catch (GenericEntityException e) { + String errMsg = "Error getting tenant by domain name: " + request.getServerName(); + Debug.logError(e, errMsg, module); + throw new RequestHandlerException(errMsg, e); + } + } + if ("Y".equals(useMultitenant) && UtilValidate.isNotEmpty(delegator.getDelegatorTenantId())) { + // re-make dispatcher from tenant delegator and change delegator of security to use tanent delegator + dispatcher = ContextFilter.makeWebappDispatcher(session.getServletContext(), delegator); + security.setDelegator(delegator); + + request.setAttribute("delegator", delegator); + request.setAttribute("dispatcher", dispatcher); + } + // the ServerHitBin call for the event is done inside the doRequest method requestHandler.doRequest(request, response, null, userLogin, delegator); } catch (RequestHandlerException e) { Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/LoginWorker.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/LoginWorker.java?rev=1032444&r1=1032443&r2=1032444&view=diff ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/LoginWorker.java (original) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/LoginWorker.java Mon Nov 8 02:04:27 2010 @@ -371,11 +371,19 @@ public class LoginWorker { ServletContext servletContext = session.getServletContext(); // make that tenant active, setup a new delegator and a new dispatcher - String delegatorName = delegator.getDelegatorName() + "#" + tenantId; + String delegatorName = delegator.getDelegatorBaseName() + "#" + tenantId; - // after this line the delegator is replaced with the new per-tenant delegator - delegator = DelegatorFactory.getDelegator(delegatorName); - dispatcher = ContextFilter.makeWebappDispatcher(servletContext, delegator); + try { + // after this line the delegator is replaced with the new per-tenant delegator + delegator = DelegatorFactory.getDelegator(delegatorName); + dispatcher = ContextFilter.makeWebappDispatcher(servletContext, delegator); + } catch (NullPointerException e) { + Debug.logError(e, "Error getting tenant delegator", module); + Map<String, String> messageMap = UtilMisc.toMap("errorMessage", "Tenant [" + tenantId + "] not found..."); + String errMsg = UtilProperties.getMessage(resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); + request.setAttribute("_ERROR_MESSAGE_", errMsg); + return "error"; + } // NOTE: these will be local for now and set in the request and session later, after we've verified that the user setupNewDelegatorEtc = true; |
Free forum by Nabble | Edit this page |