Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Workspace] Fix non-workspace admin update defaultIndex #8675

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,18 @@

export const createEnsureDefaultIndexPattern = (
uiSettings: UiSettingsCommon,
onRedirectNoIndexPattern: () => Promise<unknown> | void
onRedirectNoIndexPattern: () => Promise<unknown> | void,
isCurrentWorkspaceOwner?: boolean
yubonluo marked this conversation as resolved.
Show resolved Hide resolved
) => {
/**
* Checks whether a default index pattern is set and exists and defines
* one otherwise.
*/
return async function ensureDefaultIndexPattern(this: IndexPatternsContract) {
// If workspace is enabled, only workspace owner can update ui setting.
if (isCurrentWorkspaceOwner === false) {
return;

Check warning on line 49 in src/plugins/data/common/index_patterns/index_patterns/ensure_default_index_pattern.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/index_patterns/index_patterns/ensure_default_index_pattern.ts#L49

Added line #L49 was not covered by tests
}
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;
isCurrentWorkspaceOwner?: boolean;
}

export class IndexPatternsService {
Expand All @@ -96,6 +97,7 @@ export class IndexPatternsService {
onError,
onUnsupportedTimePattern,
onRedirectNoIndexPattern = () => {},
isCurrentWorkspaceOwner,
}: 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,
isCurrentWorkspaceOwner
);
}

Expand Down
13 changes: 12 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,9 @@ export class DataPublicPlugin
notifications.toasts,
application.navigateToApp
),
isCurrentWorkspaceOwner:
workspaces?.currentWorkspace$.getValue()?.owner ||
application.capabilities?.dashboards?.isDashboardAdmin !== false,
yubonluo marked this conversation as resolved.
Show resolved Hide resolved
});
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
10 changes: 9 additions & 1 deletion src/plugins/workspace/public/workspace_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,21 @@ export class WorkspaceClient implements IWorkspaceClient {
perPage: 999,
permissionModes: [WorkspacePermissionMode.LibraryWrite],
});
if (resultWithWritePermission?.success) {
const resultWithOwnerPermission = await this.list({
perPage: 999,
permissionModes: [WorkspacePermissionMode.Write],
});
yubonluo marked this conversation as resolved.
Show resolved Hide resolved
yubonluo marked this conversation as resolved.
Show resolved Hide resolved
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
Loading