Skip to content

Commit

Permalink
Replace savedObjectReposiotry with savedObjectClient for workspace na…
Browse files Browse the repository at this point in the history
…me validation (#156) (#160)

(cherry picked from commit f350eb3)

Signed-off-by: Hailong Cui <[email protected]>
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>
  • Loading branch information
1 parent b32cf3f commit f3af5c9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/plugins/workspace/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
11 changes: 7 additions & 4 deletions src/plugins/workspace/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<{}, {}> {
Expand Down Expand Up @@ -71,7 +75,7 @@ export class WorkspacePlugin implements Plugin<{}, {}> {

core.savedObjects.addClientWrapper(
0,
'workspace',
WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID,
workspaceSavedObjectsClientWrapper.wrapperFactory
);

Expand Down Expand Up @@ -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],
{
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/workspace/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
WorkspacePermissionMode,
Permissions,
WorkspaceAttribute,
ISavedObjectsRepository,
SavedObjectsServiceStart,
} from '../../../core/server';

export interface WorkspaceAttributeWithPermission extends WorkspaceAttribute {
Expand All @@ -36,7 +36,7 @@ export interface IRequestDetail {

export interface IWorkspaceDBImpl {
setup(dep: CoreSetup): Promise<IResponse<boolean>>;
setInternalRepository(repository: ISavedObjectsRepository): void;
setSavedObjectes(savedObjectes: SavedObjectsServiceStart): void;
create(
requestDetail: IRequestDetail,
payload: Omit<WorkspaceAttributeWithPermission, 'id'>
Expand Down
34 changes: 23 additions & 11 deletions src/plugins/workspace/server/workspace_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
SavedObjectsClientContract,
CoreSetup,
WorkspaceAttribute,
ISavedObjectsRepository,
SavedObjectsServiceStart,
} from '../../../core/server';
import { WORKSPACE_TYPE } from '../../../core/server';
import {
Expand All @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -156,12 +166,14 @@ export class WorkspaceClientWithSavedObject implements IWorkspaceDBImpl {
const client = this.getSavedObjectClientsFromRequestDetail(requestDetail);
const workspaceInDB: SavedObject<WorkspaceAttribute> = 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);
}
Expand Down

0 comments on commit f3af5c9

Please sign in to comment.