Skip to content

Commit

Permalink
[Workspace] fix:Prevent user from visiting dashboards / visualization…
Browse files Browse the repository at this point in the history
…s when out of a workspace (opensearch-project#9024)

* add workspaceAvailability

Signed-off-by: Qxisylolo <[email protected]>

* Changeset file for PR opensearch-project#9024 created/updated

* add tests

Signed-off-by: Qxisylolo <[email protected]>

* update test

Signed-off-by: Qxisylolo <[email protected]>

* fix test use piple

Signed-off-by: Qxisylolo <[email protected]>

* fix test new

Signed-off-by: Qxisylolo <[email protected]>

---------

Signed-off-by: Qxisylolo <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
Signed-off-by: Federico Silva <[email protected]>
  • Loading branch information
2 people authored and silvaf-dev committed Jan 7, 2025
1 parent a7023ac commit dcaa54b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/9024.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Prevent user from visiting dashboards / visualizations when out of a workspace ([#9024](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9024))
10 changes: 6 additions & 4 deletions src/plugins/dashboard/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import { filter, map } from 'rxjs/operators';
import { i18n } from '@osd/i18n';
import { FormattedMessage } from '@osd/i18n/react';

import { UrlForwardingSetup, UrlForwardingStart } from 'src/plugins/url_forwarding/public';
import { isEmpty } from 'lodash';
import { createHashHistory } from 'history';
import {
App,
AppMountParameters,
Expand All @@ -43,11 +46,9 @@ import {
Plugin,
PluginInitializerContext,
SavedObjectsClientContract,
WorkspaceAvailability,
ScopedHistory,
} from 'src/core/public';
import { UrlForwardingSetup, UrlForwardingStart } from 'src/plugins/url_forwarding/public';
import { isEmpty } from 'lodash';
import { createHashHistory } from 'history';
} from '../../../../src/core/public';
import { UsageCollectionSetup } from '../../usage_collection/public';
import {
CONTEXT_MENU_TRIGGER,
Expand Down Expand Up @@ -367,6 +368,7 @@ export class DashboardPlugin
id: DashboardConstants.DASHBOARDS_ID,
title: 'Dashboards',
order: 2500,
workspaceAvailability: WorkspaceAvailability.insideWorkspace,
euiIconType: 'inputOutput',
defaultPath: `#${DashboardConstants.LANDING_PAGE_PATH}`,
updater$: this.appStateUpdater,
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/visualize/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
ScopedHistory,
} from 'opensearch-dashboards/public';

import { WorkspaceAvailability } from '../../../../src/core/public';
import {
Storage,
createOsdUrlTracker,
Expand Down Expand Up @@ -158,6 +159,7 @@ export class VisualizePlugin
title: 'Visualize',
order: 8000,
euiIconType: 'inputOutput',
workspaceAvailability: WorkspaceAvailability.insideWorkspace,
defaultPath: '#/',
category: DEFAULT_APP_CATEGORIES.opensearchDashboards,
updater$: this.appStateUpdater.asObservable(),
Expand Down
30 changes: 29 additions & 1 deletion src/plugins/workspace/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import { BehaviorSubject, Observable, Subscriber } from 'rxjs';
import { waitFor } from '@testing-library/dom';
import { first } from 'rxjs/operators';

import { applicationServiceMock, chromeServiceMock, coreMock } from '../../../core/public/mocks';
import {
ChromeBreadcrumb,
NavGroupStatus,
DEFAULT_NAV_GROUPS,
AppNavLinkStatus,
WorkspaceAvailability,
AppStatus,
} from '../../../core/public';
import { WORKSPACE_FATAL_ERROR_APP_ID, WORKSPACE_DETAIL_APP_ID } from '../common/constants';
import { savedObjectsManagementPluginMock } from '../../saved_objects_management/public/mocks';
Expand Down Expand Up @@ -509,6 +510,33 @@ describe('Workspace plugin', () => {
});
});

it('#start should not be able to access app of which workspaceAvailability is set to insideWorkspace when out of workspace', async () => {
const workspacePlugin = new WorkspacePlugin();
const setupMock = getSetupMock();
const coreStart = coreMock.createStart();
await workspacePlugin.setup(setupMock, {});
coreStart.workspaces.currentWorkspace$.next(null);

coreStart.application.capabilities = {
...coreStart.application.capabilities,
dashboard: {
isDashboardAdmin: false,
},
};

workspacePlugin.start(coreStart, getMockDependencies());

const mockApp = {
id: 'dashboards',
workspaceAvailability: WorkspaceAvailability.insideWorkspace,
};

const appUpdater$ = setupMock.application.registerAppUpdater.mock.calls[0][0];

const appState = await appUpdater$.pipe(first()).toPromise();
expect(appState(mockApp)).toEqual({ status: AppStatus.inaccessible });
});

it('#stop should call unregisterNavGroupUpdater', async () => {
const workspacePlugin = new WorkspacePlugin();
const setupMock = getSetupMock();
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ export class WorkspacePlugin
*/
return { status: AppStatus.inaccessible };
});
} else {
this.appUpdater$.next((app) => {
if (app.workspaceAvailability === WorkspaceAvailability.insideWorkspace) {
return { status: AppStatus.inaccessible };
}
});
}
});

Expand Down

0 comments on commit dcaa54b

Please sign in to comment.