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(definedName): add defined name permission controller (#6857)

wpxp12345644dbe3ba4d29
diff --git a/packages/sheets-ui/src/views/defined-name/DefinedName.tsx b/packages/sheets-ui/src/views/defined-name/DefinedName.tsx
index 46e216ca9b..5cee272e2f 100644
--- a/packages/sheets-ui/src/views/defined-name/DefinedName.tsx
+++ b/packages/sheets-ui/src/views/defined-name/DefinedName.tsx
@@ -22,7 +22,7 @@ import { debounce, generateRandomId, ICommandService, IUniverInstanceService, Th
import { borderRightClassName, clsx, Dropdown } from '@univerjs/design';
import { deserializeRangeWithSheet, IDefinedNamesService, IFunctionService, ISuperTableService, LexerTreeBuilder } from '@univerjs/engine-formula';
import { MoreDownIcon } from '@univerjs/icons';
-import { getPrimaryForRange, InsertDefinedNameCommand, SCOPE_WORKBOOK_VALUE_DEFINED_NAME, SetSelectionsOperation, SetWorksheetShowCommand, SheetsSelectionsService } from '@univerjs/sheets';
+import { getPrimaryForRange, InsertDefinedNameCommand, SCOPE_WORKBOOK_VALUE_DEFINED_NAME, SetSelectionsOperation, SetWorksheetShowCommand, SheetPermissionCheckController, SheetsSelectionsService, WorkbookEditablePermission } from '@univerjs/sheets';
import { ILayoutService, useDependency } from '@univerjs/ui';
import { useEffect, useRef, useState } from 'react';
import { ScrollToCellCommand } from '../../commands/commands/set-scroll.command';
@@ -41,6 +41,7 @@ export function DefinedName({ disable }: { disable: boolean }) {
const superTableService = useDependency(ISuperTableService);
const functionService = useDependency(IFunctionService);
const layoutService = useDependency(ILayoutService);
+ const sheetPermissionCheckController = useDependency(SheetPermissionCheckController);
const workbook = univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
const unitId = workbook?.getUnitId();
@@ -178,7 +179,15 @@ export function DefinedName({ disable }: { disable: boolean }) {
focusSelection(action.refString);
return true;
case DefinedNameBoxActionType.CreateDefinedName: {
- if (!formulaOrRefString) {
+ if (
+ !formulaOrRefString ||
+ !sheetPermissionCheckController.permissionCheckWithoutRange(
+ {
+ workbookTypes: [WorkbookEditablePermission],
+ },
+ unitId
+ )
+ ) {
resetValue();
return false;
}
diff --git a/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx b/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx
index 4e2b572c8c..2a4a647bfd 100644
--- a/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx
+++ b/packages/sheets-ui/src/views/defined-name/DefinedNameContainer.tsx
@@ -16,7 +16,7 @@
import type { Nullable, Workbook } from '@univerjs/core';
import type { IDefinedNamesServiceParam, ISetDefinedNameMutationParam } from '@univerjs/engine-formula';
-import { generateRandomId, ICommandService, IUniverInstanceService, LocaleService, UniverInstanceType } from '@univerjs/core';
+import { generateRandomId, ICommandService, IPermissionService, IUniverInstanceService, LocaleService, UniverInstanceType } from '@univerjs/core';
import { Button, clsx, Confirm, scrollbarClassName, Tooltip } from '@univerjs/design';
import { IDefinedNamesService, serializeRangeWithSheet } from '@univerjs/engine-formula';
import { DeleteIcon, IncreaseIcon, PenIcon } from '@univerjs/icons';
@@ -26,7 +26,10 @@ import {
SCOPE_WORKBOOK_VALUE_DEFINED_NAME,
SetDefinedNameCommand,
SetWorksheetShowCommand,
+ SheetPermissionCheckController,
SheetsSelectionsService,
+ WorkbookEditablePermission,
+ WorksheetEditPermission,
} from '@univerjs/sheets';
import { useDependency, useVirtualList } from '@univerjs/ui';
import { useEffect, useRef, useState } from 'react';
@@ -38,6 +41,8 @@ export const DefinedNameContainer = () => {
const localeService = useDependency(LocaleService);
const definedNamesService = useDependency(IDefinedNamesService);
const selectionManagerService = useDependency(SheetsSelectionsService);
+ const permissionService = useDependency(IPermissionService);
+ const sheetPermissionCheckController = useDependency(SheetPermissionCheckController);
const workbook = univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET);
const unitId = workbook?.getUnitId();
@@ -56,6 +61,7 @@ export const DefinedNameContainer = () => {
const [definedNames, setDefinedNames] = useState<IDefinedNamesServiceParam[]>([]);
const [editorKey, setEditorKey] = useState<Nullable<string>>(null);
const [deleteConformKey, setDeleteConformKey] = useState<Nullable<string>>();
+ const [permissionCheckVersion, setPermissionCheckVersion] = useState(0);
const listContainerRef = useRef<HTMLDivElement>(undefined!);
const [virtualDefinedNames, virtualActions] = useVirtualList(definedNames, {
@@ -81,10 +87,44 @@ export const DefinedNameContainer = () => {
};
}, []);
+ // permission point update may cause the permission check result to change, so we need to update the component when permission point update happens
+ useEffect(() => {
+ const permissionSubscription = permissionService.permissionPointUpdate$.subscribe(() => {
+ setPermissionCheckVersion((v) => v + 1);
+ });
+
+ return () => {
+ permissionSubscription.unsubscribe();
+ };
+ }, [permissionService]);
+
if (!workbook || !unitId) {
return;
}
+ // check defined name permission
+ const checkWorkbookPermission = sheetPermissionCheckController.permissionCheckWithoutRange(
+ {
+ workbookTypes: [WorkbookEditablePermission],
+ },
+ unitId
+ );
+ const checkDefinedNamePermission = (definedName: IDefinedNamesServiceParam): boolean => {
+ const { localSheetId } = definedName;
+
+ if (!localSheetId || localSheetId === SCOPE_WORKBOOK_VALUE_DEFINED_NAME) {
+ return checkWorkbookPermission;
+ }
+
+ return sheetPermissionCheckController.permissionCheckWithoutRange(
+ {
+ worksheetTypes: [WorksheetEditPermission],
+ },
+ unitId,
+ localSheetId
+ );
+ };
+
const insertConfirm = (param: IDefinedNamesServiceParam) => {
const { name, formulaOrRefString, comment, localSheetId, hidden } = param;
@@ -203,6 +243,7 @@ export const DefinedNameContainer = () => {
'univer-hidden': editState,
}
)}
+ disabled={!checkWorkbookPermission}
onClick={openInsertCloseKeyEditor}
>
<IncreaseIcon />
@@ -271,45 +312,46 @@ export const DefinedNameContainer = () => {
{definedName.formulaOrRefString}
</div>
</div>
- <div
- className={`
- univer-absolute univer-right-5 univer-top-1/2 univer-hidden
- -univer-translate-y-1/2 univer-cursor-pointer univer-items-center
- univer-justify-end univer-gap-7 univer-text-xs univer-text-primary-600
- group-hover:univer-flex
- dark:hover:!univer-bg-gray-600
- `}
- >
- <Tooltip title={localeService.t('definedName.updateButton')} placement="top">
- <a
- className={`
- univer-rounded univer-p-1
- hover:univer-bg-gray-100
- `}
- onClick={(e) => {
- e.stopPropagation();
- closeInsertOpenKeyEditor(definedName.id);
- }}
- >
- <PenIcon />
- </a>
- </Tooltip>
- <Tooltip title={localeService.t('definedName.deleteButton')} placement="top">
- <a
- className={`
- univer-rounded univer-p-1 univer-text-red-600
- hover:univer-bg-gray-100
- `}
- onClick={(e) => {
- e.stopPropagation();
- deleteDefinedName(definedName.id);
- }}
- >
- <DeleteIcon />
- </a>
- </Tooltip>
- </div>
-
+ {checkDefinedNamePermission(definedName) && (
+ <div
+ className={`
+ univer-absolute univer-right-5 univer-top-1/2 univer-hidden
+ -univer-translate-y-1/2 univer-cursor-pointer univer-items-center
+ univer-justify-end univer-gap-7 univer-text-xs univer-text-primary-600
+ group-hover:univer-flex
+ dark:hover:!univer-bg-gray-600
+ `}
+ >
+ <Tooltip title={localeService.t('definedName.updateButton')} placement="top">
+ <a
+ className={`
+ univer-rounded univer-p-1
+ hover:univer-bg-gray-100
+ `}
+ onClick={(e) => {
+ e.stopPropagation();
+ closeInsertOpenKeyEditor(definedName.id);
+ }}
+ >
+ <PenIcon />
+ </a>
+ </Tooltip>
+ <Tooltip title={localeService.t('definedName.deleteButton')} placement="top">
+ <a
+ className={`
+ univer-rounded univer-p-1 univer-text-red-600
+ hover:univer-bg-gray-100
+ `}
+ onClick={(e) => {
+ e.stopPropagation();
+ deleteDefinedName(definedName.id);
+ }}
+ >
+ <DeleteIcon />
+ </a>
+ </Tooltip>
+ </div>
+ )}
</div>
<Confirm
diff --git a/packages/sheets/src/controllers/permission/sheet-permission-check.controller.ts b/packages/sheets/src/controllers/permission/sheet-permission-check.controller.ts
index 44d140f8ac..69eae02a36 100644
--- a/packages/sheets/src/controllers/permission/sheet-permission-check.controller.ts
+++ b/packages/sheets/src/controllers/permission/sheet-permission-check.controller.ts
@@ -15,6 +15,7 @@
*/
import type { ICellData, ICellDataForSheetInterceptor, ICommandInfo, IObjectMatrixPrimitiveType, IPermissionTypes, IRange, Nullable, Workbook, WorkbookPermissionPointConstructor } from '@univerjs/core';
+import type { ISetDefinedNameMutationParam } from '@univerjs/engine-formula';
import type { IAutoFillCommandParams } from '../../commands/commands/auto-fill.command';
import type { IClearSelectionAllCommandParams } from '../../commands/commands/clear-selection-all.command';
import type { IClearSelectionContentCommandParams } from '../../commands/commands/clear-selection-content.command';
@@ -32,8 +33,10 @@ import type { ISetSpecificColsVisibleCommandParams } from '../../commands/comman
import type { ISetRangeValuesCommandParams } from '../../commands/commands/set-range-values.command';
import type { ISetSpecificRowsVisibleCommandParams } from '../../commands/commands/set-row-visible.command';
import type { ISetStyleCommandParams } from '../../commands/commands/set-style.command';
+import type { ISetColWidthCommandParams } from '../../commands/commands/set-worksheet-col-width.command';
import type { ISetWorksheetNameCommandParams } from '../../commands/commands/set-worksheet-name.command';
import type { ISetWorksheetOrderCommandParams } from '../../commands/commands/set-worksheet-order.command';
+import type { ISetRowHeightCommandParams, ISetWorksheetRowIsAutoHeightCommandParams } from '../../commands/commands/set-worksheet-row-height.command';
import type { ISetWorksheetShowCommandParams } from '../../commands/commands/set-worksheet-show.command';
import {
CustomCommandExecutionError,
@@ -60,14 +63,17 @@ import { ClearSelectionContentCommand } from '../../commands/commands/clear-sele
import { ClearSelectionFormatCommand } from '../../commands/commands/clear-selection-format.command';
import { DeleteRangeMoveLeftCommand } from '../../commands/commands/delete-range-move-left.command';
import { DeleteRangeMoveUpCommand } from '../../commands/commands/delete-range-move-up.command';
+import { InsertDefinedNameCommand } from '../../commands/commands/insert-defined-name.command';
import { InsertRangeMoveDownCommand } from '../../commands/commands/insert-range-move-down.command';
import { InsertRangeMoveRightCommand } from '../../commands/commands/insert-range-move-right.command';
import { InsertColByRangeCommand, InsertRowByRangeCommand } from '../../commands/commands/insert-row-col.command';
import { MoveRangeCommand } from '../../commands/commands/move-range.command';
import { MoveColsCommand, MoveRowsCommand } from '../../commands/commands/move-rows-cols.command';
+import { RemoveDefinedNameCommand } from '../../commands/commands/remove-defined-name.command';
import { RemoveColByRangeCommand, RemoveRowByRangeCommand } from '../../commands/commands/remove-row-col.command';
import { SetBorderCommand } from '../../commands/commands/set-border-command';
import { SetSelectedColsVisibleCommand, SetSpecificColsVisibleCommand } from '../../commands/commands/set-col-visible.command';
+import { SetDefinedNameCommand } from '../../commands/commands/set-defined-name.command';
import { SetRangeValuesCommand } from '../../commands/commands/set-range-values.command';
import { SetSelectedRowsVisibleCommand, SetSpecificRowsVisibleCommand } from '../../commands/commands/set-row-visible.command';
import { SetStyleCommand } from '../../commands/commands/set-style.command';
@@ -102,6 +108,7 @@ import {
} from '../../services/permission/permission-point';
import { WorksheetProtectionRuleModel } from '../../services/permission/worksheet-permission';
import { SheetsSelectionsService } from '../../services/selections';
+import { SCOPE_WORKBOOK_VALUE_DEFINED_NAME } from '../defined-name-data.controller';
/* eslint-disable complexity */
/* eslint-disable max-lines-per-function */
@@ -275,7 +282,6 @@ export class SheetPermissionCheckController extends Disposable {
// set column width
case DeltaColumnWidthCommand.id:
- case SetColWidthCommand.id:
permission = this.permissionCheckWithoutRange(
{
worksheetTypes: [WorksheetSetColumnStylePermission],
@@ -283,15 +289,38 @@ export class SheetPermissionCheckController extends Disposable {
);
errorMsg = this._localeService.t('permission.dialog.setRowColStyleErr');
break;
+ case SetColWidthCommand.id:
+ params = commandInfo.params as ISetColWidthCommandParams;
+
+ permission = this.permissionCheckWithoutRange(
+ {
+ worksheetTypes: [WorksheetSetColumnStylePermission],
+ },
+ params.unitId,
+ params.subUnitId
+ );
+ errorMsg = this._localeService.t('permission.dialog.setRowColStyleErr');
+ break;
// set row height
case DeltaRowHeightCommand.id:
+ permission = this.permissionCheckWithoutRange(
+ {
+ worksheetTypes: [WorksheetSetRowStylePermission],
+ }
+ );
+ errorMsg = this._localeService.t('permission.dialog.setRowColStyleErr');
+ break;
case SetRowHeightCommand.id:
case SetWorksheetRowIsAutoHeightCommand.id:
+ params = commandInfo.params as ISetRowHeightCommandParams | ISetWorksheetRowIsAutoHeightCommandParams;
+
permission = this.permissionCheckWithoutRange(
{
worksheetTypes: [WorksheetSetRowStylePermission],
- }
+ },
+ params.unitId,
+ params.subUnitId
);
errorMsg = this._localeService.t('permission.dialog.setRowColStyleErr');
break;
@@ -463,6 +492,32 @@ export class SheetPermissionCheckController extends Disposable {
errorMsg = this._localeService.t('permission.dialog.autoFillErr');
break;
+ // defined name
+ case InsertDefinedNameCommand.id:
+ case SetDefinedNameCommand.id:
+ case RemoveDefinedNameCommand.id:
+ params = commandInfo.params as ISetDefinedNameMutationParam;
+
+ if (!params.localSheetId || params.localSheetId === SCOPE_WORKBOOK_VALUE_DEFINED_NAME) {
+ permission = this.permissionCheckWithoutRange(
+ {
+ workbookTypes: [WorkbookEditablePermission],
+ },
+ params.unitId
+ );
+ } else {
+ permission = this.permissionCheckWithoutRange(
+ {
+ workbookTypes: [WorkbookEditablePermission],
+ worksheetTypes: [WorksheetEditPermission],
+ },
+ params.unitId,
+ params.localSheetId
+ );
+ }
+ errorMsg = this._localeService.t('permission.dialog.editErr');
+ break;
+
default:
break;
}