Skip to content

Commit

Permalink
[Workspace] Fix non-workspace admin update defaultIndex (#8675) (#8679)
Browse files Browse the repository at this point in the history
* Fix updating defaultIndex at workspace uiSetting level



* Changeset file for PR #8675 created/updated

* optimize the code



* optimize the code



* optimize the code



---------



(cherry picked from commit 41ab14d)

Signed-off-by: yubonluo <yubonluo@amazon.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 23, 2024
1 parent d21f8a6 commit a0b42c6
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 9 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8675.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- [Workspace] Fix non-workspace admin update defaultIndex ([#8675](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8675))
2 changes: 1 addition & 1 deletion src/core/public/workspace/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { WorkspaceAttribute } from '../../types';

export type WorkspaceObject = WorkspaceAttribute & { readonly?: boolean };
export type WorkspaceObject = WorkspaceAttribute & { readonly?: boolean; owner?: boolean };

export type IWorkspaceResponse<T> =
| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ export type EnsureDefaultIndexPattern = () => Promise<unknown | void> | undefine

export const createEnsureDefaultIndexPattern = (
uiSettings: UiSettingsCommon,
onRedirectNoIndexPattern: () => Promise<unknown> | void
onRedirectNoIndexPattern: () => Promise<unknown> | void,
canUpdateUiSetting?: boolean
) => {
/**
* Checks whether a default index pattern is set and exists and defines
* one otherwise.
*/
return async function ensureDefaultIndexPattern(this: IndexPatternsContract) {
if (canUpdateUiSetting === false) {
return;
}
const patterns = await this.getIds();
let defaultId = await uiSettings.get('defaultIndex');
let defined = !!defaultId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ interface IndexPatternsServiceDeps {
onError: OnError;
onRedirectNoIndexPattern?: () => void;
onUnsupportedTimePattern: OnUnsupportedTimePattern;
canUpdateUiSetting?: boolean;
}

export class IndexPatternsService {
Expand All @@ -96,6 +97,7 @@ export class IndexPatternsService {
onError,
onUnsupportedTimePattern,
onRedirectNoIndexPattern = () => {},
canUpdateUiSetting,
}: IndexPatternsServiceDeps) {
this.apiClient = apiClient;
this.config = uiSettings;
Expand All @@ -106,7 +108,8 @@ export class IndexPatternsService {
this.onUnsupportedTimePattern = onUnsupportedTimePattern;
this.ensureDefaultIndexPattern = createEnsureDefaultIndexPattern(
uiSettings,
onRedirectNoIndexPattern
onRedirectNoIndexPattern,
canUpdateUiSetting
);
}

Expand Down
16 changes: 15 additions & 1 deletion src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,15 @@ export class DataPublicPlugin
}

public start(core: CoreStart, { uiActions }: DataStartDependencies): DataPublicPluginStart {
const { uiSettings, http, notifications, savedObjects, overlays, application } = core;
const {
uiSettings,
http,
notifications,
savedObjects,
overlays,
application,
workspaces,
} = core;
setNotifications(notifications);
setOverlays(overlays);
setUiSettings(uiSettings);
Expand All @@ -220,6 +228,12 @@ export class DataPublicPlugin
notifications.toasts,
application.navigateToApp
),
// If workspace is enabled, only workspace owner/OSD admin can update ui setting.
...(application.capabilities.workspaces.enabled && {
canUpdateUiSetting:
workspaces?.currentWorkspace$.getValue()?.owner ||
application.capabilities?.dashboards?.isDashboardAdmin !== false,
}),
});
setIndexPatterns(indexPatterns);

Expand Down
1 change: 1 addition & 0 deletions src/plugins/workspace/public/workspace_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ describe('#WorkspaceClient', () => {
expect(workspaceMock.workspaceList$.getValue()).toEqual([
{
id: 'foo',
owner: true,
readonly: false,
},
]);
Expand Down
20 changes: 15 additions & 5 deletions src/plugins/workspace/public/workspace_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,27 @@ export class WorkspaceClient implements IWorkspaceClient {
});

if (result?.success) {
const resultWithWritePermission = await this.list({
perPage: 999,
permissionModes: [WorkspacePermissionMode.LibraryWrite],
});
if (resultWithWritePermission?.success) {
const [resultWithWritePermission, resultWithOwnerPermission] = await Promise.all([
this.list({
perPage: 999,
permissionModes: [WorkspacePermissionMode.LibraryWrite],
}),
this.list({
perPage: 999,
permissionModes: [WorkspacePermissionMode.Write],
}),
]);
if (resultWithWritePermission?.success && resultWithOwnerPermission?.success) {
const workspaceIdsWithWritePermission = resultWithWritePermission.result.workspaces.map(
(workspace: WorkspaceAttribute) => workspace.id
);
const workspaceIdsWithOwnerPermission = resultWithOwnerPermission.result.workspaces.map(
(workspace: WorkspaceAttribute) => workspace.id
);
const workspaces = result.result.workspaces.map((workspace: WorkspaceAttribute) => ({
...workspace,
readonly: !workspaceIdsWithWritePermission.includes(workspace.id),
owner: workspaceIdsWithOwnerPermission.includes(workspace.id),
}));
this.workspaces.workspaceList$.next(workspaces);
}
Expand Down

0 comments on commit a0b42c6

Please sign in to comment.