From 9203e24720018b0499bd5e48ecaedb74f57bc1c0 Mon Sep 17 00:00:00 2001 From: mahmoud adel <58145645+mahmoudadel54@users.noreply.github.com> Date: Thu, 2 May 2024 18:53:18 +0300 Subject: [PATCH] #10262: Map (created with context) crashes if the export plugin is set up to be hidden to certain groups and the user is not logged in (#10270) * #10262: Map (created with context) crashes if the export plugin is set up to be hidden to certain groups and the user is not logged in Description: - edit parseExpression method in PluginUtils to handle the chanining within cfg expressions * #10262: Map (created with context) crashes if the export plugin is set up to be hidden to certain groups and the user is not logged in Description: - add unit test to test parseExpression change --- web/client/utils/PluginsUtils.js | 7 ++++++- web/client/utils/__tests__/PluginsUtils-test.js | 13 ++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/web/client/utils/PluginsUtils.js b/web/client/utils/PluginsUtils.js index 4e03fa890b..97ba9bac24 100644 --- a/web/client/utils/PluginsUtils.js +++ b/web/client/utils/PluginsUtils.js @@ -164,7 +164,12 @@ const parseExpression = (state = {}, context = {}, value) => { }; const request = url.parse(location.href, true); if (expression !== null) { - return eval(expression[1]); + let modifiedExpression = expression[1]; + // adding optional operator to the expression + if (modifiedExpression.includes(").")) { + modifiedExpression = modifiedExpression.replaceAll(").", ")?."); + } + return eval(modifiedExpression); } return value; }; diff --git a/web/client/utils/__tests__/PluginsUtils-test.js b/web/client/utils/__tests__/PluginsUtils-test.js index 312cf49c74..79eaae6b02 100644 --- a/web/client/utils/__tests__/PluginsUtils-test.js +++ b/web/client/utils/__tests__/PluginsUtils-test.js @@ -118,7 +118,18 @@ describe('PluginsUtils', () => { it('handleExpression', () => { expect(PluginsUtils.handleExpression({state1: "test1"}, {context1: "test2"}, "{state.state1 + ' ' + context.context1}")).toBe("test1 test2"); }); - + it('handleExpression in case there is a chaining within the expression that needs to access available state', () => { + const state = {groups: ["ADMIN", "NORMAL_USER"]}; + const getState = (path) => state[path]; + expect(PluginsUtils.handleExpression(getState, {context1: "test2"}, "{state('groups').filter(gr => ['ADMIN'].includes(gr)).length}")).toBe(1); + expect(PluginsUtils.handleExpression(getState, {context1: "test2"}, "{state('groups').filter(gr => ['NORMAL_USER'].includes(gr)).length}")).toBe(1); + expect(PluginsUtils.handleExpression(getState, {context1: "test2"}, "{state('groups').filter(gr => ['NOT_ADMIN'].includes(gr)).length}")).toBe(0); + }); + it('handleExpression in case there is a chaining within the expression that needs to access unavailable state', () => { + const state = {groups: undefined}; + const getState = (path) => state[path]; + expect(PluginsUtils.handleExpression(getState, {context1: "test2"}, "{state('groups').filter(gr => ['ADMIN'].includes(gr))}")).toBe(undefined); + }); it('getPluginItems', () => { const plugins = { Test1Plugin: {