diff --git a/applications/party/template/party/profileblocks/UserLogin.ftl b/applications/party/template/party/profileblocks/UserLogin.ftl index bee922f209..b1a197916b 100644 --- a/applications/party/template/party/profileblocks/UserLogin.ftl +++ b/applications/party/template/party/profileblocks/UserLogin.ftl @@ -53,6 +53,9 @@ under the License. <#if security.hasEntityPermission("SECURITY", "_VIEW", session)> ${uiLabelMap.SecurityGroups} + <#if security.hasEntityPermission("IMPERSONATE", "_ADMIN", session)> + ${uiLabelMap.CommonImpersonate} + diff --git a/applications/party/template/party/profileblocks/Visits.ftl b/applications/party/template/party/profileblocks/Visits.ftl index 3f2486bc03..54f49fb5f8 100644 --- a/applications/party/template/party/profileblocks/Visits.ftl +++ b/applications/party/template/party/profileblocks/Visits.ftl @@ -31,6 +31,7 @@ under the License. ${uiLabelMap.PartyVisitId} ${uiLabelMap.PartyUserLogin} + ${uiLabelMap.CommonImpersonateUserLogin} ${uiLabelMap.PartyNewUser} ${uiLabelMap.PartyWebApp} ${uiLabelMap.PartyClientIP} @@ -39,11 +40,16 @@ under the License. <#list visits as visitObj> <#if (visitObj_index > 4)><#break> + <#assign userLoginHistory = EntityQuery.use(delegator).from("UserLoginHistory").where('visitId',visitObj.visitId!).queryFirst()!> + <#if userLoginHistory??> + <#assign impersonateUserLoginId = userLoginHistory.originUserLoginId!> + ${visitObj.visitId!} ${visitObj.userLoginId!} + ${impersonateUserLoginId!} ${visitObj.userCreated!} ${visitObj.webappName!} ${visitObj.clientIpAddress!} diff --git a/applications/party/widget/partymgr/PartyMenus.xml b/applications/party/widget/partymgr/PartyMenus.xml index 603a13ec14..c45ca6b77b 100644 --- a/applications/party/widget/partymgr/PartyMenus.xml +++ b/applications/party/widget/partymgr/PartyMenus.xml @@ -772,5 +772,11 @@ - + + + + + + + diff --git a/applications/party/widget/partymgr/PartyVisitForms.xml b/applications/party/widget/partymgr/PartyVisitForms.xml index c4120db184..a6f9d234fc 100644 --- a/applications/party/widget/partymgr/PartyVisitForms.xml +++ b/applications/party/widget/partymgr/PartyVisitForms.xml @@ -41,7 +41,6 @@ under the License. odd-row-style="alternate-row" header-row-style="header-row-2" default-table-style="basic-table hover-bar"> - @@ -51,7 +50,15 @@ under the License. - + + + + + + + + + @@ -64,6 +71,7 @@ under the License. + diff --git a/docs/asciidoc/developer-manual.adoc b/docs/asciidoc/developer-manual.adoc index 4e1231baa5..07c16d4b6b 100644 --- a/docs/asciidoc/developer-manual.adoc +++ b/docs/asciidoc/developer-manual.adoc @@ -269,6 +269,6 @@ displays search results. == Deployment -== Security +include::../../framework/security/src/doc/asciidoc/security.adoc[leveloffset=+1] == Appendices diff --git a/framework/common/config/CommonUiLabels.xml b/framework/common/config/CommonUiLabels.xml index 45ef7cd00a..581634d5ce 100644 --- a/framework/common/config/CommonUiLabels.xml +++ b/framework/common/config/CommonUiLabels.xml @@ -5517,6 +5517,22 @@ 图片 圖片 + + Impersonate + Incarner + + + User impersonated + Utilisateur incarné + + + Impersonation in process + Incarnation en cours + + + Stop impersonation + Arrêter l'incarnation + Import 导入 @@ -8231,6 +8247,13 @@ 机构图标 機構商標 + + Herkunft Benutzeranmeldung ID + Origin User Login ID + Código de usuario de origen + Identifiant de connexion d'origine + Codice utente di origine + أخرى Jiný(á) @@ -14885,6 +14908,10 @@ 编辑数据源类型 編輯資料源類型 + + Impersonated + Substitué + قائمة مصادر المعلومات Seznam datových zdrojů diff --git a/framework/common/config/SecurityextUiLabels.xml b/framework/common/config/SecurityextUiLabels.xml index de8e9850cd..d69ae2bc86 100644 --- a/framework/common/config/SecurityextUiLabels.xml +++ b/framework/common/config/SecurityextUiLabels.xml @@ -107,6 +107,26 @@ 登录时发生下列错误:${errorMessage} 登入時發生錯誤: ${errorMessage}. + + You can't impersonate yourself ... What are you trying to do ? + Vous ne pouvez pas vous incarner vous même ... Qu'essayez-vous donc de faire ? + + + You cannot impersonate a login with more permission than you have : ${missingPermissions} + Vous ne pouvez pas incarner un utilisateur avec un niveau de permission supérieur au vôtre : ${missingPermissions} + + + You cannot impersonate a login with ADMIN permission + Vous ne pouvez pas incarner un utilisateur avec un niveau de permission ADMIN + + + Impersonation feature is disabled, please check security configuration + La fonctionnalité d'incarnation est désactivée, merci de vérifier la configuration sécurité + + + Impersonation of your user is in process by ${originUserLoginId} + Incarnation de votre utilisateur en cours par ${originUserLoginId} + Ein neues Passwort wurde erzeugt und Ihnen per E-Mail zugeschickt. Überprüfen Sie bitte Ihren E-Mail-Eingang. A new password has been created and sent to you. Please check your Email. @@ -245,6 +265,14 @@ 配置出错;请与客户服务联系。 設定有問題: 請聯絡客服. + + You already have an impersonation in process. Please leave it before continue + Vous avez déjà une incarnation en cours, veuillez mettre fin à celle-ci avant de continuer + + + Problem getting User Login Security Question record. + Problème durant la lecture de votre question de sécurité + You cannot login to this tenant No puede conectarse con esta organización @@ -865,4 +893,4 @@ 将重新启用 ${reEnableTime}。 將重新啟用 ${reEnableTime}. - \ No newline at end of file + diff --git a/framework/common/data/CommonSecurityGroupDemoData.xml b/framework/common/data/CommonSecurityGroupDemoData.xml index 62b91d67ef..0f91c425c5 100644 --- a/framework/common/data/CommonSecurityGroupDemoData.xml +++ b/framework/common/data/CommonSecurityGroupDemoData.xml @@ -40,4 +40,9 @@ + + + + + diff --git a/framework/common/data/CommonSecurityPermissionSeedData.xml b/framework/common/data/CommonSecurityPermissionSeedData.xml index 5ea573fcfd..0d69d81bc4 100644 --- a/framework/common/data/CommonSecurityPermissionSeedData.xml +++ b/framework/common/data/CommonSecurityPermissionSeedData.xml @@ -36,12 +36,15 @@ - + + + - + + diff --git a/framework/common/data/CommonTypeData.xml b/framework/common/data/CommonTypeData.xml index 658ca1cf55..9ae9fc7e38 100644 --- a/framework/common/data/CommonTypeData.xml +++ b/framework/common/data/CommonTypeData.xml @@ -113,6 +113,7 @@ under the License. + diff --git a/framework/common/servicedef/services.xml b/framework/common/servicedef/services.xml index 348bebaba9..083f81d5d5 100644 --- a/framework/common/servicedef/services.xml +++ b/framework/common/servicedef/services.xml @@ -381,6 +381,18 @@ under the License. + + Used to Automatically Authenticate a username/password; create a UserLogin object + + + + + + + + + + Create a UserLogin diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/login/LoginServices.java b/framework/common/src/main/java/org/apache/ofbiz/common/login/LoginServices.java index 43e50d95b1..bca0c8397b 100644 --- a/framework/common/src/main/java/org/apache/ofbiz/common/login/LoginServices.java +++ b/framework/common/src/main/java/org/apache/ofbiz/common/login/LoginServices.java @@ -20,6 +20,7 @@ package org.apache.ofbiz.common.login; import java.sql.Timestamp; +import java.util.Calendar; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; @@ -27,6 +28,7 @@ import java.util.Locale; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -53,6 +55,7 @@ import org.apache.ofbiz.entity.util.EntityListIterator; import org.apache.ofbiz.entity.util.EntityQuery; import org.apache.ofbiz.entity.util.EntityUtilProperties; import org.apache.ofbiz.security.Security; +import org.apache.ofbiz.security.SecurityUtil; import org.apache.ofbiz.service.DispatchContext; import org.apache.ofbiz.service.GenericServiceException; import org.apache.ofbiz.service.LocalDispatcher; @@ -435,6 +438,126 @@ public class LoginServices { return result; } + /** + * Login service to authenticate a username without password, storing history + * + * @return Map of results including (userLogin) GenericValue object + */ + public static Map userImpersonate(DispatchContext ctx, Map context) { + Locale locale = (Locale) context.get("locale"); + Delegator delegator = ctx.getDelegator(); + Map result = ServiceUtil.returnSuccess(); + + String userLoginIdToImpersonate = (String) context.get("userLoginIdToImpersonate"); + GenericValue originUserLogin = (GenericValue) context.get("userLogin"); + // get the visitId for the history entity + String visitId = (String) context.get("visitId"); + + if ("true".equalsIgnoreCase(EntityUtilProperties.getPropertyValue("security", "username.lowercase", delegator))) { + userLoginIdToImpersonate = userLoginIdToImpersonate.toLowerCase(); + } + + GenericValue userLogin; + try { + userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", userLoginIdToImpersonate).queryOne(); + } catch (GenericEntityException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + + // check impersonation controls + String errorMessage = checkImpersonationControls(delegator, originUserLogin, userLogin, locale); + if (errorMessage != null) { + return ServiceUtil.returnError(errorMessage); + } + + // return the UserLoginSession Map + Map userLoginSessionMap = LoginWorker.getUserLoginSession(userLogin); + if (userLoginSessionMap != null) { + result.put("userLoginSession", userLoginSessionMap); + } + + // grab the hasLoggedOut flag + boolean hasLoggedOut = "Y".equalsIgnoreCase(userLogin.getString("hasLoggedOut")); + if (hasLoggedOut || UtilValidate.isEmpty(userLogin.getString("hasLoggedOut"))) { + userLogin.set("hasLoggedOut", "N"); + try { + userLogin.store(); + } catch (GenericEntityException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + } + + //Log impersonation in UserLoginHistory + Map historyCreateMap = UtilMisc.toMap("userLoginId", userLoginIdToImpersonate); + historyCreateMap.put("visitId", visitId); + historyCreateMap.put("fromDate", UtilDateTime.nowTimestamp()); + historyCreateMap.put("successfulLogin", "Y"); + historyCreateMap.put("partyId", userLogin.get("partyId")); + historyCreateMap.put("originUserLoginId", originUserLogin.get("userLoginId")); + //End impersonation in one hour max + historyCreateMap.put("thruDate", UtilDateTime.adjustTimestamp(UtilDateTime.nowTimestamp(), Calendar.HOUR, 1)); + try { + delegator.create("UserLoginHistory", historyCreateMap); + } catch (GenericEntityException e) { + Debug.logError(e, module); + return ServiceUtil.returnError(e.getMessage()); + } + + result.put("userLogin", userLogin); + result.put("originUserLogin", originUserLogin); + return result; + } + + /** + * Return error message if a needed control has failed : + * userLoginToImpersonate must exist + * Impersonation have to be enabled + * Check userLoginIdToImpersonate is active, not Admin and not equals to userLogin + * Check userLogin has enough permission + * + * @param delegator + * @param userLogin + * @param userLoginToImpersonate + * @param locale + * @return + */ + private static String checkImpersonationControls(Delegator delegator, GenericValue userLogin, GenericValue userLoginToImpersonate, Locale locale) { + if (userLoginToImpersonate == null) { + return UtilProperties.getMessage(resource, "loginservices.username_missing", locale); + } + String userLoginId = userLogin.getString("userLoginId"); + String userLoginIdToImpersonate = userLoginToImpersonate.getString("userLoginId"); + + if (UtilProperties.getPropertyAsBoolean("security", "security.disable.impersonation", true)) { + return UtilProperties.getMessage(resource, "loginevents.impersonation_disabled", locale); + } + + if (!LoginWorker.isUserLoginActive(userLoginToImpersonate)) { + Map messageMap = UtilMisc.toMap("username", userLoginIdToImpersonate); + return UtilProperties.getMessage(resource, "loginservices.account_for_user_login_id_disabled", messageMap, locale); + } + + if (SecurityUtil.hasUserLoginAdminPermission(delegator, userLoginIdToImpersonate)) { + return UtilProperties.getMessage(resource, "loginevents.impersonate_notAdmin", locale); + } + + if (userLoginIdToImpersonate.equals(userLoginId)) { + return UtilProperties.getMessage(resource, "loginevents.impersonate_yourself", locale); + } + + //Cannot impersonate more privileged user + List missingNeededPermissions = SecurityUtil.hasUserLoginMorePermissionThan(delegator, userLoginId, userLoginIdToImpersonate); + if (UtilValidate.isNotEmpty(missingNeededPermissions)) { + String missingPermissionListString = missingNeededPermissions.stream().collect(Collectors.joining(", ")); + return UtilProperties.getMessage(resource, "loginevents.impersonate_notEnoughPermission", + UtilMisc.toMap("missingPermissions", missingPermissionListString), locale); + } + + return null; + } + public static void createUserLoginPasswordHistory(Delegator delegator,String userLoginId, String currentPassword) throws GenericEntityException{ int passwordChangeHistoryLimit = 0; try { diff --git a/framework/common/webcommon/WEB-INF/common-controller.xml b/framework/common/webcommon/WEB-INF/common-controller.xml index ed52c93241..b083c37b28 100644 --- a/framework/common/webcommon/WEB-INF/common-controller.xml +++ b/framework/common/webcommon/WEB-INF/common-controller.xml @@ -44,6 +44,7 @@ under the License. + @@ -53,6 +54,18 @@ under the License. + + + + + + + + + + + + @@ -315,6 +328,7 @@ under the License. + diff --git a/framework/common/widget/CommonScreens.xml b/framework/common/widget/CommonScreens.xml index b6e08707b1..1e6d31fd7b 100644 --- a/framework/common/widget/CommonScreens.xml +++ b/framework/common/widget/CommonScreens.xml @@ -229,6 +229,18 @@ under the License. + +
+ + + + + + + +
+
+
diff --git a/framework/security/config/security.properties b/framework/security/config/security.properties index 030ac41635..8d77f5cfc8 100644 --- a/framework/security/config/security.properties +++ b/framework/security/config/security.properties @@ -75,6 +75,11 @@ store.login.history=true store.login.history.on.service.auth=false store.login.history.incorrect.password=true +# -- disable impersonation +security.disable.impersonation=false +# -- if you want that an user cannot operate during an administrator impersonate his account. If true, it can be helpful for QA and dev site +security.login.authorised.during.impersonate=false + # -- should we encrypt (SHA Hash) the password? -- password.encrypt=true diff --git a/framework/security/data/SecurityGroupDemoData.xml b/framework/security/data/SecurityGroupDemoData.xml index a2605e8997..bd903bcf73 100644 --- a/framework/security/data/SecurityGroupDemoData.xml +++ b/framework/security/data/SecurityGroupDemoData.xml @@ -25,7 +25,8 @@ under the License. - + + diff --git a/framework/security/entitydef/entitymodel.xml b/framework/security/entitydef/entitymodel.xml index 1a056e868a..e13bc2f4d3 100644 --- a/framework/security/entitydef/entitymodel.xml +++ b/framework/security/entitydef/entitymodel.xml @@ -106,11 +106,15 @@ under the License. + + + + + + + + + + + + + + + + + + + + diff --git a/framework/security/src/doc/asciidoc/_include/security-impersonation.adoc b/framework/security/src/doc/asciidoc/_include/security-impersonation.adoc new file mode 100644 index 0000000000..a04d3e5144 --- /dev/null +++ b/framework/security/src/doc/asciidoc/_include/security-impersonation.adoc @@ -0,0 +1,127 @@ +//// +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. 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 under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +//// + += Impersonation +== What is Impersonation in Apache OFBiz +The Apache OFBiz Project +Release 17.12 + +:imagesdir: ../../themes/common-theme/webapp/images/img/ +ifdef::backend-pdf[] +:title-logo-image: image::OFBiz-Logo.svg[Apache OFBiz Logo, pdfwidth=4.25in, align=center] +:source-highlighter: rouge +endif::[] + +=== Introduction to User impersonation + +User Impersonation is a feature that offer a way to select a user login and impersonate it, i.e. see what the user could +see navigating through the application in his name. + +=== How do this work ? + +An authorized user _(see <> and <> section for configuration)_, can select a user +that will be impersonated. + +The impersonation start, if everything is well configured, in current application (partymgr for the demo). +Everything appears like if we were logged in with the userLoginId and the valid password (though we know nothing about it) + +The only thing showing that we currently are impersonating a user is the little bottom-right image : + +image::impersonate-ico.png[Impersonate icon, pdfwidth=0.5in, align=right] + +This icon indicates, when clicking on it, the user impersonated, and offer a way to depersonate. + +The impersonate period is stored for audit purpose, and if the impersonator forgot to depersonate, the period +is terminated _one hour_ after impersonation start. + +=== Security + +This feature can draw some concerns about security aspect. This paragraph will introduce every controls and properties +that have been implemented around the impersonation feature. + +[CAUTION] +These configuration steps are not to be neglected for a *production environment* since this feature offer a way to act + in place of another user. + +==== Properties + +The _security.properties_ file introduce two properties that control impersonation feature : + + +[source] +security.disable.impersonation = true + +This property, set by default to *true*, controls the activation of impersonation feature. If no configuration is done +any user trying to use impersonation will face an error message, indicating that the feature is disabled. + +To enable impersonation this property need to be set to *false* + + +[source] +security.login.authorised.during.impersonate = false + +This property controls the way impersonation occurred to the impersonated user : + +In default configuration, the impersonated user see nothing and can use the application without knowing that he is +currently impersonated. Several authorized user can impersonate a same login without any issue. + +[NOTE] +This configuration is intended for testing/QA environment allowing any authorized user to impersonate a login +to validate its configuration, test the application etc. + +Set to *true*, this configuration improve the control of the data generated by the impersonated user. Indeed, Only one +authorized user can impersonate a login at the same time, and during the impersonation process, the impersonated user +is unable to act within the application. + +Since the impersonation period is stored in database, the actions done by the +authorized user can be identified if there is the need to do so. +[NOTE] +This configuration is intended for production environment + + +==== Controls + +The permission:: + +First, to be able to use impersonation, a user need to possess _IMPERSONATE_ADMIN_ permissions. Demo data offer +_IMPERSONATION_ security group for this purpose. + +In demo data, _FULLADMIN_ security group also possess the permission. + + +Permission based user restriction:: + +An authorized user cannot impersonate any user. There are two main controls that will restrict the impersonation feature. + +Cannot impersonate Admin user::: + +It is impossible to impersonate a user that is granted any of the admin permission : + + "IMPERSONATE_ADMIN" + "ARTIFACT_INFO_VIEW" + "SERVICE_MAINT" + "ENTITY_MAINT" + "UTIL_CACHE_VIEW" + "UTIL_DEBUG_VIEW" + +Cannot impersonate more privileged user::: + +It is impossible to impersonate a user that has more permission than your user. Even if the missing persmission is +a minor one. + + diff --git a/framework/security/src/doc/asciidoc/security.adoc b/framework/security/src/doc/asciidoc/security.adoc new file mode 100644 index 0000000000..3341f96798 --- /dev/null +++ b/framework/security/src/doc/asciidoc/security.adoc @@ -0,0 +1,23 @@ +//// +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. 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 under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +//// + += Security + +include::_include/security-impersonation.adoc[leveloffset=+1] + diff --git a/framework/security/src/main/java/org/apache/ofbiz/security/SecurityUtil.java b/framework/security/src/main/java/org/apache/ofbiz/security/SecurityUtil.java new file mode 100644 index 0000000000..53403e801f --- /dev/null +++ b/framework/security/src/main/java/org/apache/ofbiz/security/SecurityUtil.java @@ -0,0 +1,122 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. 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 under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.apache.ofbiz.security; + + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import org.apache.ofbiz.base.util.Debug; +import org.apache.ofbiz.base.util.StringUtil; +import org.apache.ofbiz.base.util.UtilMisc; +import org.apache.ofbiz.base.util.UtilValidate; +import org.apache.ofbiz.entity.Delegator; +import org.apache.ofbiz.entity.GenericEntityException; +import org.apache.ofbiz.entity.condition.EntityCondition; +import org.apache.ofbiz.entity.condition.EntityOperator; +import org.apache.ofbiz.entity.util.EntityQuery; +import org.apache.ofbiz.entity.util.EntityUtil; + +/** + * A Security util. + */ +public final class SecurityUtil { + + public static final String module = SecurityUtil.class.getName(); + private static final List adminPermissions = UtilMisc.toList( + "IMPERSONATE_ADMIN", + "ARTIFACT_INFO_VIEW", + "SERVICE_MAINT", + "ENTITY_MAINT", + "UTIL_CACHE_VIEW", + "UTIL_DEBUG_VIEW"); + + /** + * Return true if given userLogin possess at least one of the adminPermission + * + * @param delegator + * @param userLoginId + * @return + */ + public static boolean hasUserLoginAdminPermission(Delegator delegator, String userLoginId) { + if (UtilValidate.isEmpty(userLoginId)) return false; + try { + return EntityQuery.use(delegator) + .from("UserLoginAndPermission") + .where(EntityCondition.makeCondition( + EntityCondition.makeCondition("userLoginId", userLoginId), + EntityCondition.makeCondition("permissionId", EntityOperator.IN, adminPermissions))) + .filterByDate("fromDate", "thruDate", "permissionFromDate", "permissionThruDate") + .queryCount() != 0; + } catch (GenericEntityException e) { + Debug.logError("Failed to resolve user permissions", module); + } + return false; + } + + /** + * Return the list of missing permission, if toUserLoginId has more permission thant userLoginId, emptyList either. + * + * @param delegator + * @param userLoginId + * @param toUserLoginId + * @return + */ + public static List hasUserLoginMorePermissionThan(Delegator delegator, String userLoginId, String toUserLoginId) { + ArrayList returnList = new ArrayList<>(); + if (UtilValidate.isEmpty(userLoginId) || UtilValidate.isEmpty(toUserLoginId)) return returnList; + List userLoginPermissionIds; + List toUserLoginPermissionIds; + try { + userLoginPermissionIds = EntityUtil.getFieldListFromEntityList( + EntityQuery.use(delegator) + .from("UserLoginAndPermission") + .where("userLoginId", userLoginId) + .filterByDate("fromDate", "thruDate", "permissionFromDate", "permissionThruDate") + .queryList(), "permissionId", true); + toUserLoginPermissionIds = EntityUtil.getFieldListFromEntityList( + EntityQuery.use(delegator) + .from("UserLoginAndPermission") + .where("userLoginId", toUserLoginId) + .filterByDate("fromDate", "thruDate", "permissionFromDate", "permissionThruDate") + .queryList(), "permissionId", true); + } catch (GenericEntityException e) { + Debug.logError("Failed to resolve user permissions", module); + return returnList; + } + + if (UtilValidate.isEmpty(userLoginPermissionIds)) return toUserLoginPermissionIds; + if (UtilValidate.isEmpty(toUserLoginPermissionIds)) return returnList; + + //Resolve all ADMIN permissions associated with the origin user + List adminPermissions = userLoginPermissionIds.stream() + .filter(perm -> perm.endsWith("_ADMIN")) + .map(perm -> StringUtil.replaceString(perm, "_ADMIN", "")) + .collect(Collectors.toList()); + + // if toUserLoginPermissionIds contains at least one permission that is not in admin permission or userLoginPermissionIds + // return the list of missing permission + return toUserLoginPermissionIds.stream() + .filter(perm -> + !userLoginPermissionIds.contains(perm) + && !adminPermissions.contains(perm.substring(0, perm.lastIndexOf("_")))) + .collect(Collectors.toList()); + } +} \ No newline at end of file diff --git a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/LoginWorker.java b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/LoginWorker.java index ca3515cfe7..2776c1c57d 100644 --- a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/LoginWorker.java +++ b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/LoginWorker.java @@ -26,8 +26,10 @@ import java.sql.Timestamp; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.ServiceLoader; import java.util.regex.Matcher; @@ -59,6 +61,7 @@ import org.apache.ofbiz.base.util.UtilValidate; import org.apache.ofbiz.entity.Delegator; import org.apache.ofbiz.entity.DelegatorFactory; import org.apache.ofbiz.entity.EntityCryptoException; +import org.apache.ofbiz.entity.GenericEntity; import org.apache.ofbiz.entity.GenericEntityException; import org.apache.ofbiz.entity.GenericValue; import org.apache.ofbiz.entity.condition.EntityCondition; @@ -215,6 +218,52 @@ public class LoginWorker { return userLogin; } + /** + * Return the active {@link GenericValue} of a current impersonation UserLoginHistory of current userLogin session, + * only if not the impersonator himself. + * + * @param request The HTTP request object for the current JSP or Servlet request. + * @param response The HTTP response object for the current JSP or Servlet request. + * @return GenericValue + */ + public static GenericValue checkImpersonationInProcess(HttpServletRequest request, HttpServletResponse response) { + HttpSession session = request.getSession(); + GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); + GenericValue originUserLogin = (GenericValue) session.getAttribute("originUserLogin"); + + //if originUserLogin is present, it is the impersonator session + if (originUserLogin != null) { + return null; + } + + //Check the existence of an enabled impersonation visit + GenericValue userLoginHistory = null; + if (userLogin != null) { + try { + userLoginHistory = EntityQuery.use(userLogin.getDelegator()) + .from("UserLoginHistory") + .where(EntityCondition.makeCondition("userLoginId", userLogin.get("userLoginId")), + EntityCondition.makeCondition("originUserLoginId", EntityOperator.NOT_EQUAL, null)) + .filterByDate() + .queryFirst(); + } catch (GenericEntityException e) { + Debug.logError(e, "impossible to resolve userLogin history", module); + } + } + if (userLoginHistory != null) { + List errorMessageList = UtilGenerics.checkList(request.getAttribute("_ERROR_MESSAGE_LIST")); + if (errorMessageList == null) { + errorMessageList = new LinkedList<>(); + request.setAttribute("_ERROR_MESSAGE_LIST_", errorMessageList); + } + HashMap messageMap = new HashMap<>(); + messageMap.putAll(userLoginHistory.getAllFields()); + String errMsg = UtilProperties.getMessage(resourceWebapp, "loginevents.impersonation_in_process", messageMap, UtilHttp.getLocale(request)); + errorMessageList.add(errMsg); + } + return userLoginHistory; + } + /** This WebEvent allows for java 'services' to hook into the login path. * This method loads all instances of {@link LoginCheck}, and calls the * {@link LoginCheck#associate} method. The first implementation to return @@ -315,6 +364,16 @@ public class LoginWorker { } } + //Allow loggingOut when impersonated + boolean isLoggingOut = "logout".equals(RequestHandler.getRequestUri(request.getPathInfo())); + //Check if the user has an impersonation in process + boolean authoriseLoginDuringImpersonate = EntityUtilProperties.propertyValueEquals("security", "security.login.authorised.during.impersonate", "true"); + if (!isLoggingOut && !authoriseLoginDuringImpersonate && checkImpersonationInProcess(request, response) != null) { + //remove error message that will be displayed in impersonated status screen + request.removeAttribute("_ERROR_MESSAGE_LIST_"); + return "impersonated"; + } + return "success"; } @@ -533,6 +592,156 @@ public class LoginWorker { } } + /** + * An HTTP WebEvent handler to impersonate a given userLogin without using password. This should run before the security check. + * + * @param request The HTTP request object for the current JSP or Servlet request. + * @param response The HTTP response object for the current JSP or Servlet request. + * @return Return a boolean which specifies whether or not the calling Servlet or + * JSP should generate its own content. This allows an event to override the default content. + */ + public static String impersonateLogin(HttpServletRequest request, HttpServletResponse response) { + HttpSession session = request.getSession(); + Delegator delegator = (Delegator) request.getAttribute("delegator"); + String userLoginIdToImpersonate = request.getParameter("userLoginIdToImpersonate"); + GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); + LocalDispatcher dispatcher; + + if (UtilProperties.getPropertyAsBoolean("security","security.disable.impersonation", true)) { + String errMsg = UtilProperties.getMessage(resourceWebapp, "loginevents.impersonation_disabled", UtilHttp.getLocale(request)); + request.setAttribute("_ERROR_MESSAGE_", errMsg); + return "error"; + } + + //Check if user has impersonate permission + Security security = (Security) request.getAttribute("security"); + if (!security.hasEntityPermission("IMPERSONATE", "_ADMIN", userLogin)) { + String errMsg = UtilProperties.getMessage(resourceWebapp, "loginevents.unable_to_login_this_application", UtilHttp.getLocale(request)); + request.setAttribute("_ERROR_MESSAGE_", errMsg); + return "error"; + } + + List errMsgList = new LinkedList<>(); + if (UtilValidate.isNotEmpty(session.getAttribute("originUserLogin"))) { + errMsgList.add(UtilProperties.getMessage(resourceWebapp, "loginevents.origin_username_is_present", UtilHttp.getLocale(request))); + } + if (UtilValidate.isEmpty(userLoginIdToImpersonate)) { + errMsgList.add(UtilProperties.getMessage(resourceWebapp, "loginevents.username_was_empty_reenter", UtilHttp.getLocale(request))); + } + + try { + GenericValue userLoginToImpersonate = delegator.findOne("UserLogin", false, "userLoginId", userLoginIdToImpersonate); + if (!hasBasePermission(userLoginToImpersonate, request)) { + errMsgList.add(UtilProperties.getMessage(resourceWebapp, "loginevents.unable_to_login_this_application", UtilHttp.getLocale(request))); + } + } catch (GenericEntityException e) { + String errMsg ="Error impersonating the userLoginId" + userLoginIdToImpersonate; + Debug.logError(e, errMsg, module); + errMsgList.add(errMsg); + request.setAttribute("_ERROR_MESSAGE_LIST_", errMsgList); + return "error"; + } + if (!errMsgList.isEmpty()) { + request.setAttribute("_ERROR_MESSAGE_LIST_", errMsgList); + return "error"; + } + + ServletContext servletContext = session.getServletContext(); + + Debug.logInfo("Setting default delegator", module); + String delegatorName = delegator.getDelegatorBaseName(); + delegator = DelegatorFactory.getDelegator(delegatorName); + dispatcher = WebAppUtil.makeWebappDispatcher(servletContext, delegator); + + Map result; + try { + // get the visit id to pass to the userLogin for history + String visitId = VisitHandler.getVisitId(session); + result = dispatcher.runSync("userImpersonate", + UtilMisc.toMap("userLoginIdToImpersonate", userLoginIdToImpersonate, + "userLogin", userLogin,"visitId", visitId, "locale", UtilHttp.getLocale(request))); + } catch (GenericServiceException e) { + Debug.logError(e, "Error calling userImpersonate service", module); + Map messageMap = UtilMisc.toMap("errorMessage", e.getMessage()); + String errMsg = UtilProperties.getMessage(resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); + request.setAttribute("_ERROR_MESSAGE_", errMsg); + return "error"; + } + + if (ModelService.RESPOND_SUCCESS.equals(result.get(ModelService.RESPONSE_MESSAGE))) { + userLogin = (GenericValue) result.get("userLogin"); + GenericValue originUserLogin = (GenericValue) result.get("originUserLogin"); + + Map userLoginSession = checkMap(result.get("userLoginSession"), String.class, Object.class); + + // check on JavaScriptEnabled + String javaScriptEnabled = "N"; + if ("Y".equals(request.getParameter("JavaScriptEnabled"))) { + javaScriptEnabled = "Y"; + } + try { + dispatcher.runSync("setUserPreference", UtilMisc.toMap("userPrefTypeId", "javaScriptEnabled", + "userPrefGroupTypeId", "GLOBAL_PREFERENCES", "userPrefValue", javaScriptEnabled, "userLogin", userLogin)); + } catch (GenericServiceException e) { + Debug.logError(e, "Error setting user preference", module); + } + + //add originUserLogin in session + session.setAttribute("originUserLogin", originUserLogin); + // finally do the main login routine to set everything else up in the session, etc + return doMainLogin(request, response, userLogin, userLoginSession); + } else { + Map messageMap = UtilMisc.toMap("errorMessage", result.get(ModelService.ERROR_MESSAGE)); + String errMsg = UtilProperties.getMessage(resourceWebapp, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request)); + request.setAttribute("_ERROR_MESSAGE_", errMsg); + return "error"; + } + } + + /** + * An HTTP WebEvent handler to reverse an impersonate login. + * + * @param request The HTTP request object for the current JSP or Servlet request. + * @param response The HTTP response object for the current JSP or Servlet request. + * @return Return a boolean which specifies whether or not the calling Servlet or + * JSP should generate its own content. This allows an event to override the default content. + */ + public static String depersonateLogin(HttpServletRequest request, HttpServletResponse response) { + HttpSession session = request.getSession(); + GenericValue originUserLogin = (GenericValue) session.getAttribute("originUserLogin"); + session.removeAttribute("originUserLogin"); + + List errMsgList = new LinkedList<>(); + if (null == originUserLogin) { + errMsgList.add(UtilProperties.getMessage(resourceWebapp, "loginevents.username_was_empty_reenter", UtilHttp.getLocale(request))); + } + if (!errMsgList.isEmpty()) { + request.setAttribute("_ERROR_MESSAGE_LIST_", errMsgList); + return "error"; + } + + //update the userLogin history, only one impersonation of this user can be active at the same time + EntityCondition conditions = EntityCondition.makeCondition( + EntityCondition.makeCondition("userLoginId", ((GenericValue) session.getAttribute("userLogin")).get("userLoginId")), + EntityCondition.makeCondition("originUserLoginId", originUserLogin.get("userLoginId")), + EntityUtil.getFilterByDateExpr()); + try { + //check impersonation process existence to avoid depersonation abuse + if (EntityQuery.use(originUserLogin.getDelegator()).from("UserLoginHistory").where(conditions).queryCount() == 0) { + String errMsg = UtilProperties.getMessage(resourceWebapp, "loginevents.impersonate_NotInProcess", UtilHttp.getLocale(request)); + request.setAttribute("_ERROR_MESSAGE_", errMsg); + return "error"; + } + originUserLogin.getDelegator().storeByCondition("UserLoginHistory", + UtilMisc.toMap("thruDate", UtilDateTime.nowTimestamp()), conditions); + } catch (GenericEntityException e) { + return "error"; + } + + // Log back the impersonating user + return doMainLogin(request, response, originUserLogin, null); + } + protected static void setWebContextObjects(HttpServletRequest request, HttpServletResponse response, Delegator delegator, LocalDispatcher dispatcher) { HttpSession session = request.getSession(); // NOTE: we do NOT want to set this in the servletContext, only in the request and session @@ -561,6 +770,10 @@ public class LoginWorker { public static String doMainLogin(HttpServletRequest request, HttpServletResponse response, GenericValue userLogin, Map userLoginSession) { HttpSession session = request.getSession(); + boolean authoriseLoginDuringImpersonate = EntityUtilProperties.propertyValueEquals("security", "security.login.authorised.during.impersonate", "true"); + if (!authoriseLoginDuringImpersonate && checkImpersonationInProcess(request, response) != null) { + return "error"; + } if (userLogin != null && hasBasePermission(userLogin, request)) { doBasicLogin(userLogin, request); } else { @@ -1222,4 +1435,13 @@ public class LoginWorker { } return "success"; } + + /** + * Return true if userLogin has not been disabled + * @param userLogin + * @return + */ + public static boolean isUserLoginActive(GenericValue userLogin) { + return !"N".equals(userLogin.getString("enabled")) && UtilValidate.isEmpty(userLogin.getString("disabledBy")); + } } diff --git a/themes/bluelight/template/Header.ftl b/themes/bluelight/template/Header.ftl index 2f21c89c71..e54f68c140 100644 --- a/themes/bluelight/template/Header.ftl +++ b/themes/bluelight/template/Header.ftl @@ -117,6 +117,7 @@ under the License. + <#include "component://common-theme/template/ImpersonateBanner.ftl"/> diff --git a/themes/common-theme/template/ImpersonateBanner.ftl b/themes/common-theme/template/ImpersonateBanner.ftl new file mode 100644 index 0000000000..2dd3e9f2d1 --- /dev/null +++ b/themes/common-theme/template/ImpersonateBanner.ftl @@ -0,0 +1,11 @@ +<#if parameters.originUserLogin??> + ${uiLabelMap.CommonImpersonateTitle} +
+
+ × +

${uiLabelMap.CommonImpersonateTitle}

+

${uiLabelMap.CommonImpersonateUserLogin} : ${context.userLogin.userLoginId!}

+ ${uiLabelMap.CommonImpersonateStop} +
+
+ diff --git a/themes/common-theme/template/Impersonated.ftl b/themes/common-theme/template/Impersonated.ftl new file mode 100644 index 0000000000..b6117cefe3 --- /dev/null +++ b/themes/common-theme/template/Impersonated.ftl @@ -0,0 +1,33 @@ +<#-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. 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 under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +<#assign messageMap = Static["org.apache.ofbiz.base.util.UtilMisc"].toMap("originUserLoginId", impersonator)/> +<#assign impersonationMessage= Static["org.apache.ofbiz.base.util.UtilProperties"].getMessage("SecurityextUiLabels", "loginevents.impersonation_in_process", messageMap, locale)/> +<#assign fromDate = Static["org.apache.ofbiz.base.util.UtilFormatOut"].formatDateTime(impersonationFromDate, "", locale, timeZone)/> +
+
+ ${uiLabelMap.CommonImpersonateTitle} +

${impersonationMessage!}

+

${uiLabelMap.CommonSince} ${fromDate!}

+

${uiLabelMap.CommonLogout}

+
+
+ diff --git a/themes/common-theme/webapp/common/css/impersonate.css b/themes/common-theme/webapp/common/css/impersonate.css new file mode 100644 index 0000000000..276f26c23e --- /dev/null +++ b/themes/common-theme/webapp/common/css/impersonate.css @@ -0,0 +1,180 @@ +#impersonateBtn { + position: fixed; + bottom: 37px; + right: 20px; + z-index: 10000; + width: 79px; + height: 60px; + cursor: pointer; +} + +#impersonateBtn img { + width: 100%; + max-width: 79px; + height: auto; + opacity: 0.6; + transition: opacity .6s; +} + +#impersonateBtn:hover img { + opacity: 1; +} + +#impersonateContent:before { + content: ""; + display: none; + background: rgba(225, 225, 225, 0.8); + position: fixed; + top: 0; + left: 0; + right: 0; + bottom: 0; + z-index: 10; +} + +#impersonateContent:target:before { + display: block; +} + +#impersonateContent:target .impersonateModal { + opacity: 1; + bottom: 117px; + right: 20px; + -webkit-transform: translate(0, 0); + -ms-transform: translate(0, 0); + transform: translate(0, 0); +} + +.impersonateModal { + opacity: 0; + position: fixed; + background: #FFF; + text-align: center; + padding: 30px 30px 15px 30px; + border-radius: 5px; + z-index: 11; + box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.15); + -webkit-transform: translate(0, -500%); + -ms-transform: translate(0, -500%); + transform: translate(0, -500%); + -webkit-transition: -webkit-transform 0.3s ease-out; + -moz-transition: -moz-transform 0.3s ease-out; + -o-transition: -o-transform 0.3s ease-out; + transition: transform 0.3s ease-out; +} + +.impersonateModal:before { + content: ""; + position: absolute; + bottom: -20px; + right: 37px; + border: 1em solid #FFF; + border-color: transparent transparent #FFF #FFF; + transform-origin: 0 0; + transform: rotate(-45deg); + box-shadow: -3px 3px 5px 0 rgba(0, 0, 0, 0.05); +} + +#impersonateContent .btn-close { + color: #aaa; + font-size: 25px; + text-decoration: none; + position: absolute; + right: 10px; + top: 10px; +} + +#impersonateContent .btn-close:hover { + color: #919191; +} + +#impersonateContent h3 { + font-size: 26px; + font-weight: normal; + letter-spacing: 0.5px; + line-height: 24px; + color: #f08906; + border-bottom: 1px solid #F2F2F2; + padding-bottom: 10px; + margin: 15px 0 20px 0; +} + +#impersonateContent p { + font-size: 18px; + color: #666; + letter-spacing: 0.3px; +} + +#impersonateContent p strong { + color: #005982; + font-weight: bold; +} + +#impersonateContent .btn { + border-radius: 2px; + border: 1px solid #005982; + padding: 7px 12px; + text-transform: uppercase; + letter-spacing: 1px; + color: #005982; + margin: 30px 0 10px 0; + line-height: 22px; + display: inline-block; + -webkit-transition: 0.2s ease-out; + -moz-transition: 0.2s ease-out; + -o-transition: 0.2s ease-out; + transition: 0.2s ease-out; +} + +#impersonateContent .btn:hover { + color: #f08906; + border-color: #f08906; +} + +#impersonateMode { + position: fixed; + top: 0; + left: 0; + height: 100%; + min-height: 100vh; + width: 100%; + display: flex; + justify-content: center; + align-items: center; + background: rgba(225, 225, 225, 0.8); + z-index: 11; + cursor: not-allowed; +} + +#impersonateMode .content { + background: #FFF; + padding: 30px; + box-shadow: -3px 3px 5px 0 rgba(0, 0, 0, 0.05); + text-align: center; +} + +#impersonateMode .content p.user { + margin-bottom: 15px; + color: #005982; + font-weight: bold; +} + +#impersonateMode .content p { + font-size: 18px; + color: #666; + letter-spacing: 0.3px; +} + +#impersonateMode .content img { + margin-bottom: 20px; +} + +#impersonateMode .content .user-pref-btn { + width: auto; + border-radius: 3px; + display: inline-block; + padding: 5px 15px; + font-size: 15px; + text-transform: uppercase; + font-weight: 600; +} diff --git a/themes/common-theme/webapp/images/img/impersonate-ico.png b/themes/common-theme/webapp/images/img/impersonate-ico.png new file mode 100644 index 0000000000..d12c252dfc Binary files /dev/null and b/themes/common-theme/webapp/images/img/impersonate-ico.png differ diff --git a/themes/common-theme/widget/CommonScreens.xml b/themes/common-theme/widget/CommonScreens.xml index f0cf58efa1..8e674ec07a 100644 --- a/themes/common-theme/widget/CommonScreens.xml +++ b/themes/common-theme/widget/CommonScreens.xml @@ -155,6 +155,7 @@ under the License. + @@ -546,6 +547,42 @@ under the License. + +
+ + + + + + + + + + + + + + + + + + + +
+ + + + +
+ + + +
+
+
+
+
+
@@ -608,7 +645,7 @@ under the License. - + diff --git a/themes/common-theme/widget/Theme.xml b/themes/common-theme/widget/Theme.xml index 7de3df01b1..1ac0e2f2cc 100644 --- a/themes/common-theme/widget/Theme.xml +++ b/themes/common-theme/widget/Theme.xml @@ -148,6 +148,7 @@ under the License. + diff --git a/themes/flatgrey/template/Header.ftl b/themes/flatgrey/template/Header.ftl index f0a7eb6c1f..d86d5f41fd 100644 --- a/themes/flatgrey/template/Header.ftl +++ b/themes/flatgrey/template/Header.ftl @@ -97,6 +97,7 @@ under the License. <#assign organizationLogoLinkURL = "${layoutSettings.organizationLogoLinkUrl!}"> + <#include "component://common-theme/template/ImpersonateBanner.ftl"/> diff --git a/themes/rainbowstone/template/includes/TopAppBar.ftl b/themes/rainbowstone/template/includes/TopAppBar.ftl index f5e7ca9e7e..6faf99b27f 100644 --- a/themes/rainbowstone/template/includes/TopAppBar.ftl +++ b/themes/rainbowstone/template/includes/TopAppBar.ftl @@ -30,6 +30,7 @@ under the License. +<#include "component://common-theme/template/ImpersonateBanner.ftl"/> diff --git a/themes/tomahawk/template/Header.ftl b/themes/tomahawk/template/Header.ftl index e027c421ad..33766149ec 100644 --- a/themes/tomahawk/template/Header.ftl +++ b/themes/tomahawk/template/Header.ftl @@ -102,6 +102,7 @@ under the License. <#assign organizationLogoLinkURL = "${layoutSettings.organizationLogoLinkUrl!}"> + <#include "component://common-theme/template/ImpersonateBanner.ftl"/>