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(permission): fix the disabled status listener for the context menu in the sheet bar (#6877)

wpxp12345667371e8ffb80
diff --git a/packages/sheets-ui/src/menu/menu-util.ts b/packages/sheets-ui/src/menu/menu-util.ts
index b1c76d6989..6c7b938b14 100644
--- a/packages/sheets-ui/src/menu/menu-util.ts
+++ b/packages/sheets-ui/src/menu/menu-util.ts
@@ -418,8 +418,6 @@ export function getCellMenuHidden$(accessor: IAccessor, type: 'row' | 'col') {
export function getWorkbookPermissionDisable$(accessor: IAccessor, workbookPermissionTypes: WorkbookPermissionPointConstructor[]) {
const univerInstanceService = accessor.get(IUniverInstanceService);
const workbook$ = univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
- const worksheetRuleModel = accessor.get(WorksheetProtectionRuleModel);
- const selectionRuleModel = accessor.get(RangeProtectionRuleModel);
const permissionService = accessor.get(IPermissionService);
const userManagerService = accessor.get(UserManagerService);
@@ -428,29 +426,42 @@ export function getWorkbookPermissionDisable$(accessor: IAccessor, workbookPermi
if (!workbook) {
return of(true);
}
+
return workbook.activeSheet$.pipe(
- switchMap((activeSheet) => {
- if (!activeSheet) {
+ switchMap((worksheet) => {
+ if (!worksheet) {
return of(true);
}
+
const unitId = workbook.getUnitId();
+ const subUnitId = worksheet.getSheetId();
+
+ const selectionProtectionRuleModel = accessor.get(RangeProtectionRuleModel);
+ const worksheetProtectionRuleModel = accessor.get(WorksheetProtectionRuleModel);
+
const workbookPermissionIds: string[] = [];
workbookPermissionTypes.forEach((F) => workbookPermissionIds.push(new F(unitId).id));
- const workbookPermission$ = permissionService.composePermission$(workbookPermissionIds).pipe(map((list) => list.every((item) => item.value === true)));
- return workbookPermission$.pipe(
- map((basePermission) => {
- if (!basePermission) {
- return true;
- }
+ const permission$ = permissionService.composePermission$(workbookPermissionIds).pipe(
+ map((list) => list.every((item) => item.value === true))
+ ) ?? of(false);
- const subUnitId = activeSheet.getSheetId();
- const worksheetRule = worksheetRuleModel.getRule(unitId, subUnitId);
- const rangeRuleList = selectionRuleModel.getSubunitRuleList(unitId, subUnitId);
- if (worksheetRule || rangeRuleList.length) {
- return true;
- }
- return false;
+ const ruleChange$ = merge(
+ selectionProtectionRuleModel.ruleChange$,
+ worksheetProtectionRuleModel.ruleChange$
+ ).pipe(
+ startWith(null)
+ );
+
+ return combineLatest([permission$, ruleChange$]).pipe(
+ map(([permission, _]) => {
+ if (!permission) return true;
+
+ const worksheetRule = worksheetProtectionRuleModel.getRule(unitId, subUnitId);
+ if (worksheetRule?.permissionId) return true;
+
+ const subUnitRuleList = selectionProtectionRuleModel.getSubunitRuleList(unitId, subUnitId)?.filter((item) => item?.permissionId);
+ return subUnitRuleList.length > 0;
})
);
})
diff --git a/packages/sheets-ui/src/menu/permission-menu-util.ts b/packages/sheets-ui/src/menu/permission-menu-util.ts
index b05f036772..b235e91ebf 100644
--- a/packages/sheets-ui/src/menu/permission-menu-util.ts
+++ b/packages/sheets-ui/src/menu/permission-menu-util.ts
@@ -297,22 +297,33 @@ export function getAddPermissionFromSheetBarDisable$(accessor: IAccessor) {
if (!worksheet) {
return of(true);
}
+
const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();
+
const selectionProtectionRuleModel = accessor.get(RangeProtectionRuleModel);
const worksheetProtectionRuleModel = accessor.get(WorksheetProtectionRuleModel);
- const permission$ = permissionService.composePermission$([new WorkbookCreateProtectPermission(unitId).id]).pipe(map((permissions) => permissions.every((permission) => permission.value))) ?? of(false);
+
+ const permission$ = permissionService.composePermission$([
+ new WorkbookCreateProtectPermission(unitId).id,
+ ]).pipe(
+ map((permissions) => permissions.every((permission) => permission.value))
+ ) ?? of(false);
+
const ruleChange$ = merge(
selectionProtectionRuleModel.ruleChange$,
worksheetProtectionRuleModel.ruleChange$
).pipe(
startWith(null)
);
+
return combineLatest([permission$, ruleChange$]).pipe(
map(([permission, _]) => {
if (!permission) return true;
+
const worksheetRule = worksheetProtectionRuleModel.getRule(unitId, subUnitId);
if (worksheetRule?.permissionId) return true;
+
const subUnitRuleList = selectionProtectionRuleModel.getSubunitRuleList(unitId, subUnitId)?.filter((item) => item?.permissionId);
return subUnitRuleList.length > 0;
})
@@ -340,14 +351,20 @@ export function getRemovePermissionFromSheetBarDisable$(accessor: IAccessor) {
if (!worksheet) {
return of(true);
}
+
const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();
+
const worksheetProtectionRuleModel = accessor.get(WorksheetProtectionRuleModel);
- return worksheetProtectionRuleModel.ruleChange$.pipe(startWith(null)).pipe(
- map(() => {
- const rule = worksheetProtectionRuleModel.getRule(unitId, subUnitId);
- if (rule) {
- return permissionService.getPermissionPoint(new WorksheetDeleteProtectionPermission(unitId, subUnitId).id)?.value === false;
+
+ const permission$ = permissionService.getPermissionPoint$(new WorksheetDeleteProtectionPermission(unitId, subUnitId).id)?.pipe(map((e) => !!e.value)) ?? of(false);
+ const ruleChange$ = worksheetProtectionRuleModel.ruleChange$.pipe(startWith(null));
+
+ return combineLatest([permission$, ruleChange$]).pipe(
+ map(([permission, _]) => {
+ const worksheetRule = worksheetProtectionRuleModel.getRule(unitId, subUnitId);
+ if (worksheetRule?.permissionId) {
+ return !permission;
}
return true;
})
@@ -375,24 +392,36 @@ export function getSetPermissionFromSheetBarDisable$(accessor: IAccessor) {
if (!worksheet) {
return of(true);
}
+
const unitId = workbook.getUnitId();
+ const subUnitId = worksheet.getSheetId();
+
const selectionProtectionRuleModel = accessor.get(RangeProtectionRuleModel);
const worksheetProtectionRuleModel = accessor.get(WorksheetProtectionRuleModel);
- const permission$ = permissionService.composePermission$([new WorkbookCreateProtectPermission(unitId).id, new WorkbookManageCollaboratorPermission(unitId).id]).pipe(map((permissions) => permissions.every((permission) => permission.value))) ?? of(false);
- const worksheetRuleChange$ = worksheetProtectionRuleModel.ruleChange$.pipe(startWith(null));
- const selectionRuleChange$ = selectionProtectionRuleModel.ruleChange$.pipe(startWith(null));
- return combineLatest([permission$, worksheetRuleChange$, selectionRuleChange$]).pipe(
- map(([permission, _, __]) => {
- if (!permission) {
- return true;
- }
- const subUnitId = worksheet.getSheetId();
+
+ const permission$ = permissionService.composePermission$([
+ new WorkbookCreateProtectPermission(unitId).id,
+ new WorkbookManageCollaboratorPermission(unitId).id,
+ ]).pipe(
+ map((permissions) => permissions.every((permission) => permission.value))
+ ) ?? of(false);
+
+ const ruleChange$ = merge(
+ selectionProtectionRuleModel.ruleChange$,
+ worksheetProtectionRuleModel.ruleChange$
+ ).pipe(
+ startWith(null)
+ );
+
+ return combineLatest([permission$, ruleChange$]).pipe(
+ map(([permission, _]) => {
+ if (!permission) return true;
+
const worksheetRule = worksheetProtectionRuleModel.getRule(unitId, subUnitId);
- const selectionRuleList = selectionProtectionRuleModel.getSubunitRuleList(unitId, subUnitId);
- if (worksheetRule || selectionRuleList.length) {
- return false;
- }
- return true;
+ if (worksheetRule?.permissionId) return false;
+
+ const subUnitRuleList = selectionProtectionRuleModel.getSubunitRuleList(unitId, subUnitId)?.filter((item) => item?.permissionId);
+ return subUnitRuleList.length === 0;
})
);
})