From 21baad0135f38e24a6dce7efb1067a955b46dba5 Mon Sep 17 00:00:00 2001 From: Suren Date: Thu, 20 Feb 2025 20:25:33 +0530 Subject: [PATCH] #10816: Allow dynamic management of style editing permissions (#10821) --- web/client/plugins/StyleEditor.jsx | 12 ++----- .../plugins/__tests__/StyleEditor-test.jsx | 15 +++++---- .../selectors/__tests__/styleeditor-test.js | 31 ++++++++++++++++++- web/client/selectors/styleeditor.js | 9 ++++++ 4 files changed, 49 insertions(+), 18 deletions(-) diff --git a/web/client/plugins/StyleEditor.jsx b/web/client/plugins/StyleEditor.jsx index 9fca0c85f0..4b7b48d860 100644 --- a/web/client/plugins/StyleEditor.jsx +++ b/web/client/plugins/StyleEditor.jsx @@ -14,7 +14,7 @@ import { branch, compose, lifecycle, toClass } from 'recompose'; import { createSelector } from 'reselect'; import { updateSettingsParams } from '../actions/layers'; -import { initStyleService, setEditPermissionStyleEditor, toggleStyleEditor } from '../actions/styleeditor'; +import { initStyleService, toggleStyleEditor } from '../actions/styleeditor'; import HTML from '../components/I18N/HTML'; import BorderLayout from '../components/layout/BorderLayout'; import emptyState from '../components/misc/enhancers/emptyState'; @@ -43,8 +43,7 @@ const StyleEditorPanel = ({ editingAllowedGroups, enableSetDefaultStyle, canEdit, - editorConfig, - onSetPermission + editorConfig }) => { useEffect(() => { @@ -57,10 +56,6 @@ const StyleEditorPanel = ({ ); }, []); - useEffect(() => { - onSetPermission(canEdit); - }, [canEdit]); - return ( { // detect if the static service has been updated with new information in the global state diff --git a/web/client/plugins/__tests__/StyleEditor-test.jsx b/web/client/plugins/__tests__/StyleEditor-test.jsx index 2353e50f96..e888d11b64 100644 --- a/web/client/plugins/__tests__/StyleEditor-test.jsx +++ b/web/client/plugins/__tests__/StyleEditor-test.jsx @@ -14,8 +14,7 @@ import { getPluginForTest } from './pluginsTestUtils'; import { act } from 'react-dom/test-utils'; import { INIT_STYLE_SERVICE, - TOGGLE_STYLE_EDITOR, - SET_EDIT_PERMISSION + TOGGLE_STYLE_EDITOR } from '../../actions/styleeditor'; describe('StyleEditor Plugin', () => { @@ -45,9 +44,9 @@ describe('StyleEditor Plugin', () => { styleService={cfgStyleService} />, document.getElementById("container")); }); - expect(actions.length).toBe(3); + expect(actions.length).toBe(2); expect(actions.map(action => action.type)) - .toEqual([ TOGGLE_STYLE_EDITOR, INIT_STYLE_SERVICE, SET_EDIT_PERMISSION ]); + .toEqual([ TOGGLE_STYLE_EDITOR, INIT_STYLE_SERVICE ]); expect(actions[1].service).toBeTruthy(); expect(actions[1].service).toEqual({ ...cfgStyleService, isStatic: true }); expect(actions[1].permissions.editingAllowedRoles).toEqual(['ADMIN']); @@ -86,9 +85,9 @@ describe('StyleEditor Plugin', () => { {...permissions} />, document.getElementById("container")); }); - expect(actions.length).toBe(3); + expect(actions.length).toBe(2); expect(actions.map(action => action.type)) - .toEqual([ TOGGLE_STYLE_EDITOR, INIT_STYLE_SERVICE, SET_EDIT_PERMISSION ]); + .toEqual([ TOGGLE_STYLE_EDITOR, INIT_STYLE_SERVICE ]); expect(actions[1].service).toBeTruthy(); expect(actions[1].service).toEqual(stateStyleService); expect(actions[1].permissions).toEqual(permissions); @@ -110,9 +109,9 @@ describe('StyleEditor Plugin', () => { active />, document.getElementById("container")); }); - expect(actions.length).toBe(3); + expect(actions.length).toBe(2); expect(actions.map(action => action.type)) - .toEqual([ TOGGLE_STYLE_EDITOR, INIT_STYLE_SERVICE, SET_EDIT_PERMISSION ]); + .toEqual([ TOGGLE_STYLE_EDITOR, INIT_STYLE_SERVICE ]); expect(actions[1].service).toBeTruthy(); expect(actions[1].service).toEqual(styleService); }); diff --git a/web/client/selectors/__tests__/styleeditor-test.js b/web/client/selectors/__tests__/styleeditor-test.js index 842b7d4b82..fb0c7b693c 100644 --- a/web/client/selectors/__tests__/styleeditor-test.js +++ b/web/client/selectors/__tests__/styleeditor-test.js @@ -31,7 +31,8 @@ import { editorMetadataSelector, selectedStyleMetadataSelector, editingAllowedRolesSelector, - editingAllowedGroupsSelector + editingAllowedGroupsSelector, + canEditSelector } from '../styleeditor'; import { setCustomUtils, @@ -792,5 +793,33 @@ describe('Test styleeditor selector', () => { } })).toBeFalsy(); }); + it('test `canEdit` taking precedence over allowed roles and groups', () => { + expect(canEditStyleSelector({ + styleeditor: { + canEdit: true, + editingAllowedRoles: ['USER1'], + editingAllowedGroups: ['some'] + }, + security: { + user: { + role: 'USER', + groups: { + group: { + enabled: true, + groupName: 'test' + } + } + } + } + })).toBeTruthy(); + }); + it('test canEditSelector', () => { + expect(canEditSelector({ + styleeditor: { + canEdit: true + } + })).toBeTruthy(); + expect(canEditSelector()).toBeFalsy(); + }); }); }); diff --git a/web/client/selectors/styleeditor.js b/web/client/selectors/styleeditor.js index 3031ce1d08..d0dbf0127c 100644 --- a/web/client/selectors/styleeditor.js +++ b/web/client/selectors/styleeditor.js @@ -128,6 +128,13 @@ export const editingAllowedRolesSelector = (state) => get(state, 'styleeditor.ed * @returns {object} */ export const editingAllowedGroupsSelector = (state) => get(state, 'styleeditor.editingAllowedGroups', []); +/** + * Selects canEdit configuration value if any + * @memberof selectors.styleeditor + * @param {object} state the state + * @returns {object} + */ +export const canEditSelector = (state) => get(state, 'styleeditor.canEdit', false); /** * selects canEdit status of styleeditor service from state * @memberof selectors.styleeditor @@ -135,6 +142,8 @@ export const editingAllowedGroupsSelector = (state) => get(state, 'styleeditor.e * @return {bool} */ export const canEditStyleSelector = (state) => { + const canEdit = canEditSelector(state); + if (canEdit) return canEdit; const allowedRoles = editingAllowedRolesSelector(state); const allowedGroups = editingAllowedGroupsSelector(state); const _isSameOrigin = isSameOrigin(getUpdatedLayer(state), styleServiceSelector(state));