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

write permisions outside instead of workspace attributes #79

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
4 changes: 2 additions & 2 deletions src/core/public/workspace/workspaces_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class WorkspacesClient {
* @returns
*/
public async create(
attributes: Omit<WorkspaceAttribute, 'id' | 'permissions'> & {
attributes: Omit<WorkspaceAttribute, 'id'> & {
permissions: WorkspaceRoutePermissionItem[];
}
): Promise<IResponse<WorkspaceAttribute>> {
Expand Down Expand Up @@ -280,7 +280,7 @@ export class WorkspacesClient {
public async update(
id: string,
attributes: Partial<
Omit<WorkspaceAttribute, 'permissions'> & {
WorkspaceAttribute & {
permissions: WorkspaceRoutePermissionItem[];
}
>
Expand Down
5 changes: 4 additions & 1 deletion src/core/server/saved_objects/serialization/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class SavedObjectsSerializer {
*/
public rawToSavedObject(doc: SavedObjectsRawDoc): SavedObjectSanitizedDoc {
const { _id, _source, _seq_no, _primary_term } = doc;
const { type, namespace, namespaces, originId, workspaces } = _source;
const { type, namespace, namespaces, originId, workspaces, permissions } = _source;

const version =
_seq_no != null || _primary_term != null
Expand All @@ -92,6 +92,7 @@ export class SavedObjectsSerializer {
...(_source.updated_at && { updated_at: _source.updated_at }),
...(version && { version }),
...(workspaces && { workspaces }),
...(permissions && { permissions }),
};
}

Expand All @@ -114,6 +115,7 @@ export class SavedObjectsSerializer {
version,
references,
workspaces,
permissions,
} = savedObj;
const source = {
[type]: attributes,
Expand All @@ -125,6 +127,7 @@ export class SavedObjectsSerializer {
...(migrationVersion && { migrationVersion }),
...(updated_at && { updated_at }),
...(workspaces && { workspaces }),
...(permissions && { permissions }),
};

return {
Expand Down
1 change: 1 addition & 0 deletions src/core/server/saved_objects/serialization/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ interface SavedObjectDoc<T = unknown> {
updated_at?: string;
originId?: string;
workspaces?: string[];
permissions?: Permissions;
}

interface Referencable {
Expand Down
8 changes: 6 additions & 2 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 Expand Up @@ -1012,7 +1014,7 @@ export class SavedObjectsRepository {
throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id);
}

const { originId, updated_at: updatedAt, workspaces } = body._source;
const { originId, updated_at: updatedAt, workspaces, permissions } = body._source;

let namespaces: string[] = [];
if (!this._registry.isNamespaceAgnostic(type)) {
Expand All @@ -1028,6 +1030,7 @@ export class SavedObjectsRepository {
...(originId && { originId }),
...(updatedAt && { updated_at: updatedAt }),
...(workspaces && { workspaces }),
...(permissions && { permissions }),
version: encodeHitVersion(body),
attributes: body._source[type],
references: body._source.references || [],
Expand Down Expand Up @@ -1056,7 +1059,7 @@ export class SavedObjectsRepository {
throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id);
}

const { version, references, refresh = DEFAULT_REFRESH_SETTING } = options;
const { version, references, refresh = DEFAULT_REFRESH_SETTING, permissions } = options;
const namespace = normalizeNamespace(options.namespace);

let preflightResult: SavedObjectsRawDoc | undefined;
Expand All @@ -1070,6 +1073,7 @@ export class SavedObjectsRepository {
[type]: attributes,
updated_at: time,
...(Array.isArray(references) && { references }),
...(permissions && { permissions }),
};

const { body, statusCode } = await this.client.update<SavedObjectsRawDocSource>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
* under the License.
*/

import { Permissions } from '../permission_control/acl';

import { ISavedObjectsRepository } from './lib';
import {
SavedObject,
Expand Down Expand Up @@ -68,6 +70,8 @@ export interface SavedObjectsCreateOptions extends SavedObjectsBaseOptions {
* Note: this can only be used for multi-namespace object types.
*/
initialNamespaces?: string[];
/** permission control describe by ACL object */
permissions?: Permissions;
}

/**
Expand Down Expand Up @@ -182,6 +186,8 @@ export interface SavedObjectsUpdateOptions extends SavedObjectsBaseOptions {
references?: SavedObjectReference[];
/** The OpenSearch Refresh setting for this operation */
refresh?: MutatingOperationRefreshSetting;
/** permission control describe by ACL object */
permissions?: Permissions;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/core/server/workspaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export {
InternalWorkspacesServiceStart,
} from './workspaces_service';

export { WorkspaceAttribute, WorkspaceFindOptions } from './types';
export {
WorkspaceAttribute,
WorkspaceFindOptions,
WorkspaceAttributeWithPermission,
} from './types';

export { workspacesValidator, formatWorkspaces } from './utils';
export { WORKSPACE_TYPE } from './constants';
14 changes: 10 additions & 4 deletions src/core/server/workspaces/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export interface WorkspaceAttribute {
color?: string;
icon?: string;
defaultVISTheme?: string;
}

export interface WorkspaceAttributeWithPermission extends WorkspaceAttribute {
permissions: Permissions;
}

Expand All @@ -44,23 +47,26 @@ export interface IWorkspaceDBImpl {
setup(dep: WorkspacesSetupDeps): Promise<IResponse<boolean>>;
create(
requestDetail: IRequestDetail,
payload: Omit<WorkspaceAttribute, 'id'>
payload: Omit<WorkspaceAttributeWithPermission, 'id'>
): Promise<IResponse<{ id: WorkspaceAttribute['id'] }>>;
list(
requestDetail: IRequestDetail,
options: WorkspaceFindOptions
): Promise<
IResponse<
{
workspaces: WorkspaceAttribute[];
workspaces: WorkspaceAttributeWithPermission[];
} & Pick<SavedObjectsFindResponse, 'page' | 'per_page' | 'total'>
>
>;
get(requestDetail: IRequestDetail, id: string): Promise<IResponse<WorkspaceAttribute>>;
get(
requestDetail: IRequestDetail,
id: string
): Promise<IResponse<WorkspaceAttributeWithPermission>>;
update(
requestDetail: IRequestDetail,
id: string,
payload: Omit<WorkspaceAttribute, 'id'>
payload: Omit<WorkspaceAttributeWithPermission, 'id'>
): Promise<IResponse<boolean>>;
delete(requestDetail: IRequestDetail, id: string): Promise<IResponse<boolean>>;
destroy(): Promise<IResponse<boolean>>;
Expand Down
26 changes: 17 additions & 9 deletions src/core/server/workspaces/workspaces_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
WorkspaceFindOptions,
IResponse,
IRequestDetail,
WorkspaceAttributeWithPermission,
} from './types';
import { WorkspacesSetupDeps } from './workspaces_service';
import { workspace } from './saved_objects';
Expand All @@ -24,11 +25,12 @@ export class WorkspacesClientWithSavedObject implements IWorkspaceDBImpl {
): SavedObjectsClientContract {
return requestDetail.context.core.savedObjects.client;
}
private getFlatternedResultWithSavedObject(
private getFlattenedResultWithSavedObject(
savedObject: SavedObject<WorkspaceAttribute>
): WorkspaceAttribute {
): WorkspaceAttributeWithPermission {
return {
...savedObject.attributes,
permissions: savedObject.permissions || {},
id: savedObject.id,
};
}
Expand All @@ -44,12 +46,15 @@ export class WorkspacesClientWithSavedObject implements IWorkspaceDBImpl {
}
public async create(
requestDetail: IRequestDetail,
payload: Omit<WorkspaceAttribute, 'id'>
payload: Omit<WorkspaceAttributeWithPermission, 'id'>
): ReturnType<IWorkspaceDBImpl['create']> {
try {
const { permissions, ...attributes } = payload;
const result = await this.getSavedObjectClientsFromRequestDetail(requestDetail).create<
Omit<WorkspaceAttribute, 'id'>
>(WORKSPACE_TYPE, payload);
>(WORKSPACE_TYPE, attributes, {
permissions,
});
return {
success: true,
result: {
Expand Down Expand Up @@ -81,7 +86,7 @@ export class WorkspacesClientWithSavedObject implements IWorkspaceDBImpl {
success: true,
result: {
...others,
workspaces: savedObjects.map((item) => this.getFlatternedResultWithSavedObject(item)),
workspaces: savedObjects.map((item) => this.getFlattenedResultWithSavedObject(item)),
},
};
} catch (e: unknown) {
Expand All @@ -94,14 +99,14 @@ export class WorkspacesClientWithSavedObject implements IWorkspaceDBImpl {
public async get(
requestDetail: IRequestDetail,
id: string
): Promise<IResponse<WorkspaceAttribute>> {
): Promise<IResponse<WorkspaceAttributeWithPermission>> {
try {
const result = await this.getSavedObjectClientsFromRequestDetail(requestDetail).get<
WorkspaceAttribute
>(WORKSPACE_TYPE, id);
return {
success: true,
result: this.getFlatternedResultWithSavedObject(result),
result: this.getFlattenedResultWithSavedObject(result),
};
} catch (e: unknown) {
return {
Expand All @@ -113,12 +118,15 @@ export class WorkspacesClientWithSavedObject implements IWorkspaceDBImpl {
public async update(
requestDetail: IRequestDetail,
id: string,
payload: Omit<WorkspaceAttribute, 'id'>
payload: Omit<WorkspaceAttributeWithPermission, 'id'>
): Promise<IResponse<boolean>> {
const { permissions, ...attributes } = payload;
try {
await this.getSavedObjectClientsFromRequestDetail(requestDetail).update<
Omit<WorkspaceAttribute, 'id'>
>(WORKSPACE_TYPE, id, payload);
>(WORKSPACE_TYPE, id, attributes, {
permissions,
});
return {
success: true,
result: true,
Expand Down
Loading