Skip to content

Commit

Permalink
#10816: Allow dynamic management of style editing permissions (#10821)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsuren1 authored Feb 20, 2025
1 parent fb0c04f commit 21baad0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
12 changes: 3 additions & 9 deletions web/client/plugins/StyleEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -43,8 +43,7 @@ const StyleEditorPanel = ({
editingAllowedGroups,
enableSetDefaultStyle,
canEdit,
editorConfig,
onSetPermission
editorConfig
}) => {

useEffect(() => {
Expand All @@ -57,10 +56,6 @@ const StyleEditorPanel = ({
);
}, []);

useEffect(() => {
onSetPermission(canEdit);
}, [canEdit]);

return (
<BorderLayout
className="ms-style-editor-container"
Expand Down Expand Up @@ -168,8 +163,7 @@ const StyleEditorPlugin = compose(
),
{
onInit: initStyleService,
onUpdateParams: updateSettingsParams,
onSetPermission: setEditPermissionStyleEditor
onUpdateParams: updateSettingsParams
},
(stateProps, dispatchProps, ownProps) => {
// detect if the static service has been updated with new information in the global state
Expand Down
15 changes: 7 additions & 8 deletions web/client/plugins/__tests__/StyleEditor-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});
Expand Down
31 changes: 30 additions & 1 deletion web/client/selectors/__tests__/styleeditor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import {
editorMetadataSelector,
selectedStyleMetadataSelector,
editingAllowedRolesSelector,
editingAllowedGroupsSelector
editingAllowedGroupsSelector,
canEditSelector
} from '../styleeditor';
import {
setCustomUtils,
Expand Down Expand Up @@ -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();
});
});
});
9 changes: 9 additions & 0 deletions web/client/selectors/styleeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,22 @@ 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
* @param {object} state the state
* @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));
Expand Down

0 comments on commit 21baad0

Please sign in to comment.