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.

feat(permission): add facade api (#3132)

ybzkyca21fb848912
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index c5a232b055..f933356941 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -188,7 +188,7 @@ export { DataValidationImeMode } from './types/enum/data-validation-ime-mode';
export { DataValidationOperator } from './types/enum/data-validation-operator';
export { DataValidationType } from './types/enum/data-validation-type';
export { DataValidationStatus } from './types/enum/data-validation-status';
-export type { IPermissionTypes, WorkbookPermissionPointConstructor } from './services/permission/type';
+export type { IPermissionTypes, WorkbookPermissionPointConstructor, WorkSheetPermissionPointConstructor, RangePermissionPointConstructor } from './services/permission/type';
export { PermissionService } from './services/permission/permission.service';
export { AuthzIoLocalService } from './services/authz-io/authz-io-local.service';
export { IAuthzIoService } from './services/authz-io/type';
diff --git a/packages/core/src/services/authz-io/authz-io-local.service.ts b/packages/core/src/services/authz-io/authz-io-local.service.ts
index d024c9f409..1663fccead 100644
--- a/packages/core/src/services/authz-io/authz-io-local.service.ts
+++ b/packages/core/src/services/authz-io/authz-io-local.service.ts
@@ -14,10 +14,10 @@
* limitations under the License.
*/
-import type { IActionInfo, IAllowedRequest, IBatchAllowedResponse, ICollaborator, ICreateRequest, ICreateRequest_SelectRangeObject, IListPermPointRequest, IPermissionPoint, IPutCollaboratorsRequest, IUnitRoleKV, IUpdatePermPointRequest, UnitAction } from '@univerjs/protocol';
-import { ObjectScope, UnitObject, UnitRole, UniverType } from '@univerjs/protocol';
+import type { IActionInfo, IAllowedRequest, IBatchAllowedResponse, ICollaborator, ICreateRequest, ICreateRequest_SelectRangeObject, IListPermPointRequest, IPermissionPoint, IPutCollaboratorsRequest, IUnitRoleKV, IUpdatePermPointRequest, UnitAction, UnitObject } from '@univerjs/protocol';
+import { ObjectScope, UnitRole, UniverType } from '@univerjs/protocol';
import { Inject } from '../../common/di';
-import { Tools } from '../../shared/tools';
+import { generateRandomId } from '../../shared/tools';
import { IResourceManagerService } from '../resource-manager/type';
import { UserManagerService } from '../user-manager/user-manager.service';
import { createDefaultUser, isDevRole } from '../user-manager/const';
@@ -83,21 +83,7 @@ export class AuthzIoLocalService implements IAuthzIoService {
}
async create(config: ICreateRequest): Promise<string> {
- const id = Tools.generateRandomId(8);
- switch (config.objectType) {
- case UnitObject.SelectRange: {
- const params = config.selectRangeObject!;
- this._permissionMap.set(id, { ...params, objectType: config.objectType });
- break;
- }
- case UnitObject.Worksheet: {
- const params = config.worksheetObject!;
-
- this._permissionMap.set(id, { ...params, objectType: config.objectType });
- }
- }
-
- return id;
+ return generateRandomId(8);
}
async allowed(_config: IAllowedRequest): Promise<IActionInfo[]> {
diff --git a/packages/facade/src/apis/__tests__/create-test-bed.ts b/packages/facade/src/apis/__tests__/create-test-bed.ts
index 6eaeecee40..acdb90c203 100644
--- a/packages/facade/src/apis/__tests__/create-test-bed.ts
+++ b/packages/facade/src/apis/__tests__/create-test-bed.ts
@@ -18,8 +18,6 @@
import type { Dependency, IWorkbookData, UnitModel, Workbook } from '@univerjs/core';
import {
- AuthzIoLocalService,
- IAuthzIoService,
ILogService,
Inject,
Injector,
@@ -164,7 +162,6 @@ export function createFacadeTestBed(workbookData?: IWorkbookData, dependencies?:
injector.add([WorkbookPermissionService]);
injector.add([WorksheetProtectionPointModel]);
injector.add([RangeProtectionRuleModel]);
- injector.add([IAuthzIoService, { useClass: AuthzIoLocalService }]);
injector.add([WorksheetProtectionRuleModel]);
const renderManagerService = injector.get(IRenderManagerService);
diff --git a/packages/facade/src/apis/facade.ts b/packages/facade/src/apis/facade.ts
index 8443ee424c..0ea1f4c230 100644
--- a/packages/facade/src/apis/facade.ts
+++ b/packages/facade/src/apis/facade.ts
@@ -35,7 +35,8 @@ import {
Quantity,
toDisposable,
UndoCommand,
- Univer, UniverInstanceType, WrapStrategy } from '@univerjs/core';
+ Univer, UniverInstanceType, WrapStrategy,
+} from '@univerjs/core';
import type { ISocket } from '@univerjs/network';
import { ISocketService, WebSocketService } from '@univerjs/network';
import type { IRegisterFunctionParams } from '@univerjs/sheets-formula';
@@ -57,6 +58,7 @@ import { FWorkbook } from './sheets/f-workbook';
import { FSheetHooks } from './sheets/f-sheet-hooks';
import { FHooks } from './f-hooks';
import { FDataValidationBuilder } from './sheets/f-data-validation-builder';
+import { FPermission } from './sheets/f-permission';
export class FUniver {
static BorderStyle = BorderStyleTypes;
@@ -468,4 +470,13 @@ export class FUniver {
const sheetRow = this._getSheetRenderComponent(unitId, SHEET_VIEW_KEY.ROW) as SpreadsheetRowHeader;
sheetRow.setCustomHeader(cfg);
}
+
+ /**
+ * Get the PermissionInstance.
+ *
+ * @returns {FPermission} - The PermissionInstance.
+ */
+ getPermission(): FPermission {
+ return this._injector.createInstance(FPermission);
+ }
}
diff --git a/packages/facade/src/apis/sheets/__tests__/f-permission.spec.ts b/packages/facade/src/apis/sheets/__tests__/f-permission.spec.ts
new file mode 100644
index 0000000000..fb0e7442e7
--- /dev/null
+++ b/packages/facade/src/apis/sheets/__tests__/f-permission.spec.ts
@@ -0,0 +1,115 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * 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 { Injector } from '@univerjs/core';
+import { ICommandService, IPermissionService } from '@univerjs/core';
+import { AddRangeProtectionMutation, AddWorksheetProtectionMutation, DeleteRangeProtectionMutation, DeleteWorksheetProtectionMutation, RangeProtectionPermissionEditPoint, RangeProtectionRuleModel, SetRangeProtectionMutation, WorkbookEditablePermission, WorksheetEditPermission } from '@univerjs/sheets';
+import { beforeEach, describe, expect, it } from 'vitest';
+
+import type { FUniver } from '../../facade';
+import { createFacadeTestBed } from '../../__tests__/create-test-bed';
+
+describe('Test FPermission', () => {
+ let get: Injector['get'];
+ let commandService: ICommandService;
+ let univerAPI: FUniver;
+ let permissionService: IPermissionService;
+ let rangeProtectionRuleModel: RangeProtectionRuleModel;
+
+ beforeEach(() => {
+ const testBed = createFacadeTestBed();
+ get = testBed.get;
+ univerAPI = testBed.univerAPI;
+
+ commandService = get(ICommandService);
+ commandService.registerCommand(AddWorksheetProtectionMutation);
+ commandService.registerCommand(DeleteWorksheetProtectionMutation);
+ commandService.registerCommand(AddRangeProtectionMutation);
+ commandService.registerCommand(SetRangeProtectionMutation);
+ commandService.registerCommand(DeleteRangeProtectionMutation);
+
+ permissionService = get(IPermissionService);
+ rangeProtectionRuleModel = get(RangeProtectionRuleModel);
+ });
+
+ it('set workbook edit point false', () => {
+ const permission = univerAPI.getPermission();
+ const unitId = univerAPI.getActiveWorkbook()?.getId();
+ if (unitId) {
+ permission.setWorkbookEditPermission(unitId, false);
+ let editValue = permissionService.getPermissionPoint(new WorkbookEditablePermission(unitId).id)?.value;
+ expect(editValue).toBe(false);
+ permission.setWorkbookEditPermission(unitId, true);
+ editValue = permissionService.getPermissionPoint(new WorkbookEditablePermission(unitId).id)?.value;
+ expect(editValue).toBe(true);
+ }
+ });
+
+ it('set worksheet edit point false', async () => {
+ const permission = univerAPI.getPermission();
+ const unitId = univerAPI.getActiveWorkbook()?.getId();
+ const subUnitId = univerAPI.getActiveWorkbook()?.getActiveSheet().getSheetId();
+ if (unitId && subUnitId) {
+ await permission.addWorksheetBasePermission(unitId, subUnitId);
+ await permission.setWorksheetPermissionPoint(unitId, subUnitId, WorksheetEditPermission, false);
+ let editValue = permissionService.getPermissionPoint(new WorksheetEditPermission(unitId, subUnitId).id)?.value;
+ expect(editValue).toBe(false);
+ await permission.setWorksheetPermissionPoint(unitId, subUnitId, WorksheetEditPermission, true);
+ editValue = permissionService.getPermissionPoint(new WorksheetEditPermission(unitId, subUnitId).id)?.value;
+ expect(editValue).toBe(true);
+ await permission.setWorksheetPermissionPoint(unitId, subUnitId, WorksheetEditPermission, false);
+ editValue = permissionService.getPermissionPoint(new WorksheetEditPermission(unitId, subUnitId).id)?.value;
+ expect(editValue).toBe(false);
+ permission.removeWorksheetPermission(unitId, subUnitId);
+ editValue = permissionService.getPermissionPoint(new WorksheetEditPermission(unitId, subUnitId).id)?.value;
+ expect(editValue).toBe(true);
+ }
+ });
+
+ it('set range protection point false', async () => {
+ const permission = univerAPI.getPermission();
+ const unitId = univerAPI.getActiveWorkbook()?.getId();
+ const subUnitId = univerAPI.getActiveWorkbook()?.getActiveSheet().getSheetId();
+ const ranges = [{ startRow: 0, endRow: 1, startColumn: 0, endColumn: 1 }];
+ const newRanges = [{ startRow: 0, endRow: 2, startColumn: 0, endColumn: 2 }];
+
+ if (unitId && subUnitId) {
+ const res = await permission.addRangeBaseProtection(unitId, subUnitId, ranges);
+ if (!res?.permissionId || !res?.ruleId) {
+ return;
+ }
+ permission.setRangeProtectionPermissionPoint(unitId, subUnitId, res.permissionId, RangeProtectionPermissionEditPoint, false);
+ let editValue = permissionService.getPermissionPoint(new RangeProtectionPermissionEditPoint(unitId, subUnitId, res.permissionId).id)?.value;
+ expect(editValue).toBe(false);
+ permission.setRangeProtectionPermissionPoint(unitId, subUnitId, res.permissionId, RangeProtectionPermissionEditPoint, true);
+ editValue = permissionService.getPermissionPoint(new RangeProtectionPermissionEditPoint(unitId, subUnitId, res.permissionId).id)?.value;
+ expect(editValue).toBe(true);
+ let catchErr = false;
+ try {
+ await permission.addWorksheetBasePermission(unitId, subUnitId);
+ } catch (e) {
+ catchErr = true;
+ }
+ expect(catchErr).toBe(true);
+ permission.setRangeProtectionRanges(unitId, subUnitId, res.ruleId, newRanges);
+ let rule = rangeProtectionRuleModel.getRule(unitId, subUnitId, res.ruleId);
+ expect(rule?.ranges).toStrictEqual(newRanges);
+ permission.removeRangeProtection(unitId, subUnitId, [res.ruleId]);
+ rule = rangeProtectionRuleModel.getRule(unitId, subUnitId, res.ruleId);
+ expect(rule).toBeUndefined();
+ }
+ });
+});
diff --git a/packages/facade/src/apis/sheets/f-permission.ts b/packages/facade/src/apis/sheets/f-permission.ts
new file mode 100644
index 0000000000..69a8172712
--- /dev/null
+++ b/packages/facade/src/apis/sheets/f-permission.ts
@@ -0,0 +1,289 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * 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 { IRange, RangePermissionPointConstructor, WorkbookPermissionPointConstructor, WorkSheetPermissionPointConstructor } from '@univerjs/core';
+import { generateRandomId, IAuthzIoService, ICommandService, Inject, Injector, IPermissionService, Rectangle } from '@univerjs/core';
+import { AddRangeProtectionMutation, AddWorksheetProtectionMutation, DeleteRangeProtectionMutation, DeleteWorksheetProtectionMutation, getAllWorksheetPermissionPoint, getAllWorksheetPermissionPointByPointPanel, RangeProtectionRuleModel, SetRangeProtectionMutation, WorkbookEditablePermission, WorksheetEditPermission, WorksheetProtectionPointModel, WorksheetProtectionRuleModel, WorksheetViewPermission } from '@univerjs/sheets';
+import { SheetPermissionInterceptorBaseController } from '@univerjs/sheets-ui';
+
+export class FPermission {
+ constructor(
+ @Inject(Injector) private readonly _injector: Injector,
+ @ICommandService private readonly _commandService: ICommandService,
+ @IPermissionService private readonly _permissionService: IPermissionService,
+ @Inject(WorksheetProtectionRuleModel) private readonly _worksheetProtectionRuleModel: WorksheetProtectionRuleModel,
+ @Inject(RangeProtectionRuleModel) private readonly _rangeProtectionRuleModel: RangeProtectionRuleModel,
+ @Inject(WorksheetProtectionPointModel) private readonly _worksheetProtectionPointRuleModel: WorksheetProtectionPointModel,
+ @Inject(IAuthzIoService) private readonly _authzIoService: IAuthzIoService
+ ) {
+ // empty
+ }
+
+ /**
+ * Configures a specific permission point for a workbook.
+ *
+ * This function sets or updates a permission point for a workbook identified by `unitId`.
+ * It creates a new permission point if it does not already exist, and updates the point with the provided value.
+ *
+ * @param {string} unitId - The unique identifier of the workbook for which the permission is being set.
+ * @param {WorkbookPermissionPointConstructor} FPointClass - The constructor function for creating a permission point instance. Other point constructors can See the [permission-point documentation](https://github.com/dream-num/univer/tree/dev/packages/sheets/src/services/permission/permission-point) for more details.
+ * @param {boolean} value - The boolean value to determine whether the permission point is enabled or disabled.
+ *
+ */
+ setWorkbookPermissionPoint(unitId: string, FPointClass: WorkbookPermissionPointConstructor, value: boolean): void {
+ const instance = new FPointClass(unitId);
+ const permissionPoint = this._permissionService.getPermissionPoint(instance.id);
+ if (!permissionPoint) {
+ this._permissionService.addPermissionPoint(instance);
+ }
+ this._permissionService.updatePermissionPoint(instance.id, value);
+ }
+
+ /**
+ * This function is used to set whether the workbook can be edited
+ *
+ * @param {string} unitId - The unique identifier of the workbook for which the permission is being set.
+ * @param {boolean} value - A value that controls whether the workbook can be edited
+ *
+ */
+ setWorkbookEditPermission(unitId: string, value: boolean): void {
+ this.setWorkbookPermissionPoint(unitId, WorkbookEditablePermission, value);
+ }
+
+ /**
+ * This function is used to add a base permission for a worksheet.
+ *
+ * @param {string} unitId - The unique identifier of the workbook for which the permission is being set.
+ * @param {string} subUnitId - The unique identifier of the worksheet for which the permission is being set.
+ */
+ async addWorksheetBasePermission(unitId: string, subUnitId: string): Promise<string | undefined> {
+ const hasRangeProtection = this._rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).length > 0;
+ if (hasRangeProtection) {
+ throw new Error('sheet protection cannot intersect with range protection');
+ }
+ const permissionId = await this._authzIoService.create({ objectType: 2 });
+ const res = this._commandService.syncExecuteCommand(AddWorksheetProtectionMutation.id, {
+ unitId,
+ subUnitId,
+ rule: {
+ permissionId,
+ unitType: 2,
+ unitId,
+ subUnitId,
+ },
+ });
+ if (res) {
+ return permissionId;
+ }
+ }
+
+ /**
+ * Delete the entire table protection set for the worksheet and reset the point permissions of the worksheet to true
+ *
+ * @param {string} unitId - The unique identifier of the workbook for which the permission is being set.
+ * @param {string} subUnitId - The unique identifier of the worksheet for which the permission is being set.
+ */
+ removeWorksheetPermission(unitId: string, subUnitId: string): void {
+ this._commandService.syncExecuteCommand(DeleteWorksheetProtectionMutation.id, {
+ unitId,
+ subUnitId,
+ });
+
+ [...getAllWorksheetPermissionPoint(), ...getAllWorksheetPermissionPointByPointPanel()].forEach((F) => {
+ const instance = new F(unitId, subUnitId);
+ this._permissionService.updatePermissionPoint(instance.id, true);
+ });
+ this._worksheetProtectionPointRuleModel.deleteRule(unitId, subUnitId);
+ }
+
+ /**
+ * Sets the worksheet permission point by updating or adding the permission point for the worksheet.
+ * If the worksheet doesn't have a base permission, it creates one to used render
+ *
+ * @param {string} unitId - The unique identifier of the workbook.
+ * @param {string} subUnitId - The unique identifier of the worksheet.
+ * @param {WorkSheetPermissionPointConstructor} FPointClass - The constructor for the permission point class.
+ * See the [permission-point documentation](https://github.com/dream-num/univer/tree/dev/packages/sheets/src/services/permission/permission-point) for more details.
+ * @param {boolean} value - The new permission value to be set for the worksheet.
+ */
+ async setWorksheetPermissionPoint(unitId: string, subUnitId: string, FPointClass: WorkSheetPermissionPointConstructor, value: boolean): Promise<string | undefined> {
+ const hasBasePermission = this._worksheetProtectionRuleModel.getRule(unitId, subUnitId);
+ let permissionId;
+ const isBasePoint = FPointClass === WorksheetEditPermission || FPointClass === WorksheetViewPermission;
+ if (isBasePoint) {
+ if (!hasBasePermission) {
+ const hasRangeProtection = this._rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).length > 0;
+ if (hasRangeProtection) {
+ throw new Error('sheet protection cannot intersect with range protection');
+ }
+ permissionId = await this.addWorksheetBasePermission(unitId, subUnitId);
+ } else {
+ permissionId = hasBasePermission.permissionId;
+ }
+ }
+
+ const instance = new FPointClass(unitId, subUnitId);
+ const permissionPoint = this._permissionService.getPermissionPoint(instance.id);
+ if (!permissionPoint) {
+ this._permissionService.addPermissionPoint(instance);
+ }
+ this._permissionService.updatePermissionPoint(instance.id, value);
+
+ return permissionId;
+ }
+
+ /**
+ * Adds a range protection to the worksheet.
+ *
+ * @param {string} unitId - The unique identifier of the workbook.
+ * @param {string} subUnitId - The unique identifier of the worksheet.
+ * @param {IRange[]} ranges - The ranges to be protected.
+ */
+ async addRangeBaseProtection(unitId: string, subUnitId: string, ranges: IRange[]): Promise<{
+ permissionId: string;
+ ruleId: string;
+ } | undefined> {
+ // The permission ID generation here only provides the most basic permission type. If need collaborators later, need to expand this
+ const permissionId = await this._authzIoService.create({ objectType: 3 });
+ const ruleId = `ruleId_${generateRandomId(6)}`;
+ const worksheetProtection = this._worksheetProtectionRuleModel.getRule(unitId, subUnitId);
+ if (worksheetProtection) {
+ throw new Error('sheet protection cannot intersect with range protection');
+ }
+ const subunitRuleList = this._rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId);
+ const overlap = subunitRuleList.some((rule) => {
+ return rule.ranges.some((range) => {
+ return ranges.some((newRange) => {
+ return Rectangle.intersects(newRange, range);
+ });
+ });
+ });
+ if (overlap) {
+ throw new Error('range protection cannot intersect');
+ }
+ const res = this._commandService.syncExecuteCommand(AddRangeProtectionMutation.id, {
+ unitId,
+ subUnitId,
+ rules: [{
+ permissionId,
+ unitType: 3,
+ unitId,
+ subUnitId,
+ ranges,
+ id: ruleId,
+ }],
+ });
+ if (res) {
+ return {
+ permissionId,
+ ruleId,
+ };
+ }
+ }
+
+ /**
+ * Removes the range protection from the worksheet.
+ *
+ * @param {string} unitId - The unique identifier of the workbook.
+ * @param {string} subUnitId - The unique identifier of the worksheet.
+ * @param {string[]} ruleIds - The rule IDs of the range protection to be removed.
+ */
+ removeRangeProtection(unitId: string, subUnitId: string, ruleIds: string[]): void {
+ const res = this._commandService.syncExecuteCommand(DeleteRangeProtectionMutation.id, {
+ unitId,
+ subUnitId,
+ ruleIds,
+ });
+ if (res) {
+ const ruleList = this._rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId);
+ if (ruleList.length === 0) {
+ // because this rule is attached to other protection, if other protection is deleted, this rule should be deleted.
+ this._worksheetProtectionPointRuleModel.deleteRule(unitId, subUnitId);
+ [...getAllWorksheetPermissionPointByPointPanel()].forEach((F) => {
+ const instance = new F(unitId, subUnitId);
+ this._permissionService.updatePermissionPoint(instance.id, instance.value);
+ });
+ }
+ }
+ }
+
+ /**
+ * Modify the permission points of a custom area
+ *
+ * @param {string} unitId - The unique identifier of the workbook.
+ * @param {string} subUnitId - The unique identifier of the worksheet within the workbook.
+ * @param {string} permissionId - The unique identifier of the permission that controls access to the range.
+ * @param {RangePermissionPointConstructor} FPointClass - The constructor for the range permission point class.
+ * See the [permission-point documentation](https://github.com/dream-num/univer/tree/dev/packages/sheets/src/services/permission/permission-point) for more details.
+ * @param {boolean} value - The new permission value to be set for the range (e.g., true for allowing access, false for restricting access).
+ */
+ setRangeProtectionPermissionPoint(unitId: string, subUnitId: string, permissionId: string, FPointClass: RangePermissionPointConstructor, value: boolean): void {
+ const instance = new FPointClass(unitId, subUnitId, permissionId);
+ const permissionPoint = this._permissionService.getPermissionPoint(instance.id);
+ if (!permissionPoint) {
+ this._permissionService.addPermissionPoint(instance);
+ }
+ this._permissionService.updatePermissionPoint(instance.id, value);
+ }
+
+ /**
+ * Sets the ranges for range protection in a worksheet.
+ *
+ * This method finds the rule by unitId, subUnitId, and ruleId, and updates the rule with the provided ranges.
+ * It checks for overlaps with existing ranges in the same subunit and shows an error message if any overlap is detected.
+ * If no overlap is found, it executes the command to update the range protection with the new ranges.
+ *
+ * @param {string} unitId - The unique identifier of the workbook.
+ * @param {string} subUnitId - The unique identifier of the worksheet within the workbook.
+ * @param {string} ruleId - The ruleId of the range protection rule that is being updated.
+ * @param {IRange[]} ranges - The array of new ranges to be set for the range protection rule.
+ */
+ setRangeProtectionRanges(unitId: string, subUnitId: string, ruleId: string, ranges: IRange[]): void {
+ const rule = this._rangeProtectionRuleModel.getRule(unitId, subUnitId, ruleId);
+ if (rule) {
+ const subunitRuleList = this._rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).filter((r) => r.id !== ruleId);
+ const overlap = subunitRuleList.some((rule) => {
+ return rule.ranges.some((range) => {
+ return ranges.some((newRange) => {
+ return Rectangle.intersects(newRange, range);
+ });
+ });
+ });
+ if (overlap) {
+ throw new Error('range protection cannot intersect');
+ }
+ this._commandService.syncExecuteCommand(SetRangeProtectionMutation.id, {
+ unitId,
+ subUnitId,
+ ruleId,
+ rule: {
+ ...rule,
+ ranges,
+ },
+ });
+ }
+ }
+
+ /**
+ * Set visibility of unauthorized pop-up window
+ *
+ * @param {boolean} visible
+ */
+ setPermissionDialogVisible(visible: boolean): void {
+ const sheetPermissionInterceptorBaseController = this._injector.get(SheetPermissionInterceptorBaseController);
+ sheetPermissionInterceptorBaseController.setShowPermissionDialog(visible);
+ }
+}
diff --git a/packages/sheets-ui/src/controllers/permission/sheet-permission-init.controller.ts b/packages/sheets-ui/src/controllers/permission/sheet-permission-init.controller.ts
index 4b97b82318..4c2bc12683 100644
--- a/packages/sheets-ui/src/controllers/permission/sheet-permission-init.controller.ts
+++ b/packages/sheets-ui/src/controllers/permission/sheet-permission-init.controller.ts
@@ -380,7 +380,7 @@ export class SheetPermissionInitController extends Disposable {
handler: (cell = {}, context, next) => {
const { unitId, subUnitId } = context;
const worksheetRule = this._worksheetProtectionRuleModel.getRule(unitId, subUnitId);
- if (worksheetRule?.permissionId && worksheetRule.name) {
+ if (worksheetRule?.permissionId) {
const selectionProtection = [{
[UnitAction.View]: this._permissionService.getPermissionPoint(new WorksheetViewPermission(unitId, subUnitId).id)?.value ?? false,
[UnitAction.Edit]: this._permissionService.getPermissionPoint(new WorksheetEditPermission(unitId, subUnitId).id)?.value ?? false,
diff --git a/packages/sheets/src/commands/mutations/add-range-protection.mutation.ts b/packages/sheets/src/commands/mutations/add-range-protection.mutation.ts
index 8f51ee9d76..25b01596c8 100644
--- a/packages/sheets/src/commands/mutations/add-range-protection.mutation.ts
+++ b/packages/sheets/src/commands/mutations/add-range-protection.mutation.ts
@@ -26,7 +26,7 @@ export interface IAddRangeProtectionMutationParams {
rules: IRangeProtectionRule[];
unitId: string;
subUnitId: string;
- name: string;
+ name?: string;
description?: string;
};
diff --git a/packages/sheets/src/services/permission/type.ts b/packages/sheets/src/services/permission/type.ts
index 129b7b86e4..dd59db7996 100644
--- a/packages/sheets/src/services/permission/type.ts
+++ b/packages/sheets/src/services/permission/type.ts
@@ -18,7 +18,7 @@ import type { UnitObject } from '@univerjs/protocol';
export interface IWorksheetProtectionRule {
permissionId: string;
- name: string;
+ name?: string;
description?: string;
unitType: UnitObject;
unitId: string;