From 11c20ec1f9083970fa3ce092fea695d19704cf46 Mon Sep 17 00:00:00 2001 From: Hailong Cui Date: Thu, 14 Sep 2023 12:04:27 +0800 Subject: [PATCH] Replace savedObjectReposiotry with savedObjectClient for workspace name validation Signed-off-by: Hailong Cui --- src/plugins/workspace/common/constants.ts | 1 + src/plugins/workspace/server/plugin.ts | 11 +++--- src/plugins/workspace/server/types.ts | 4 +-- .../workspace/server/workspace_client.ts | 34 +++++++++++++------ 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/plugins/workspace/common/constants.ts b/src/plugins/workspace/common/constants.ts index b4cf30700f19..036aa4ef7ce6 100644 --- a/src/plugins/workspace/common/constants.ts +++ b/src/plugins/workspace/common/constants.ts @@ -21,6 +21,7 @@ export const PATHS = { }; export const WORKSPACE_OP_TYPE_CREATE = 'create'; export const WORKSPACE_OP_TYPE_UPDATE = 'update'; +export const WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID = 'workspace'; export const WORKSPACE_NAV_CATEGORY: AppCategory = { id: 'workspace', diff --git a/src/plugins/workspace/server/plugin.ts b/src/plugins/workspace/server/plugin.ts index d1c68e4bd070..508b5c79d7b2 100644 --- a/src/plugins/workspace/server/plugin.ts +++ b/src/plugins/workspace/server/plugin.ts @@ -26,7 +26,11 @@ import { IWorkspaceDBImpl } from './types'; import { WorkspaceClientWithSavedObject } from './workspace_client'; import { WorkspaceSavedObjectsClientWrapper } from './saved_objects'; import { registerRoutes } from './routes'; -import { WORKSPACE_OVERVIEW_APP_ID, WORKSPACE_UPDATE_APP_ID } from '../common/constants'; +import { + WORKSPACE_OVERVIEW_APP_ID, + WORKSPACE_UPDATE_APP_ID, + WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID, +} from '../common/constants'; import { ConfigSchema } from '../config'; export class WorkspacePlugin implements Plugin<{}, {}> { @@ -71,7 +75,7 @@ export class WorkspacePlugin implements Plugin<{}, {}> { core.savedObjects.addClientWrapper( 0, - 'workspace', + WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID, workspaceSavedObjectsClientWrapper.wrapperFactory ); @@ -122,7 +126,6 @@ export class WorkspacePlugin implements Plugin<{}, {}> { private async setupWorkspaces(startDeps: CoreStart) { const internalRepository = startDeps.savedObjects.createInternalRepository(); - this.client?.setInternalRepository(internalRepository); const publicWorkspaceACL = new ACL().addPermission( [WorkspacePermissionMode.LibraryRead, WorkspacePermissionMode.LibraryWrite], { @@ -165,7 +168,7 @@ export class WorkspacePlugin implements Plugin<{}, {}> { public start(core: CoreStart) { this.logger.debug('Starting SavedObjects service'); - + this.client?.setSavedObjectes(core.savedObjects); this.setupWorkspaces(core); return { diff --git a/src/plugins/workspace/server/types.ts b/src/plugins/workspace/server/types.ts index c87588535d88..d8eef9c13389 100644 --- a/src/plugins/workspace/server/types.ts +++ b/src/plugins/workspace/server/types.ts @@ -11,7 +11,7 @@ import { WorkspacePermissionMode, Permissions, WorkspaceAttribute, - ISavedObjectsRepository, + SavedObjectsServiceStart, } from '../../../core/server'; export interface WorkspaceAttributeWithPermission extends WorkspaceAttribute { @@ -36,7 +36,7 @@ export interface IRequestDetail { export interface IWorkspaceDBImpl { setup(dep: CoreSetup): Promise>; - setInternalRepository(repository: ISavedObjectsRepository): void; + setSavedObjectes(savedObjectes: SavedObjectsServiceStart): void; create( requestDetail: IRequestDetail, payload: Omit diff --git a/src/plugins/workspace/server/workspace_client.ts b/src/plugins/workspace/server/workspace_client.ts index ad2062e68487..6a1b36e11c38 100644 --- a/src/plugins/workspace/server/workspace_client.ts +++ b/src/plugins/workspace/server/workspace_client.ts @@ -8,7 +8,7 @@ import type { SavedObjectsClientContract, CoreSetup, WorkspaceAttribute, - ISavedObjectsRepository, + SavedObjectsServiceStart, } from '../../../core/server'; import { WORKSPACE_TYPE } from '../../../core/server'; import { @@ -20,6 +20,7 @@ import { } from './types'; import { workspace } from './saved_objects'; import { generateRandomId } from './utils'; +import { WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID } from '../common/constants'; const WORKSPACE_ID_SIZE = 6; @@ -30,9 +31,18 @@ const DUPLICATE_WORKSPACE_NAME_ERROR = i18n.translate('workspace.duplicate.name. export class WorkspaceClientWithSavedObject implements IWorkspaceDBImpl { private setupDep: CoreSetup; - private internalSavedObjectsRepository?: ISavedObjectsRepository; - setInternalRepository(repository: ISavedObjectsRepository) { - this.internalSavedObjectsRepository = repository; + private savedObjects?: SavedObjectsServiceStart; + + setSavedObjectes(savedObjects: SavedObjectsServiceStart) { + this.savedObjects = savedObjects; + } + + private getScopeClientWithoutPermisson( + requestDetail: IRequestDetail + ): SavedObjectsClientContract | undefined { + return this.savedObjects?.getScopedClient(requestDetail.request, { + excludedWrappers: [WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID], + }); } constructor(core: CoreSetup) { @@ -70,7 +80,7 @@ export class WorkspaceClientWithSavedObject implements IWorkspaceDBImpl { const { permissions, ...attributes } = payload; const id = generateRandomId(WORKSPACE_ID_SIZE); const client = this.getSavedObjectClientsFromRequestDetail(requestDetail); - const existingWorkspaceRes = await this.internalSavedObjectsRepository?.find({ + const existingWorkspaceRes = await this.getScopeClientWithoutPermisson(requestDetail)?.find({ type: WORKSPACE_TYPE, search: attributes.name, searchFields: ['name'], @@ -156,12 +166,14 @@ export class WorkspaceClientWithSavedObject implements IWorkspaceDBImpl { const client = this.getSavedObjectClientsFromRequestDetail(requestDetail); const workspaceInDB: SavedObject = await client.get(WORKSPACE_TYPE, id); if (workspaceInDB.attributes.name !== attributes.name) { - const existingWorkspaceRes = await this.internalSavedObjectsRepository?.find({ - type: WORKSPACE_TYPE, - search: attributes.name, - searchFields: ['name'], - fields: ['_id'], - }); + const existingWorkspaceRes = await this.getScopeClientWithoutPermisson(requestDetail)?.find( + { + type: WORKSPACE_TYPE, + search: attributes.name, + searchFields: ['name'], + fields: ['_id'], + } + ); if (existingWorkspaceRes && existingWorkspaceRes.total > 0) { throw new Error(DUPLICATE_WORKSPACE_NAME_ERROR); }