Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Aug 27, 2023
1 parent 7bac360 commit 91605cc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
13 changes: 13 additions & 0 deletions src/plugins/workspace/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
this.workspaceSavedObjectsClientWrapper.wrapperFactory
);

core.savedObjects.setClientFactoryProvider(
(repositoryFactory) => ({ request, includedHiddenTypes }) => {
const enabled = this.isEnabled;
if (enabled) {
return new SavedObjectsClient(repositoryFactory.createInternalRepository());
}

return new SavedObjectsClient(
repositoryFactory.createScopedRepository(request, includedHiddenTypes)
);
}
);

this.proxyWorkspaceTrafficToRealHandler(core);

registerRoutes({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,6 @@ export class WorkspaceSavedObjectsClientWrapper {
}

public wrapperFactory: SavedObjectsClientWrapperFactory = (wrapperOptions) => {
/**
* The client here is scopedSavedObjectsClient by default
*/
let client = wrapperOptions.client;
const featureFlagEnabled = this.options.enabled$.getValue();

if (!featureFlagEnabled) {
return client;
}

/**
* If featureFlag is open and we have internalRepositoryFactory
* Use internal repository as access control will be provided by ACL.
*/
if (featureFlagEnabled && this.internalRepositoryFactory) {
client = new SavedObjectsClient(this.internalRepositoryFactory());
}
const deleteWithWorkspacePermissionControl = async (
type: string,
id: string,
Expand All @@ -191,13 +174,13 @@ export class WorkspaceSavedObjectsClientWrapper {
]);
}

const objectToDeleted = await client.get(type, id, options);
const objectToDeleted = await wrapperOptions.client.get(type, id, options);
await this.validateMultiWorkspacesPermissions(
objectToDeleted.workspaces,
wrapperOptions.request,
[WorkspacePermissionMode.LibraryWrite, WorkspacePermissionMode.Management]
);
return await client.delete(type, id, options);
return await wrapperOptions.client.delete(type, id, options);
};

const updateWithWorkspacePermissionControl = async <T = unknown>(
Expand All @@ -211,7 +194,7 @@ export class WorkspaceSavedObjectsClientWrapper {
WorkspacePermissionMode.Management,
]);
}
return await client.update(type, id, attributes, options);
return await wrapperOptions.client.update(type, id, attributes, options);
};

const bulkUpdateWithWorkspacePermissionControl = async <T = unknown>(
Expand All @@ -233,7 +216,7 @@ export class WorkspaceSavedObjectsClientWrapper {
throw generateWorkspacePermissionError();
}

return await client.bulkUpdate(objects, options);
return await wrapperOptions.client.bulkUpdate(objects, options);
};

const bulkCreateWithWorkspacePermissionControl = async <T = unknown>(
Expand All @@ -246,7 +229,7 @@ export class WorkspaceSavedObjectsClientWrapper {
WorkspacePermissionMode.Management,
]);
}
return await client.bulkCreate(objects, options);
return await wrapperOptions.client.bulkCreate(objects, options);
};

const createWithWorkspacePermissionControl = async <T = unknown>(
Expand All @@ -261,15 +244,15 @@ export class WorkspaceSavedObjectsClientWrapper {
[WorkspacePermissionMode.LibraryWrite, WorkspacePermissionMode.Management]
);
}
return await client.create(type, attributes, options);
return await wrapperOptions.client.create(type, attributes, options);
};

const getWithWorkspacePermissionControl = async <T = unknown>(
type: string,
id: string,
options: SavedObjectsBaseOptions = {}
): Promise<SavedObject<T>> => {
const objectToGet = await client.get<T>(type, id, options);
const objectToGet = await wrapperOptions.client.get<T>(type, id, options);
await this.validateAtLeastOnePermittedWorkspaces(
objectToGet.workspaces,
wrapperOptions.request,
Expand All @@ -286,7 +269,7 @@ export class WorkspaceSavedObjectsClientWrapper {
objects: SavedObjectsBulkGetObject[] = [],
options: SavedObjectsBaseOptions = {}
): Promise<SavedObjectsBulkResponse<T>> => {
const objectToBulkGet = await client.bulkGet<T>(objects, options);
const objectToBulkGet = await wrapperOptions.client.bulkGet<T>(objects, options);
for (const object of objectToBulkGet.saved_objects) {
await this.validateAtLeastOnePermittedWorkspaces(
object.workspaces,
Expand Down Expand Up @@ -395,7 +378,7 @@ export class WorkspaceSavedObjectsClientWrapper {
}
}

return await client.find<T>(options);
return await wrapperOptions.client.find<T>(options);
};

const addToWorkspacesWithPermissionControl = async (
Expand All @@ -422,24 +405,24 @@ export class WorkspaceSavedObjectsClientWrapper {
throw generateSavedObjectsPermissionError();
}

return await client.addToWorkspaces(objects, targetWorkspaces, options);
return await wrapperOptions.client.addToWorkspaces(objects, targetWorkspaces, options);
};

const isDashboardAdmin = this.isDashboardAdmin(wrapperOptions.request);

if (isDashboardAdmin) {
return client;
return wrapperOptions.client;
}

return {
...client,
...wrapperOptions.client,
get: getWithWorkspacePermissionControl,
checkConflicts: client.checkConflicts,
checkConflicts: wrapperOptions.client.checkConflicts,
find: findWithWorkspacePermissionControl,
bulkGet: bulkGetWithWorkspacePermissionControl,
errors: client.errors,
addToNamespaces: client.addToNamespaces,
deleteFromNamespaces: client.deleteFromNamespaces,
errors: wrapperOptions.client.errors,
addToNamespaces: wrapperOptions.client.addToNamespaces,
deleteFromNamespaces: wrapperOptions.client.deleteFromNamespaces,
create: createWithWorkspacePermissionControl,
bulkCreate: bulkCreateWithWorkspacePermissionControl,
delete: deleteWithWorkspacePermissionControl,
Expand Down

0 comments on commit 91605cc

Please sign in to comment.