From 03c6187fde7d014615116a0addfe3a72168098b6 Mon Sep 17 00:00:00 2001 From: hengsin Date: Wed, 2 Aug 2023 01:14:26 +0800 Subject: [PATCH] IDEMPIERE-5813 Change Role must close session like Logout does (#1959) * IDEMPIERE-5813 Change Role must close session like Logout does * IDEMPIERE-5813 Change Role must close session like Logout does --- .../src/org/compiere/util/Env.java | 2 ++ .../org/adempiere/webui/AdempiereWebUI.java | 10 +++++--- .../webui/panel/ValidateMFAPanel.java | 2 +- .../webui/session/SessionManager.java | 2 +- .../adempiere/webui/util/DesktopWatchDog.java | 25 +++++++++++++++++++ 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/util/Env.java b/org.adempiere.base/src/org/compiere/util/Env.java index bd784ef55a..bbda1bdd03 100644 --- a/org.adempiere.base/src/org/compiere/util/Env.java +++ b/org.adempiere.base/src/org/compiere/util/Env.java @@ -127,6 +127,8 @@ public final class Env public static final String M_PRICELIST_ID = "#M_PriceList_ID"; public static final String M_PRODUCT_CATEGORY_ID = "#M_Product_Category_ID"; public static final String M_WAREHOUSE_ID = "#M_Warehouse_ID"; + /** Context for multi factor authentication */ + public static final String MFA_Registration_ID = "#MFA_Registration_ID"; /** Context for POS ID */ public static final String POS_ID = "#POS_ID"; public static final String R_STATUSCATEGORY_ID = "#R_StatusCategory_ID"; diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java index 1ab74281c0..a8361dc0d6 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java @@ -708,6 +708,7 @@ public class AdempiereWebUI extends Window implements EventListener, IWeb Env.setContext(properties, Env.CLIENT_INFO_ORIENTATION, clientInfo.orientation); Env.setContext(properties, Env.CLIENT_INFO_MOBILE, clientInfo.tablet); Env.setContext(properties, Env.CLIENT_INFO_TIME_ZONE, clientInfo.timeZone.getID()); + Env.setContext(properties, Env.MFA_Registration_ID, Env.getContext(Env.getCtx(), Env.MFA_Registration_ID)); Desktop desktop = Executions.getCurrent().getDesktop(); Locale locale = (Locale) desktop.getSession().getAttribute(Attributes.PREFERRED_LOCALE); @@ -715,12 +716,12 @@ public class AdempiereWebUI extends Window implements EventListener, IWeb Env.setContext(properties, SessionContextListener.SERVLET_SESSION_ID, httpRequest.getSession().getId()); if (Env.getCtx().get(ServerContextURLHandler.SERVER_CONTEXT_URL_HANDLER) != null) properties.put(ServerContextURLHandler.SERVER_CONTEXT_URL_HANDLER, Env.getCtx().get(ServerContextURLHandler.SERVER_CONTEXT_URL_HANDLER)); - + //desktop cleanup IDesktop appDesktop = getAppDeskop(); HttpSession session = httpRequest.getSession(); if (appDesktop != null) - appDesktop.logout(T -> {if (T) asyncChangeRole(session, locale, properties);}); + appDesktop.logout(T -> {if (T) asyncChangeRole(session, locale, properties, desktop);}); } /** @@ -728,8 +729,9 @@ public class AdempiereWebUI extends Window implements EventListener, IWeb * @param httpSession * @param locale * @param properties + * @param desktop */ - private void asyncChangeRole(HttpSession httpSession, Locale locale, Properties properties) { + private void asyncChangeRole(HttpSession httpSession, Locale locale, Properties properties, Desktop desktop) { //stop key listener if (keyListener != null) { keyListener.detach(); @@ -761,6 +763,8 @@ public class AdempiereWebUI extends Window implements EventListener, IWeb //show change role window and set new context for env and session onChangeRole(locale, properties); + + Executions.schedule(desktop, e -> DesktopWatchDog.removeOtherDesktopsInSession(desktop), new Event("onRemoveOtherDesktops")); } @Override diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/panel/ValidateMFAPanel.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/panel/ValidateMFAPanel.java index 4ccf57bb77..cb19719de0 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/panel/ValidateMFAPanel.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/panel/ValidateMFAPanel.java @@ -376,7 +376,7 @@ public class ValidateMFAPanel extends Window implements EventListener { PO.clearCrossTenantSafe(); } } - Env.setContext(m_ctx, "#MFA_Registration_ID", registrationId); + Env.setContext(m_ctx, Env.MFA_Registration_ID, registrationId); if (m_isClientDefined) { wndLogin.showRolePanel(m_userName, m_showRolePanel, m_clientsKNPairs, m_isClientDefined, true); diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionManager.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionManager.java index 60c746e5a9..1de9f24fb1 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionManager.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionManager.java @@ -43,7 +43,7 @@ public class SessionManager String adRoleId = Env.getContext(ctx, Env.AD_ROLE_ID); String adClientId = Env.getContext(ctx, Env.AD_CLIENT_ID); String adOrgId = Env.getContext(ctx, Env.AD_ORG_ID); - String mfaId = Env.getContext(ctx, "#MFA_Registration_ID"); + String mfaId = Env.getContext(ctx, Env.MFA_Registration_ID); return ( !"".equals(mfaId) && !"".equals(adOrgId) diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/util/DesktopWatchDog.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/util/DesktopWatchDog.java index 3b55e0aa9d..494991233c 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/util/DesktopWatchDog.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/util/DesktopWatchDog.java @@ -133,4 +133,29 @@ public class DesktopWatchDog { } } } + + /** + * Remove other desktops that share the same session with the pass in desktop parameter + * @param desktop + */ + public static void removeOtherDesktopsInSession(Desktop desktop) { + Iterator iterator = INSTANCE.desktops.iterator(); + while (iterator.hasNext()) { + DesktopEntry entry = iterator.next(); + if (entry.desktop == desktop) + continue; + if (entry.desktop.getSession() != desktop.getSession()) + continue; + + iterator.remove(); + try { + final WebApp wapp = desktop.getWebApp(); + final Session session = desktop.getSession(); + final DesktopCache desktopCache = ((WebAppCtrl) wapp).getDesktopCache(session); + desktopCache.removeDesktop(entry.desktop); + } catch (Throwable t) { + t.printStackTrace(); + } + } + } }