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 8, 2023
1 parent e1fee65 commit 6a44fce
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export class SavedObjectsRepository {
initialNamespaces,
version,
workspaces,
permissions,
} = options;
const namespace = normalizeNamespace(options.namespace);

Expand Down Expand Up @@ -310,6 +311,7 @@ export class SavedObjectsRepository {
updated_at: time,
...(Array.isArray(references) && { references }),
...(Array.isArray(savedObjectWorkspaces) && { workspaces: savedObjectWorkspaces }),
...(permissions && { permissions }),
});

const raw = this._serializer.savedObjectToRaw(migrated as SavedObjectSanitizedDoc);
Expand Down
2 changes: 2 additions & 0 deletions src/core/server/saved_objects/service/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
SavedObjectsFindOptions,
} from '../types';
import { SavedObjectsErrorHelpers } from './lib/errors';
import { Permissions } from '../permission_control/acl';

/**
*
Expand Down Expand Up @@ -68,6 +69,7 @@ export interface SavedObjectsCreateOptions extends SavedObjectsBaseOptions {
* Note: this can only be used for multi-namespace object types.
*/
initialNamespaces?: string[];
permissions?: Permissions;
}

/**
Expand Down
44 changes: 30 additions & 14 deletions src/core/server/workspaces/workspaces_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ import { IWorkspaceDBImpl, WorkspaceAttribute } from './types';
import { WorkspacesClientWithSavedObject } from './workspaces_client';
import { WorkspaceSavedObjectsClientWrapper } from './saved_objects';
import { WORKSPACE_TYPE } from './constants';
import { MANAGEMENT_WORKSPACE, PUBLIC_WORKSPACE, PermissionMode } from '../../utils';
import { ACL } from '../saved_objects/permission_control/acl';
import {
MANAGEMENT_WORKSPACE,
PUBLIC_WORKSPACE,
PermissionMode,
DASHBOARD_ADMIN_GROUP,
} from '../../utils';
import { ACL, Permissions } from '../saved_objects/permission_control/acl';

export interface WorkspacesServiceSetup {
client: IWorkspaceDBImpl;
Expand Down Expand Up @@ -98,7 +103,8 @@ export class WorkspacesService
private async checkAndCreateWorkspace(
internalRepository: ISavedObjectsRepository,
workspaceId: string,
workspaceAttribute: Omit<WorkspaceAttribute, 'id'>
workspaceAttribute: Omit<WorkspaceAttribute, 'id'>,
permissions?: Permissions
) {
/**
* Internal repository is attached to global tenant.
Expand All @@ -111,6 +117,7 @@ export class WorkspacesService
try {
const createResult = await internalRepository.create(WORKSPACE_TYPE, workspaceAttribute, {
id: workspaceId,
permissions,
});
if (createResult.id) {
this.logger.info(`Created workspace ${createResult.id} in global tenant.`);
Expand All @@ -122,31 +129,40 @@ export class WorkspacesService
}

private async setupWorkspaces(startDeps: WorkpsaceStartDeps) {
return;
const internalRepository = startDeps.savedObjects.createInternalRepository();
const publicWorkspaceACL = new ACL()
.addPermission([PermissionMode.LibraryRead, PermissionMode.LibraryWrite], {
users: ['*'],
})
.addPermission([PermissionMode.Management], {
groups: ['dashboard_admin'],
groups: [DASHBOARD_ADMIN_GROUP],
});
const managementWorkspaceACL = new ACL()
.addPermission([PermissionMode.LibraryRead], {
users: ['*'],
})
.addPermission([PermissionMode.Management], {
groups: ['dashboard_admin'],
.addPermission([PermissionMode.Management, PermissionMode.LibraryWrite], {
groups: [DASHBOARD_ADMIN_GROUP],
});

await Promise.all([
this.checkAndCreateWorkspace(internalRepository, PUBLIC_WORKSPACE, {
name: 'public',
permissions: publicWorkspaceACL,
}),
this.checkAndCreateWorkspace(internalRepository, MANAGEMENT_WORKSPACE, {
name: 'Management',
permissions: managementWorkspaceACL,
}),
this.checkAndCreateWorkspace(
internalRepository,
PUBLIC_WORKSPACE,
{
name: 'public',
},
publicWorkspaceACL.getPermissions()
),
this.checkAndCreateWorkspace(
internalRepository,
MANAGEMENT_WORKSPACE,
{
name: 'Management',
},
managementWorkspaceACL.getPermissions()
),
]);
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ export const PUBLIC_WORKSPACE = 'public';
export const MANAGEMENT_WORKSPACE = 'management';

export const WORKSPACE_FEATURE_FLAG_KEY_IN_UI_SETTINGS = 'workspace:enabled';

export const DASHBOARD_ADMIN_GROUP = 'dashboard_admin';
1 change: 1 addition & 0 deletions src/core/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ export {
PUBLIC_WORKSPACE,
WORKSPACE_FEATURE_FLAG_KEY_IN_UI_SETTINGS,
MANAGEMENT_WORKSPACE,
DASHBOARD_ADMIN_GROUP,
} from './constants';

0 comments on commit 6a44fce

Please sign in to comment.