Skip to content

Commit

Permalink
fix: fix some permission bugs (#4478)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybzky authored Jan 24, 2025
1 parent a104643 commit 2683655
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/univer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function createUniverInjector(parentInjector?: Injector, override?: DependencyOv
[IContextService, { useClass: ContextService }],
[IResourceManagerService, { useClass: ResourceManagerService, lazy: true }],
[IResourceLoaderService, { useClass: ResourceLoaderService, lazy: true }],
[IAuthzIoService, { useClass: AuthzIoLocalService, lazy: true }],
[IAuthzIoService, { useClass: AuthzIoLocalService }],
[IMentionIOService, { useClass: MentionIOLocalService, lazy: true }],
], override);

Expand Down
78 changes: 47 additions & 31 deletions packages/sheets-ui/src/views/formula-bar/FormulaBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/

import type { Workbook } from '@univerjs/core';
import { DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, FOCUSING_FX_BAR_EDITOR, IContextService, IPermissionService, IUniverInstanceService, Rectangle, UniverInstanceType, useDependency, useObservable } from '@univerjs/core';
import { DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, FOCUSING_FX_BAR_EDITOR, IContextService, IPermissionService, IUniverInstanceService, UniverInstanceType, useDependency, useObservable } from '@univerjs/core';
import { DeviceInputEventType } from '@univerjs/engine-render';
import { CheckMarkSingle, CloseSingle, DropdownSingle, FxSingle } from '@univerjs/icons';
import { RangeProtectionPermissionEditPoint, RangeProtectionRuleModel, SheetsSelectionsService, WorkbookEditablePermission, WorksheetEditPermission, WorksheetProtectionRuleModel, WorksheetSetCellValuePermission } from '@univerjs/sheets';
import { RangeProtectionCache, RangeProtectionRuleModel, SheetsSelectionsService, UnitAction, WorksheetEditPermission, WorksheetProtectionRuleModel, WorksheetViewPermission } from '@univerjs/sheets';
import { ComponentContainer, ComponentManager, KeyCode, useComponentsOfPart } from '@univerjs/ui';
import clsx from 'clsx';
import React, { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
import { EMPTY, merge, switchMap } from 'rxjs';
import { EMPTY, merge, of, switchMap } from 'rxjs';
import { EMBEDDING_FORMULA_EDITOR_COMPONENT_KEY } from '../../common/keys';
import { useActiveWorkbook } from '../../components/hook';
import { SheetsUIPart } from '../../consts/ui-name';
Expand All @@ -48,7 +48,12 @@ export function FormulaBar() {
const univerInstanceService = useDependency(IUniverInstanceService);
const selectionManager = useDependency(SheetsSelectionsService);
const permissionService = useDependency(IPermissionService);
const [disable, setDisable] = useState<boolean>(false);
const rangeProtectionCache = useDependency(RangeProtectionCache);

const [disableInfo, setDisableInfo] = useState<{ editDisable: boolean; viewDisable: boolean }>({
editDisable: false,
viewDisable: false,
});
const [imageDisable, setImageDisable] = useState<boolean>(false);
const currentWorkbook = useActiveWorkbook();
const componentManager = useDependency(ComponentManager);
Expand All @@ -63,14 +68,6 @@ export function FormulaBar() {
const isFocusFxBar = contextService.getContextValue(FOCUSING_FX_BAR_EDITOR);
const ref = useRef<HTMLDivElement>(null);

function getPermissionIds(unitId: string, subUnitId: string): string[] {
return [
new WorkbookEditablePermission(unitId).id,
new WorksheetSetCellValuePermission(unitId, subUnitId).id,
new WorksheetEditPermission(unitId, subUnitId).id,
];
}

useLayoutEffect(() => {
const subscription = workbook.activeSheet$.pipe(
switchMap((worksheet) => {
Expand All @@ -88,25 +85,42 @@ export function FormulaBar() {
const subUnitId = worksheet.getSheetId();
const range = selectionManager.getCurrentLastSelection()?.range;
if (!range) return EMPTY;
const primary = selectionManager.getCurrentLastSelection()?.primary;
if (!primary) {
return of(null);
}

const permissionIds = getPermissionIds(unitId, subUnitId);

const selectionRanges = selectionManager.getCurrentSelections()?.map((selection) => selection.range);
const permissionList = rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).filter((rule) => {
return rule.ranges.some((r) => selectionRanges?.some((selectionRange) => Rectangle.intersects(r, selectionRange)));
return of({
unitId,
subUnitId,
primary,
});

permissionList.forEach((p) => {
permissionIds.push(new RangeProtectionPermissionEditPoint(unitId, subUnitId, p.permissionId).id);
});

return permissionService.composePermission$(permissionIds);
})
);
})
).subscribe((permissions) => {
if (permissions) {
setDisable(!permissions.every((p) => p.value));
).subscribe((cellInfo) => {
if (cellInfo) {
const { unitId, subUnitId, primary } = cellInfo;
if (worksheetProtectionRuleModel.getRule(unitId, subUnitId)) {
const editDisable = !(permissionService.getPermissionPoint(new WorksheetEditPermission(unitId, subUnitId).id)?.value ?? true);
const viewDisable = !(permissionService.getPermissionPoint(new WorksheetViewPermission(unitId, subUnitId).id)?.value ?? true);
setDisableInfo({
viewDisable,
editDisable,
});
return;
}
const { actualRow, actualColumn } = primary;
const cellInfoWithPermission = rangeProtectionCache.getCellInfo(unitId, subUnitId, actualRow, actualColumn);
setDisableInfo({
editDisable: !(cellInfoWithPermission?.[UnitAction.Edit] ?? true),
viewDisable: !(cellInfoWithPermission?.[UnitAction.View] ?? true),
});
} else {
setDisableInfo({
viewDisable: false,
editDisable: false,
});
}
});

Expand Down Expand Up @@ -188,17 +202,19 @@ export function FormulaBar() {
formulaEditorManagerService.handleFxBtnClick(true);
}

const disabled = disable || imageDisable;
// TODO Is there a need to disable an editor here?
const { viewDisable, editDisable } = disableInfo;
const disabled = editDisable || imageDisable;
return (
<div
className={styles.formulaBox}
style={{
height: ArrowDirection.Down === arrowDirection ? '28px' : '82px',
pointerEvents: disable ? 'none' : 'auto',
pointerEvents: editDisable ? 'none' : 'auto',
}}
>
<div className={styles.nameRanges}>
<DefinedName disable={disable} />
<DefinedName disable={editDisable} />
</div>

<div className={styles.formulaBar}>
Expand Down Expand Up @@ -231,7 +247,7 @@ export function FormulaBar() {
disableSelectionOnClick
editorId={DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY}
initValue=""
onChange={() => {}}
onChange={() => { }}
isFocus={isFocusFxBar}
className={styles.formulaContent}
unitId={editState?.unitId}
Expand All @@ -253,7 +269,7 @@ export function FormulaBar() {
/>
)}
</div>
<div className={clsx(styles.arrowContainer, { [styles.arrowContainerDisable]: disable })} onClick={handleArrowClick}>
<div className={clsx(styles.arrowContainer, { [styles.arrowContainerDisable]: editDisable })} onClick={handleArrowClick}>
{arrowDirection === ArrowDirection.Down
? (
<DropdownSingle />
Expand Down
3 changes: 2 additions & 1 deletion packages/sheets/src/facade/f-permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import type { RangePermissionPointConstructor, WorkbookPermissionPointConstructor, WorkSheetPermissionPointConstructor } from '@univerjs/core';
import type { ISetWorksheetPermissionPointsMutationParams } from '@univerjs/sheets';
import type { Observable } from 'rxjs';
import type { FRange } from './f-range';
import { FBase, generateRandomId, IAuthzIoService, ICommandService, Inject, Injector, IPermissionService, Rectangle } from '@univerjs/core';
Expand Down Expand Up @@ -220,7 +221,7 @@ export class FPermission extends FBase {
scope: undefined,
},
});
this._commandService.syncExecuteCommand(SetWorksheetPermissionPointsMutation.id, { unitId, subUnitId, permissionId });
this._commandService.syncExecuteCommand<ISetWorksheetPermissionPointsMutationParams>(SetWorksheetPermissionPointsMutation.id, { unitId, subUnitId, rule: { unitId, subUnitId, permissionId } });
} else {
permissionId = rule.permissionId;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sheets/src/sheets-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export class UniverSheetsPlugin extends Plugin {
[SheetInterceptorService],
[RangeProtectionService],
[IExclusiveRangeService],
[SheetPermissionInitController],
]);
}

Expand All @@ -151,7 +152,6 @@ export class UniverSheetsPlugin extends Plugin {
override onRendered(): void {
touchDependencies(this._injector, [
[INumfmtService],
[SheetPermissionInitController],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
"name": "SHEET_RANGE_PROTECTION_PLUGIN",
"data": ""
},
{
"data": "{}",
"name": "SHEET_AuthzIoMockService_PLUGIN"
},
{
"name": "SHEET_WORKSHEET_PROTECTION_PLUGIN",
"data": "{}"
Expand Down Expand Up @@ -121,4 +125,4 @@
}
],
"rev": 4
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
"name": "SHEET_RANGE_PROTECTION_PLUGIN",
"data": ""
},
{
"data": "{}",
"name": "SHEET_AuthzIoMockService_PLUGIN"
},
{
"name": "SHEET_WORKSHEET_PROTECTION_PLUGIN",
"data": "{}"
Expand Down

0 comments on commit 2683655

Please sign in to comment.