Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: permission check use compose points #3540

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/sheets-hyper-link-ui/src/controllers/popup.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { CustomRangeType, Disposable, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DOCS_ZEN_EDITOR_UNIT_ID_KEY, ICommandService, Inject, IPermissionService, IUniverInstanceService, LifecycleStages, OnLifecycle, Rectangle } from '@univerjs/core';
import { CustomRangeType, Disposable, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DOCS_ZEN_EDITOR_UNIT_ID_KEY, ICommandService, Inject, IPermissionService, IUniverInstanceService, LifecycleStages, OnLifecycle, Rectangle, UniverInstanceType } from '@univerjs/core';
import { DocSelectionManagerService } from '@univerjs/docs';
import { DocEventManagerService } from '@univerjs/docs-ui';
import { IRenderManagerService } from '@univerjs/engine-render';
Expand All @@ -35,7 +35,7 @@ import {
import { HoverManagerService, HoverRenderController, IEditorBridgeService, SheetPermissionInterceptorBaseController, SheetSkeletonManagerService } from '@univerjs/sheets-ui';
import { IZenZoneService } from '@univerjs/ui';
import { debounceTime, map, mergeMap, Observable } from 'rxjs';
import type { Nullable } from '@univerjs/core';
import type { Nullable, Workbook } from '@univerjs/core';
import type { ISheetLocationBase } from '@univerjs/sheets';
import type { Subscription } from 'rxjs';
import { SheetsHyperLinkPopupService } from '../services/popup.service';
Expand Down Expand Up @@ -66,6 +66,16 @@ export class SheetsHyperLinkPopupController extends Disposable {

private _getLinkPermission(location: ISheetLocationBase) {
const { unitId, subUnitId, row: currentRow, col: currentCol } = location;
const workbook = this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET);
const worksheet = workbook?.getSheetBySheetId(subUnitId);
if (!worksheet) {
return {
viewPermission: false,
editPermission: false,
copyPermission: false,
};
}

const viewPermission = this._sheetPermissionInterceptorBaseController.permissionCheckWithRanges({
workbookTypes: [WorkbookViewPermission],
worksheetTypes: [WorksheetViewPermission],
Expand Down
12 changes: 6 additions & 6 deletions packages/sheets-ui/src/controllers/menu/menu-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { FOCUSING_COMMON_DRAWINGS, IContextService, IPermissionService, IUniverInstanceService, Rectangle, Tools, UniverInstanceType, UserManagerService } from '@univerjs/core';
import { IExclusiveRangeService, RangeProtectionPermissionEditPoint, RangeProtectionRuleModel, SheetsSelectionsService, WorkbookEditablePermission, WorkbookManageCollaboratorPermission, WorksheetEditPermission, WorksheetProtectionRuleModel } from '@univerjs/sheets';
import { combineLatest, map, merge, of, startWith, switchMap } from 'rxjs';
import { combineLatest, debounceTime, map, merge, of, startWith, switchMap } from 'rxjs';
import type { IAccessor, IPermissionTypes, IRange, Nullable, Workbook, WorkbookPermissionPointConstructor, Worksheet } from '@univerjs/core';
import type { Observable } from 'rxjs';

Expand Down Expand Up @@ -181,7 +181,7 @@ export function getBaseRangeMenuHidden$(accessor: IAccessor) {
const rangeProtectionRuleModel = accessor.get(RangeProtectionRuleModel);
const permissionService = accessor.get(IPermissionService);

return merge(selectionManagerService.selectionMoveEnd$, permissionService.permissionPointUpdate$).pipe(
return merge(selectionManagerService.selectionMoveEnd$, permissionService.permissionPointUpdate$.pipe(debounceTime(100))).pipe(
map(() => {
const range = selectionManagerService.getCurrentLastSelection()?.range;
if (!range) return true;
Expand Down Expand Up @@ -215,7 +215,7 @@ export function getInsertAfterMenuHidden$(accessor: IAccessor, type: 'row' | 'co
const rangeProtectionRuleModel = accessor.get(RangeProtectionRuleModel);
const permissionService = accessor.get(IPermissionService);

return merge(selectionManagerService.selectionMoveEnd$, permissionService.permissionPointUpdate$).pipe(
return merge(selectionManagerService.selectionMoveEnd$, permissionService.permissionPointUpdate$.pipe(debounceTime(100))).pipe(
map(() => {
const range = selectionManagerService.getCurrentLastSelection()?.range;
if (!range) return true;
Expand Down Expand Up @@ -257,7 +257,7 @@ export function getInsertBeforeMenuHidden$(accessor: IAccessor, type: 'row' | 'c
const rangeProtectionRuleModel = accessor.get(RangeProtectionRuleModel);
const permissionService = accessor.get(IPermissionService);

return merge(selectionManagerService.selectionMoveEnd$, permissionService.permissionPointUpdate$).pipe(
return merge(selectionManagerService.selectionMoveEnd$, permissionService.permissionPointUpdate$.pipe(debounceTime(100))).pipe(
map(() => {
const range = selectionManagerService.getCurrentLastSelection()?.range;
if (!range) return true;
Expand Down Expand Up @@ -300,7 +300,7 @@ export function getDeleteMenuHidden$(accessor: IAccessor, type: 'row' | 'col') {

const permissionService = accessor.get(IPermissionService);

return merge(selectionManagerService.selectionMoveEnd$, permissionService.permissionPointUpdate$).pipe(
return merge(selectionManagerService.selectionMoveEnd$, permissionService.permissionPointUpdate$.pipe(debounceTime(100))).pipe(
map(() => {
const range = selectionManagerService.getCurrentLastSelection()?.range;
if (!range) return true;
Expand Down Expand Up @@ -345,7 +345,7 @@ export function getCellMenuHidden$(accessor: IAccessor, type: 'row' | 'col') {

const permissionService = accessor.get(IPermissionService);

return merge(selectionManagerService.selectionMoveEnd$, permissionService.permissionPointUpdate$).pipe(
return merge(selectionManagerService.selectionMoveEnd$, permissionService.permissionPointUpdate$.pipe(debounceTime(100))).pipe(
map(() => {
const range = selectionManagerService.getCurrentLastSelection()?.range;
if (!range) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,50 +462,28 @@ export class SheetPermissionInterceptorBaseController extends Disposable {
}

const { workbookTypes, worksheetTypes, rangeTypes } = permissionTypes;
const permissionIds = [];
if (workbookTypes) {
const workbookDisable = workbookTypes.some((F) => {
const instance = new F(unitId);
const permission = this._permissionService.getPermissionPoint(instance.id)?.value ?? false;
return permission === false;
});
if (workbookDisable === true) {
return false;
}
permissionIds.push(...workbookTypes.map((F) => new F(unitId).id));
}
if (worksheetTypes) {
const worksheetDisable = worksheetTypes.some((F) => {
const instance = new F(unitId, subUnitId);
const permission = this._permissionService.getPermissionPoint(instance.id)?.value ?? false;
return permission === false;
});
if (worksheetDisable === true) {
return false;
}
permissionIds.push(...worksheetTypes.map((F) => new F(unitId, subUnitId).id));
}
if (rangeTypes) {
const hasPermission = ranges?.every((range) => {
return rangeTypes.every((F) => {
for (let row = range.startRow; row <= range.endRow; row++) {
for (let col = range.startColumn; col <= range.endColumn; col++) {
const cellInfo = (worksheet.getCell(row, col) as (ICellDataForSheetInterceptor & { selectionProtection: ICellPermission[] }))?.selectionProtection?.[0];
if (!cellInfo?.ruleId) {
continue;
}
const permissionId = this._rangeProtectionRuleModel.getRule(unitId, subUnitId, cellInfo.ruleId)?.permissionId;
if (!permissionId) {
continue;
}
const instance = new F(unitId, subUnitId, permissionId);
const permission = this._permissionService.getPermissionPoint(instance.id)?.value ?? false;
if (permission === false) {
return false;
}
}
}
return true;
this._rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).forEach((rule) => {
const overlap = ranges.some((range) => {
return rule.ranges.some((r) => {
return Rectangle.intersects(r, range);
});
});
if (overlap) {
permissionIds.push(...rangeTypes.map((F) => new F(unitId, subUnitId, rule.permissionId).id));
}
});
return hasPermission;
}

if (permissionIds.length) {
return this._permissionService.composePermission(permissionIds).every((permission) => permission.value);
}

return true;
Expand Down
Loading