details: https://code.openbravo.com/erp/devel/pi/rev/1c428b6ceb65 changeset: 27037:1c428b6ceb65 user: Alvaro Ferraz <alvaro.ferraz <at> openbravo.com> date: Fri Jun 05 15:21:15 2015 +0200 summary: Fixes issue 30057: Org Access added automatically to automatic roles
Org Access will be automatically created when creating a new no manual role following this rule: System user level: * organization with Organization Administrator flag = N. Client user level: * organization with Organization Administrator flag = N and all the rest of organizations of the client with Organization Administrator flag = Y. Client+Organization user level: * organization with Organization Administrator flag = N and all the rest of organizations of the client with Organization Administrator flag = Y. Organization user level: all the rest of organizations of the client (excluding * organization) with Organization Administrator flag = Y. details: https://code.openbravo.com/erp/devel/pi/rev/d7e8168bd47b changeset: 27038:d7e8168bd47b user: Víctor Martínez Romanos <victor.martinez <at> openbravo.com> date: Thu Jun 25 18:56:49 2015 +0200 summary: Related to issue 30057: code review preview Removed flushes from RoleEventHandler because they create conflicts. As a consequence we lose the ability to clear the session, but this shouldn't be a problem because it's very unlikely to have environments with great amount of organizations. In RoleEventHandler we only create access to * organization when access level is Client (before this changeset it created also records for other organizations and it is useless). Reverted changes for Initial Client/Organization Setup. RoleEventHandler won't do anything if executed from an Initial Client/Organization Setup (this change is a way to isolate the problems reported at #30253) Added log4j to RoleEventHandler. Removed admin mode from RoleEventHandler because it's not needed (the user always has access t the entities involved in the process). diffstat: src/org/openbravo/event/RoleEventHandler.java | 173 ++++++++++++++++++++++++++ 1 files changed, 173 insertions(+), 0 deletions(-) diffs (178 lines): diff -r 946d4d023912 -r d7e8168bd47b src/org/openbravo/event/RoleEventHandler.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/openbravo/event/RoleEventHandler.java Thu Jun 25 18:56:49 2015 +0200 @@ -0,0 +1,173 @@ +/* + ************************************************************************* + * The contents of this file are subject to the Openbravo Public License + * Version 1.1 (the "License"), being the Mozilla Public License + * Version 1.1 with a permitted attribution clause; you may not use this + * file except in compliance with the License. You may obtain a copy of + * the License at http://www.openbravo.com/legal/license.html + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the + * License for the specific language governing rights and limitations + * under the License. + * The Original Code is Openbravo ERP. + * The Initial Developer of the Original Code is Openbravo SLU + * All portions are Copyright (C) 2015 Openbravo SLU + * All Rights Reserved. + * Contributor(s): ______________________________________. + ************************************************************************ + */ +package org.openbravo.event; + +import java.util.ArrayList; +import java.util.List; + +import javax.enterprise.event.Observes; + +import org.apache.commons.lang.StringUtils; +import org.hibernate.ScrollMode; +import org.hibernate.ScrollableResults; +import org.hibernate.criterion.Restrictions; +import org.openbravo.base.model.Entity; +import org.openbravo.base.model.ModelProvider; +import org.openbravo.base.model.Property; +import org.openbravo.base.provider.OBProvider; +import org.openbravo.client.kernel.event.EntityNewEvent; +import org.openbravo.client.kernel.event.EntityPersistenceEventObserver; +import org.openbravo.dal.service.OBCriteria; +import org.openbravo.dal.service.OBDal; +import org.openbravo.model.ad.access.Role; +import org.openbravo.model.ad.access.RoleOrganization; +import org.openbravo.model.common.enterprise.Organization; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RoleEventHandler extends EntityPersistenceEventObserver { + private static final String InitialOrgSetup_CLASSNAME = "org.openbravo.erpCommon.businessUtility.InitialOrgSetup"; + private static final String InitialClientSetup_CLASSNAME = "org.openbravo.erpCommon.businessUtility.InitialClientSetup"; + + private static Entity[] entities = { ModelProvider.getInstance().getEntity(Role.ENTITY_NAME) }; + + protected Logger logger = LoggerFactory.getLogger(RoleEventHandler.class); + + @Override + protected Entity[] getObservedEntities() { + return entities; + } + + public void onNew(@Observes + EntityNewEvent event) { + if (!isValidEvent(event)) { + return; + } + + final Entity roleEntity = ModelProvider.getInstance().getEntity(Role.class); + final Property roleProperty = roleEntity.getProperty(Role.PROPERTY_ADROLEORGANIZATIONLIST); + final Role role = (Role) event.getTargetInstance(); + + populateOrgAccess(event, role, roleProperty); + } + + /** + * Creates the necessary Org Access records only when the role is set Manual=N and when we don't + * come from the Initial Client/Organization setup + */ + private void populateOrgAccess(EntityNewEvent event, Role role, Property roleProperty) { + // Create org access for new automatic role + try { + if (!role.isManual() && !isComingFromInitialClientOrganizationSetup()) { + List<RoleOrganization> roleOrganizationList = getRoleOrganizationList(role); + @SuppressWarnings("unchecked") + final List<Object> roleOrganizations = (List<Object>) event.getCurrentState(roleProperty); + roleOrganizations.addAll(roleOrganizationList); + } + } catch (Exception e) { + logger + .error("Error in RoleEventHandler while inserting Org Access to role " + role.getName()); + } + } + + // Get org access list + private List<RoleOrganization> getRoleOrganizationList(Role role) throws Exception { + List<RoleOrganization> roleOrganizationList = new ArrayList<RoleOrganization>(); + + // Client or System level: Only * [isOrgAdmin=N] + if (StringUtils.equals(role.getUserLevel(), " C") + || StringUtils.equals(role.getUserLevel(), "S")) { + roleOrganizationList.add(getRoleOrganization(role, + OBDal.getInstance().get(Organization.class, "0"), false)); + logger.debug("Added organization * to role " + role.getName()); + } + + // Client/Organization level: * [isOrgAdmin=N], other Orgs (but *) [isOrgAdmin=Y] + else if (StringUtils.equals(role.getUserLevel(), " CO")) { + roleOrganizationList.add(getRoleOrganization(role, + OBDal.getInstance().get(Organization.class, "0"), false)); + logger.debug("Added organization * to role " + role.getName()); + + OBCriteria<Organization> criteria = OBDal.getInstance().createCriteria(Organization.class); + criteria.add(Restrictions.eq(Organization.PROPERTY_CLIENT, role.getClient())); + criteria.add(Restrictions.ne(Organization.PROPERTY_ID, "0")); + ScrollableResults scroll = criteria.scroll(ScrollMode.FORWARD_ONLY); + try { + while (scroll.next()) { + final Organization organization = (Organization) scroll.get()[0]; + roleOrganizationList.add(getRoleOrganization(role, organization, true)); + logger.debug("Added organization " + organization.getName() + " to role " + + role.getName()); + } + } finally { + scroll.close(); + } + } + + // Organization level: Orgs (but *) [isOrgAdmin=Y] + else if (StringUtils.equals(role.getUserLevel(), " O")) { + OBCriteria<Organization> criteria = OBDal.getInstance().createCriteria(Organization.class); + criteria.add(Restrictions.eq(Organization.PROPERTY_CLIENT, role.getClient())); + ScrollableResults scroll = criteria.scroll(ScrollMode.FORWARD_ONLY); + try { + while (scroll.next()) { + final Organization organization = (Organization) scroll.get()[0]; + roleOrganizationList.add(getRoleOrganization(role, organization, true)); + logger.debug("Added organization " + organization.getName() + " to role " + + role.getName()); + } + } finally { + scroll.close(); + } + } + + return roleOrganizationList; + } + + // Get org access + private RoleOrganization getRoleOrganization(Role role, Organization orgProvided, + boolean isOrgAdmin) throws Exception { + final RoleOrganization newRoleOrganization = OBProvider.getInstance().get( + RoleOrganization.class); + newRoleOrganization.setClient(role.getClient()); + newRoleOrganization.setOrganization(orgProvided); + newRoleOrganization.setRole(role); + newRoleOrganization.setOrgAdmin(isOrgAdmin); + return newRoleOrganization; + } + + /** + * Returns true if the Initial Client/Organization Setup is in the stack trace + */ + private boolean isComingFromInitialClientOrganizationSetup() { + boolean comeFrom_ICS_IOS = false; + for (final StackTraceElement ste : Thread.currentThread().getStackTrace()) { + final String clazz = ste.getClassName(); + if (StringUtils.equals(clazz, InitialOrgSetup_CLASSNAME) + || StringUtils.equals(clazz, InitialClientSetup_CLASSNAME)) { + comeFrom_ICS_IOS = true; + logger + .debug("Coming from Initial Client/Organization Setup. RoleEventHandler will not insert Org Access records"); + break; + } + } + return comeFrom_ICS_IOS; + } + +} \ No newline at end of file ------------------------------------------------------------------------------ Monitor 25 network devices or servers for free with OpManager! OpManager is web-based network management software that monitors network devices and physical & virtual servers, alerts via email & sms for fault. Monitor 25 devices for free with no restriction. Download now http://ad.doubleclick.net/ddm/clk/292181274;119417398;o _______________________________________________ Openbravo-commits mailing list Openbravo-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbravo-commits