Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

23.0 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import type { Workbook, Worksheet } from '@univerjs/core';
import type { IDrawingGroupUpdateParam } from '@univerjs/drawing';
import type {
    IInsertSheetDrawingCommandParams,
    IRemoveSheetDrawingCommandParams,
    ISetDrawingArrangeCommandParams,
    ISetDrawingCommandParams,
} from '@univerjs/sheets-drawing';
import {
    Disposable,
    ICommandService,
    Inject,
    IPermissionService,
    IUniverInstanceService,
    LocaleService,
    SheetTypes,
    UniverInstanceType,
    UserManagerService,
} from '@univerjs/core';
import { IRenderManagerService, ObjectType } from '@univerjs/engine-render';
import {
    SheetPermissionCheckController,
    WorkbookEditablePermission,
    WorkbookViewPermission,
    WorksheetEditPermission,
    WorksheetViewPermission,
} from '@univerjs/sheets';
import {
    InsertSheetDrawingCommand,
    ISheetDrawingService,
    RemoveSheetDrawingCommand,
    SetDrawingArrangeCommand,
    SetSheetDrawingCommand,
    SetSheetDrawingPlacementCommand,
} from '@univerjs/sheets-drawing';
import { combineLatest, distinctUntilChanged, EMPTY, map, switchMap, tap } from 'rxjs';
import type { IFlipSheetDrawingCommandParams } from '../commands/commands/flip-drawings.command';
import type { LocaleKey } from '../locale/types';
import { FlipSheetDrawingCommand } from '../commands/commands/flip-drawings.command';
import { GroupSheetDrawingCommand } from '../commands/commands/group-sheet-drawing.command';
import { UngroupSheetDrawingCommand } from '../commands/commands/ungroup-sheet-drawing.command';

const drawingObjectTypes = [
    ObjectType.IMAGE, // floating image
    ObjectType.SHAPE, // shape
    ObjectType.CHART, // chart rect
    ObjectType.DRAWING_DOM, // floating dom rect
];

export class SheetDrawingPermissionController extends Disposable {
    constructor(
        @Inject(ICommandService) private _commandService: ICommandService,
        @Inject(LocaleService) private _localeService: LocaleService,
        @IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
        @IPermissionService private readonly _permissionService: IPermissionService,
        @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
        @Inject(UserManagerService) private _userManagerService: UserManagerService,
        @Inject(SheetPermissionCheckController) private _sheetPermissionCheckController: SheetPermissionCheckController,
        @Inject(ISheetDrawingService) private readonly _sheetDrawingService: ISheetDrawingService
    ) {
        super();
        this._initDrawingVisible();
        this._initDrawingEditable();
        this._initViewPermissionChange();
        this._initEditPermissionChange();
        this._initCommandPermissionCheck();
        this._initChartSheetCommandPermissionCheck();
    }

    private _initDrawingVisible() {
        const workbook$ = this._univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET);
        const currentUser$ = this._userManagerService.currentUser$;
        const combined$ = combineLatest([workbook$, currentUser$]);

        this.disposeWithMe(
            combined$
                .pipe(
                    switchMap(([workbook, _]) => {
                        if (!workbook) {
                            this._sheetDrawingService.setDrawingVisible(false);
                            return EMPTY;
                        }

                        return workbook.activeSheet$.pipe(
                            tap((sheet) => {
                                if (!sheet) {
                                    this._sheetDrawingService.setDrawingVisible(false);
                                    return;
                                }

                                const unitId = workbook.getUnitId();
                                const subUnitId = sheet.getSheetId();

                                const worksheetViewPermission = this._permissionService
                                    .composePermission([
                                        new WorkbookViewPermission(unitId).id,
                                        new WorksheetViewPermission(unitId, subUnitId).id,
                                    ])
                                    .every((permission) => permission.value);

                                if (worksheetViewPermission) {
                                    this._sheetDrawingService.setDrawingVisible(true);
                                } else {
                                    this._handleDrawingVisibilityFalse(workbook, sheet);
                                }
                            })
                        );
                    })
                )
                .subscribe()
        );
    }

    private _handleDrawingVisibilityFalse(workbook: Workbook, sheet: Worksheet) {
        this._sheetDrawingService.setDrawingVisible(false);

        const unitId = workbook.getUnitId();
        const subUnitId = sheet.getSheetId();
        const drawingData = this._sheetDrawingService.getDrawingData(unitId, subUnitId);
        const drawingDataValues = Object.values(drawingData);

        const renderObject = this._renderManagerService.getRenderUnitById(unitId);
        const scene = renderObject?.scene;

        if (!scene) {
            return;
        }

        const objects = scene.getAllObjectsByOrder();

        objects.forEach((object) => {
            if (
                drawingObjectTypes.includes(object.objectType) &&
                drawingDataValues.some((item) => object.oKey.includes(item.drawingId))
            ) {
                scene.removeObject(object);
            }
        });
    }

    private _initDrawingEditable() {
        const workbook$ = this._univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET);
        const currentUser$ = this._userManagerService.currentUser$;

        const combined$ = combineLatest([workbook$, currentUser$]);

        this.disposeWithMe(
            combined$
                .pipe(
                    switchMap(([workbook, _]) => {
                        if (!workbook) {
                            this._sheetDrawingService.setDrawingEditable(false);
                            return EMPTY;
                        }

                        return workbook.activeSheet$.pipe(
                            tap((sheet) => {
                                if (!sheet) {
                                    this._sheetDrawingService.setDrawingEditable(false);
                                    return;
                                }

                                const unitId = workbook.getUnitId();
                                const subUnitId = sheet.getSheetId();

                                const worksheetEditPermission = this._permissionService
                                    .composePermission([
                                        new WorkbookEditablePermission(unitId).id,
                                        new WorksheetEditPermission(unitId, subUnitId).id,
                                    ])
                                    .every((permission) => permission.value);

                                if (worksheetEditPermission) {
                                    this._sheetDrawingService.setDrawingEditable(true);
                                } else {
                                    this._handleDrawingEditableFalse(workbook, sheet);
                                }
                            })
                        );
                    })
                )
                .subscribe()
        );
    }

    private _handleDrawingEditableFalse(workbook: Workbook, sheet: Worksheet) {
        this._sheetDrawingService.setDrawingEditable(false);

        const unitId = workbook.getUnitId();
        const subUnitId = sheet.getSheetId();
        const drawingData = this._sheetDrawingService.getDrawingData(unitId, subUnitId);
        const drawingDataValues = Object.values(drawingData);

        const renderObject = this._renderManagerService.getRenderUnitById(unitId);
        const scene = renderObject?.scene;

        if (!scene) {
            return;
        }

        const objects = scene.getAllObjectsByOrder();

        objects.forEach((object) => {
            if (
                drawingObjectTypes.includes(object.objectType) &&
                drawingDataValues.some((item) => object.oKey.includes(item.drawingId))
            ) {
                scene.detachTransformerFrom(object);
            }
        });
    }

    // eslint-disable-next-line max-lines-per-function
    private _initViewPermissionChange() {
        const workbook$ = this._univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET);
        const currentUser$ = this._userManagerService.currentUser$;
        this.disposeWithMe(
            combineLatest([workbook$, currentUser$])
                .pipe(
                    switchMap(([workbook, _]) => {
                        if (!workbook) return EMPTY;

                        return workbook.activeSheet$.pipe(
                            switchMap((sheet) => {
                                if (!sheet) {
                                    return EMPTY;
                                }

                                const unitId = workbook.getUnitId();
                                const subUnitId = sheet.getSheetId();
                                const renderObject = this._renderManagerService.getRenderUnitById(unitId);
                                const scene = renderObject?.scene;

                                if (!scene) {
                                    return EMPTY;
                                }

                                const transformer = scene.getTransformerByCreate();

                                const worksheetViewPermission$ = this._permissionService
                                    .composePermission$([
                                        new WorkbookViewPermission(unitId).id,
                                        new WorksheetViewPermission(unitId, subUnitId).id,
                                    ])
                                    .pipe(
                                        map((permissions) => permissions.every((item) => item.value)),
                                        distinctUntilChanged()
                                    );

                                return worksheetViewPermission$.pipe(
                                    map((permission) => ({
                                        permission,
                                        scene,
                                        transformer,
                                        unitId,
                                        subUnitId,
                                    }))
                                );
                            })
                        );
                    })
                )
                .subscribe({
                    next: ({ permission, scene, transformer, unitId, subUnitId }) => {
                        this._sheetDrawingService.setDrawingVisible(permission);

                        const objects = scene.getAllObjectsByOrder();
                        const drawingData = this._sheetDrawingService.getDrawingData(unitId, subUnitId);
                        const drawingDataValues = Object.values(drawingData);

                        if (permission) {
                            this._sheetDrawingService.addNotification(drawingDataValues);
                        } else {
                            objects.forEach((object) => {
                                if (
                                    drawingObjectTypes.includes(object.objectType) &&
                                    drawingDataValues.some((item) => object.oKey.includes(item.drawingId))
                                ) {
                                    scene.removeObject(object);
                                }
                            });
                            transformer.clearSelectedObjects();
                        }
                    },
                    complete: () => {
                        this._sheetDrawingService.setDrawingVisible(true);
                        const workbook = this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET);
                        const sheet = workbook?.getActiveSheet();
                        const unitId = workbook?.getUnitId();
                        const subUnitId = sheet?.getSheetId();
                        if (!unitId || !subUnitId) {
                            return;
                        }
                        const drawingData = this._sheetDrawingService.getDrawingData(unitId, subUnitId);
                        const drawingDataValues = Object.values(drawingData);
                        this._sheetDrawingService.addNotification(drawingDataValues);
                    },
                })
        );
    }

    // eslint-disable-next-line max-lines-per-function
    private _initEditPermissionChange() {
        const workbook$ = this._univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET);
        const currentUser$ = this._userManagerService.currentUser$;

        this.disposeWithMe(
            combineLatest([workbook$, currentUser$])
                .pipe(
                    switchMap(([workbook, _]) => {
                        if (!workbook) {
                            return EMPTY;
                        }

                        return workbook.activeSheet$.pipe(
                            switchMap((sheet) => {
                                if (!sheet) {
                                    return EMPTY;
                                }

                                const unitId = workbook.getUnitId();
                                const subUnitId = sheet.getSheetId();
                                const renderObject = this._renderManagerService.getRenderUnitById(unitId);
                                const scene = renderObject?.scene;

                                if (!scene) {
                                    return EMPTY;
                                }

                                const transformer = scene.getTransformerByCreate();

                                const composeWorksheetEditPermission$ = this._permissionService
                                    .composePermission$([
                                        new WorkbookEditablePermission(unitId).id,
                                        new WorksheetEditPermission(unitId, subUnitId).id,
                                    ])
                                    .pipe(
                                        map((permissions) => permissions.every((item) => item.value)),
                                        distinctUntilChanged()
                                    );

                                return composeWorksheetEditPermission$.pipe(
                                    map((permission) => ({
                                        permission,
                                        scene,
                                        transformer,
                                        unitId,
                                        subUnitId,
                                    }))
                                );
                            })
                        );
                    })
                )
                .subscribe({
                    next: ({ permission, scene, transformer, unitId, subUnitId }) => {
                        this._sheetDrawingService.setDrawingEditable(permission);

                        const objects = scene.getAllObjectsByOrder();
                        const drawingData = this._sheetDrawingService.getDrawingData(unitId, subUnitId);
                        const drawingDataValues = Object.values(drawingData);

                        if (permission) {
                            objects.forEach((object) => {
                                if (
                                    drawingObjectTypes.includes(object.objectType) &&
                                    drawingDataValues.some((item) => object.oKey.includes(item.drawingId))
                                ) {
                                    scene.attachTransformerTo(object);
                                }
                            });

                            this._sheetDrawingService.addNotification(drawingDataValues);
                        } else {
                            objects.forEach((object) => {
                                if (
                                    drawingObjectTypes.includes(object.objectType) &&
                                    drawingDataValues.some((item) => object.oKey.includes(item.drawingId))
                                ) {
                                    scene.detachTransformerFrom(object);
                                }
                            });

                            transformer.clearSelectedObjects();
                        }
                    },
                    complete: () => {
                        const workbook = this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET);
                        if (!workbook) {
                            return;
                        }

                        const unitId = workbook.getUnitId();
                        const sheet = workbook.getActiveSheet();
                        if (!sheet) {
                            return;
                        }

                        const subUnitId = sheet.getSheetId();
                        const renderObject = this._renderManagerService.getRenderUnitById(unitId);
                        const scene = renderObject?.scene;

                        if (!scene) {
                            return;
                        }

                        const drawingData = this._sheetDrawingService.getDrawingData(unitId, subUnitId);
                        const drawingDataValues = Object.values(drawingData);

                        this._sheetDrawingService.setDrawingEditable(true);

                        const objects = scene.getAllObjectsByOrder();
                        objects.forEach((object) => {
                            if (
                                drawingObjectTypes.includes(object.objectType) &&
                                drawingDataValues.some((item) => object.oKey.includes(item.drawingId))
                            ) {
                                scene.detachTransformerFrom(object);
                            }
                        });
                    },
                })
        );
    }

    private _initChartSheetCommandPermissionCheck(): void {
        this.disposeWithMe(this._commandService.beforeCommandExecuted((command) => {
            if (command.id === GroupSheetDrawingCommand.id || command.id === UngroupSheetDrawingCommand.id) {
                for (const group of (command.params as IDrawingGroupUpdateParam[] | undefined) ?? []) {
                    this._checkChartSheetEditPermission(group.parent.unitId, group.parent.subUnitId);
                    for (const child of group.children) {
                        this._checkChartSheetEditPermission(child.unitId, child.subUnitId);
                    }
                }
                return;
            }
            if (command.id === FlipSheetDrawingCommand.id) {
                const params = command.params as IFlipSheetDrawingCommandParams | undefined;
                for (const drawing of params?.drawings ?? []) {
                    this._checkChartSheetEditPermission(drawing.unitId, drawing.subUnitId);
                }
            }
        }));
    }

    private _checkChartSheetEditPermission(unitId: string, subUnitId: string): void {
        const worksheet = this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET)
            ?.getSheetBySheetId(subUnitId);
        if (worksheet?.getConfig().type !== SheetTypes.CHART) {
            return;
        }
        if (!this._sheetPermissionCheckController.permissionCheckWithoutRange({
            workbookTypes: [WorkbookEditablePermission],
            worksheetTypes: [WorksheetEditPermission],
        }, unitId, subUnitId)) {
            this._sheetPermissionCheckController.blockExecuteWithoutPermission(this._localeService.t<LocaleKey>('sheets-drawing-ui.permission.dialog.editErr'));
        }
    }

    private _initCommandPermissionCheck() {
        this.disposeWithMe(
            this._commandService.beforeCommandExecuted((command) => {
                let unitId: string | undefined;
                let subUnitId: string | undefined;

                if (command.id === InsertSheetDrawingCommand.id || command.id === RemoveSheetDrawingCommand.id || command.id === SetSheetDrawingCommand.id) {
                    const params = command.params as IInsertSheetDrawingCommandParams | IRemoveSheetDrawingCommandParams | ISetDrawingCommandParams;
                    const { drawings } = params;
                    unitId = drawings?.[0]?.unitId;
                    subUnitId = drawings?.[0]?.subUnitId;
                } else if (command.id === SetSheetDrawingPlacementCommand.id) {
                    const target = getPlacementCommandTarget(command.params);
                    unitId = target.unitId;
                    subUnitId = target.subUnitId;
                } else if (command.id === SetDrawingArrangeCommand.id) {
                    const params = command.params as ISetDrawingArrangeCommandParams;
                    unitId = params.unitId;
                    subUnitId = params.subUnitId;
                }

                if (!unitId || !subUnitId) {
                    return;
                }

                const permission = this._sheetPermissionCheckController.permissionCheckWithoutRange({
                    workbookTypes: [WorkbookEditablePermission],
                    worksheetTypes: [WorksheetEditPermission],
                }, unitId, subUnitId);
                if (!permission) {
                    this._sheetPermissionCheckController.blockExecuteWithoutPermission(this._localeService.t<LocaleKey>('sheets-drawing-ui.permission.dialog.editErr'));
                }
            })
        );
    }
}

function getPlacementCommandTarget(params: object | undefined): {
    unitId?: string;
    subUnitId?: string;
} {
    if (!params) {
        return {};
    }

    const unitId = 'unitId' in params && typeof params.unitId === 'string'
        ? params.unitId
        : undefined;
    const subUnitId = 'subUnitId' in params && typeof params.subUnitId === 'string'
        ? params.subUnitId
        : undefined;
    return { unitId, subUnitId };
}