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 all 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,17 @@

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;

Check warning on line 48 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#L48

Added line #L48 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;
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 @@
}

public start(core: CoreStart, { uiActions }: DataStartDependencies): DataPublicPluginStart {
const { uiSettings, http, notifications, savedObjects, overlays, application } = core;
const {
uiSettings,
http,
notifications,
savedObjects,
overlays,
application,
workspaces,
} = core;

Check warning on line 204 in src/plugins/data/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/plugin.ts#L204

Added line #L204 was not covered by tests
setNotifications(notifications);
setOverlays(overlays);
setUiSettings(uiSettings);
Expand All @@ -220,6 +228,12 @@
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
Loading