From 6a871ce3eb6b76ce3db6a3a897cae124e96eee72 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Wed, 3 Jul 2024 16:47:09 +0800 Subject: [PATCH] [Workspace] Capabilities service add dashboard admin flag (#7103) (#7151) * Capabilities service add dashboard admin flag * Capabilities service add dashboard admin flag * Changeset file for PR #7103 created/updated * optimize the code * optimize the code * if groups/users are not configured or [], login defaults to OSD Admin * optimize the code --------- (cherry picked from commit bb9eeef2610a288f251ed4a1b5c2d9e17e3d16ed) Signed-off-by: yubonluo Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> --- changelogs/fragments/7103.yml | 2 ++ src/plugins/workspace/server/plugin.test.ts | 29 ++++++++++++++++++++- src/plugins/workspace/server/plugin.ts | 8 ++++++ src/plugins/workspace/server/utils.test.ts | 2 +- src/plugins/workspace/server/utils.ts | 4 +-- 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/7103.yml diff --git a/changelogs/fragments/7103.yml b/changelogs/fragments/7103.yml new file mode 100644 index 000000000000..f016a8c9962b --- /dev/null +++ b/changelogs/fragments/7103.yml @@ -0,0 +1,2 @@ +feat: +- [Workspace] Capabilities service add dashboard admin flag ([#7103](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7103)) \ No newline at end of file diff --git a/src/plugins/workspace/server/plugin.test.ts b/src/plugins/workspace/server/plugin.test.ts index 0b943cf47252..df9c9edac5ed 100644 --- a/src/plugins/workspace/server/plugin.test.ts +++ b/src/plugins/workspace/server/plugin.test.ts @@ -6,21 +6,31 @@ import { OnPostAuthHandler, OnPreRoutingHandler } from 'src/core/server'; import { coreMock, httpServerMock } from '../../../core/server/mocks'; import { WorkspacePlugin } from './plugin'; -import { getWorkspaceState } from '../../../core/server/utils'; +import { getWorkspaceState, updateWorkspaceState } from '../../../core/server/utils'; import * as utilsExports from './utils'; describe('Workspace server plugin', () => { it('#setup', async () => { let value; + const capabilities = {} as any; const setupMock = coreMock.createSetup(); const initializerContextConfigMock = coreMock.createPluginInitializerContext({ enabled: true, }); + const request = httpServerMock.createOpenSearchDashboardsRequest(); + setupMock.capabilities.registerProvider.mockImplementationOnce((fn) => (value = fn())); + setupMock.capabilities.registerSwitcher.mockImplementationOnce((fn) => { + return fn(request, capabilities); + }); + const workspacePlugin = new WorkspacePlugin(initializerContextConfigMock); await workspacePlugin.setup(setupMock); expect(value).toMatchInlineSnapshot(` Object { + "dashboards": Object { + "isDashboardAdmin": false, + }, "workspaces": Object { "enabled": true, "permissionEnabled": true, @@ -28,6 +38,23 @@ describe('Workspace server plugin', () => { } `); expect(setupMock.savedObjects.addClientWrapper).toBeCalledTimes(4); + + let registerSwitcher; + let result; + updateWorkspaceState(request, { isDashboardAdmin: false }); + registerSwitcher = setupMock.capabilities.registerSwitcher.mock.calls[0][0]; + result = registerSwitcher(request, capabilities); + expect(result).toEqual({ dashboards: { isDashboardAdmin: false } }); + + updateWorkspaceState(request, { isDashboardAdmin: true }); + registerSwitcher = setupMock.capabilities.registerSwitcher.mock.calls[0][0]; + result = registerSwitcher(request, capabilities); + expect(result).toEqual({ dashboards: { isDashboardAdmin: true } }); + + updateWorkspaceState(request, { isDashboardAdmin: undefined }); + registerSwitcher = setupMock.capabilities.registerSwitcher.mock.calls[0][0]; + result = registerSwitcher(request, capabilities); + expect(result).toEqual({ dashboards: { isDashboardAdmin: true } }); }); it('#proxyWorkspaceTrafficToRealHandler', async () => { diff --git a/src/plugins/workspace/server/plugin.ts b/src/plugins/workspace/server/plugin.ts index 2ec8447ad817..1479da984580 100644 --- a/src/plugins/workspace/server/plugin.ts +++ b/src/plugins/workspace/server/plugin.ts @@ -30,6 +30,7 @@ import { WorkspaceSavedObjectsClientWrapper } from './saved_objects'; import { cleanWorkspaceId, getWorkspaceIdFromUrl, + getWorkspaceState, updateWorkspaceState, } from '../../../core/server/utils'; import { WorkspaceConflictSavedObjectsClientWrapper } from './saved_objects/saved_objects_wrapper_for_check_workspace_conflict'; @@ -157,7 +158,14 @@ export class WorkspacePlugin implements Plugin { + // If the value is undefined/true, the user is dashboard admin. + const isDashboardAdmin = getWorkspaceState(request).isDashboardAdmin !== false; + return { dashboards: { isDashboardAdmin } }; + }); return { client: this.client, diff --git a/src/plugins/workspace/server/utils.test.ts b/src/plugins/workspace/server/utils.test.ts index 15e6783c2cab..095208dca8b9 100644 --- a/src/plugins/workspace/server/utils.test.ts +++ b/src/plugins/workspace/server/utils.test.ts @@ -128,7 +128,7 @@ describe('workspace utils', () => { const configGroups: string[] = []; const configUsers: string[] = []; updateDashboardAdminStateForRequest(mockRequest, groups, users, configGroups, configUsers); - expect(getWorkspaceState(mockRequest)?.isDashboardAdmin).toBe(false); + expect(getWorkspaceState(mockRequest)?.isDashboardAdmin).toBe(true); }); it('should get correct admin config when admin config is enabled ', async () => { diff --git a/src/plugins/workspace/server/utils.ts b/src/plugins/workspace/server/utils.ts index 9037038f16af..d98352050a31 100644 --- a/src/plugins/workspace/server/utils.ts +++ b/src/plugins/workspace/server/utils.ts @@ -67,9 +67,9 @@ export const updateDashboardAdminStateForRequest = ( updateWorkspaceState(request, { isDashboardAdmin: true }); return; } - + // If groups/users are not configured or [], login defaults to OSD Admin if (!configGroups.length && !configUsers.length) { - updateWorkspaceState(request, { isDashboardAdmin: false }); + updateWorkspaceState(request, { isDashboardAdmin: true }); return; } const groupMatchAny = groups.some((group) => configGroups.includes(group));