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.

fix(sheet): fix the permission mutation works before init (#6066)

VicKune0b74f09940f
diff --git a/packages/sheets/src/commands/mutations/add-worksheet-protection.mutation.ts b/packages/sheets/src/commands/mutations/add-worksheet-protection.mutation.ts
index 38550cf656..8cd46696cc 100644
--- a/packages/sheets/src/commands/mutations/add-worksheet-protection.mutation.ts
+++ b/packages/sheets/src/commands/mutations/add-worksheet-protection.mutation.ts
@@ -15,9 +15,10 @@
*/
import type { IMutation } from '@univerjs/core';
+import type { IWorksheetProtectionRule } from '../../services/permission/type';
import { CommandType } from '@univerjs/core';
+import { SheetPermissionInitController } from '../../controllers/permission/sheet-permission-init.controller';
import { WorksheetProtectionRuleModel } from '../../services/permission/worksheet-permission/worksheet-permission-rule.model';
-import type { IWorksheetProtectionRule } from '../../services/permission/type';
export interface IAddWorksheetProtectionParams {
unitId: string;
@@ -29,8 +30,16 @@ export const AddWorksheetProtectionMutation: IMutation<IAddWorksheetProtectionPa
id: 'sheet.mutation.add-worksheet-protection',
type: CommandType.MUTATION,
handler: (accessor, params) => {
- const { unitId, rule } = params;
+ const sheetPermissionInitController = accessor.get(SheetPermissionInitController);
const worksheetProtectionRuleModel = accessor.get(WorksheetProtectionRuleModel);
+ if (!sheetPermissionInitController.getIsPermissionInitFinish()) {
+ sheetPermissionInitController.addCmdToBufferList({
+ id: AddWorksheetProtectionMutation.id,
+ params,
+ });
+ return true;
+ }
+ const { unitId, rule } = params;
worksheetProtectionRuleModel.addRule(unitId, rule);
return true;
},
diff --git a/packages/sheets/src/commands/mutations/delete-worksheet-protection.mutation.ts b/packages/sheets/src/commands/mutations/delete-worksheet-protection.mutation.ts
index 66bfedfb7a..8a0ce8594e 100644
--- a/packages/sheets/src/commands/mutations/delete-worksheet-protection.mutation.ts
+++ b/packages/sheets/src/commands/mutations/delete-worksheet-protection.mutation.ts
@@ -16,6 +16,7 @@
import type { IMutation } from '@univerjs/core';
import { CommandType } from '@univerjs/core';
+import { SheetPermissionInitController } from '../../controllers/permission/sheet-permission-init.controller';
import { WorksheetProtectionRuleModel } from '../../services/permission/worksheet-permission/worksheet-permission-rule.model';
export interface IDeleteWorksheetProtectionParams {
@@ -27,8 +28,17 @@ export const DeleteWorksheetProtectionMutation: IMutation<IDeleteWorksheetProtec
id: 'sheet.mutation.delete-worksheet-protection',
type: CommandType.MUTATION,
handler: (accessor, params) => {
- const { unitId, subUnitId } = params;
const worksheetProtectionRuleModel = accessor.get(WorksheetProtectionRuleModel);
+ const sheetPermissionInitController = accessor.get(SheetPermissionInitController);
+ if (!sheetPermissionInitController.getIsPermissionInitFinish()) {
+ sheetPermissionInitController.addCmdToBufferList({
+ id: DeleteWorksheetProtectionMutation.id,
+ params,
+ });
+ return true;
+ }
+ const { unitId, subUnitId } = params;
+
worksheetProtectionRuleModel.deleteRule(unitId, subUnitId);
return true;
},
diff --git a/packages/sheets/src/controllers/permission/sheet-permission-init.controller.ts b/packages/sheets/src/controllers/permission/sheet-permission-init.controller.ts
index c41f2d8d84..3632e99158 100644
--- a/packages/sheets/src/controllers/permission/sheet-permission-init.controller.ts
+++ b/packages/sheets/src/controllers/permission/sheet-permission-init.controller.ts
@@ -18,6 +18,7 @@ import type { Workbook } from '@univerjs/core';
import type { UnitAction } from '@univerjs/protocol';
import type { IAddRangeProtectionMutationParams } from '../../commands/mutations/add-range-protection.mutation';
import type { IAddWorksheetProtectionParams } from '../../commands/mutations/add-worksheet-protection.mutation';
+import type { IDeleteWorksheetProtectionParams } from '../../commands/mutations/delete-worksheet-protection.mutation';
import type { ISetWorksheetPermissionPointsMutationParams } from '../../commands/mutations/set-worksheet-permission-points.mutation';
import { Disposable, IAuthzIoService, ICommandService, Inject, IPermissionService, IUndoRedoService, IUniverInstanceService, UniverInstanceType, UserManagerService } from '@univerjs/core';
import { UnitObject } from '@univerjs/protocol';
@@ -32,7 +33,17 @@ import { defaultWorkbookPermissionPoints, getAllWorkbookPermissionPoint } from '
import { WorkbookPermissionService } from '../../services/permission/workbook-permission/workbook-permission.service';
import { WorksheetProtectionPointModel, WorksheetProtectionRuleModel } from '../../services/permission/worksheet-permission';
+interface ISheetPermissionCmdBufferListItemType {
+ id: string;
+ params: IDeleteWorksheetProtectionParams;
+}
+
export class SheetPermissionInitController extends Disposable {
+ private _cmdBufferList: ISheetPermissionCmdBufferListItemType[] = [];
+ private _isRangePermissionInitFinish: boolean = false;
+ private _isWorksheetPermissionInitFinish: boolean = false;
+ private _isWorkbookPermissionInitFinish: boolean = false;
+
constructor(
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@IPermissionService private _permissionService: IPermissionService,
@@ -63,6 +74,24 @@ export class SheetPermissionInitController extends Disposable {
this._initRangePermissionFromSnapshot();
}
+ public getIsPermissionInitFinish() {
+ return this._isWorksheetPermissionInitFinish && this._isRangePermissionInitFinish && this._isWorkbookPermissionInitFinish;
+ }
+
+ public addCmdToBufferList(item: ISheetPermissionCmdBufferListItemType) {
+ this._cmdBufferList.push(item);
+ }
+
+ private _processCmdBufferList() {
+ if (!this.getIsPermissionInitFinish() || this._cmdBufferList.length === 0) {
+ return;
+ }
+
+ for (const item of this._cmdBufferList) {
+ this._commandService.executeCommand(item.id, item.params, { onlyLocal: true });
+ }
+ }
+
private async _initRangePermissionFromSnapshot() {
const initRangePermissionFunc = async (workbook: Workbook) => {
const allAllowedParams: {
@@ -89,6 +118,10 @@ export class SheetPermissionInitController extends Disposable {
if (!allAllowedParams.length) {
this._rangeProtectionRuleModel.changeRuleInitState(true);
+ this._isRangePermissionInitFinish = true;
+ if (this.getIsPermissionInitFinish()) {
+ this._processCmdBufferList();
+ }
return;
}
@@ -107,6 +140,10 @@ export class SheetPermissionInitController extends Disposable {
}
});
this._rangeProtectionRuleModel.changeRuleInitState(true);
+ this._isRangePermissionInitFinish = true;
+ if (this.getIsPermissionInitFinish()) {
+ this._processCmdBufferList();
+ }
});
};
@@ -175,6 +212,10 @@ export class SheetPermissionInitController extends Disposable {
this._permissionService.updatePermissionPoint(instance.id, action.allowed);
}
});
+ this._isWorkbookPermissionInitFinish = true;
+ if (this.getIsPermissionInitFinish()) {
+ this._processCmdBufferList();
+ }
});
}
@@ -292,11 +333,18 @@ export class SheetPermissionInitController extends Disposable {
}
});
this._worksheetProtectionRuleModel.changeRuleInitState(true);
+ this._isWorksheetPermissionInitFinish = true;
+ if (this.getIsPermissionInitFinish()) {
+ this._processCmdBufferList();
+ }
});
};
await Promise.all(this._univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET).map((workbook) => initSheetPermissionFunc(workbook)));
this._worksheetProtectionRuleModel.changeRuleInitState(true);
+ if (this.getIsPermissionInitFinish()) {
+ this._processCmdBufferList();
+ }
}
private _initUserChange() {
diff --git a/packages/sheets/src/facade/__tests__/create-test-bed.ts b/packages/sheets/src/facade/__tests__/create-test-bed.ts
index 18a0523e54..8efa658c3b 100644
--- a/packages/sheets/src/facade/__tests__/create-test-bed.ts
+++ b/packages/sheets/src/facade/__tests__/create-test-bed.ts
@@ -38,6 +38,7 @@ import {
RangeProtectionRuleModel,
RefRangeService,
SheetInterceptorService,
+ SheetPermissionInitController,
SheetSkeletonService,
SheetsSelectionsService,
WorkbookPermissionService,
@@ -145,6 +146,7 @@ export function createFacadeTestBed(workbookData?: IWorkbookData, dependencies?:
injector.add([WorksheetProtectionPointModel]);
injector.add([RangeProtectionRuleModel]);
injector.add([WorksheetProtectionRuleModel]);
+ injector.add([SheetPermissionInitController]);
injector.add([IDefinedNamesService, { useClass: DefinedNamesService }]);
dependencies?.forEach((d) => injector.add(d));