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 permission bugs 0815 (#3081)

ybzky870cd22e5c44
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 b0a55062d6..72d2aedd2e 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,7 +14,7 @@
* limitations under the License.
*/
-import type { IActionInfo, IAllowedRequest, IBatchAllowedResponse, ICollaborator, ICreateRequest, ICreateRequest_SelectRangeObject, ICreateRequest_WorksheetObject, IListPermPointRequest, IPermissionPoint, IPutCollaboratorsRequest, IUnitRoleKV, IUpdatePermPointRequest, UnitAction } from '@univerjs/protocol';
+import type { IActionInfo, IAllowedRequest, IBatchAllowedResponse, ICollaborator, ICreateRequest, ICreateRequest_SelectRangeObject, IListPermPointRequest, IPermissionPoint, IPutCollaboratorsRequest, IUnitRoleKV, IUpdatePermPointRequest, UnitAction } from '@univerjs/protocol';
import { CreateRequest_WorkSheetObjectScope, UnitObject, UnitRole, UniverType } from '@univerjs/protocol';
import { Inject } from '../../common/di';
import { Tools } from '../../shared/tools';
@@ -100,49 +100,17 @@ export class AuthzIoLocalService implements IAuthzIoService {
return id;
}
- async allowed(config: IAllowedRequest): Promise<IActionInfo[]> {
- if (this._permissionMap.has(config.objectID)) {
- const rule = this._permissionMap.get(config.objectID);
- if (rule && rule.objectType === UnitObject.Worksheet) {
- return (rule as unknown as ICreateRequest_WorksheetObject)?.strategies?.map((s) => {
- return {
- action: s.action,
- allowed: s.role === UnitRole.Editor,
- };
- });
- }
- }
- return config.actions.map((a) => ({
- action: a, allowed: this._getRole(UnitRole.Owner) || this._getRole(UnitRole.Editor),
- }));
+ async allowed(_config: IAllowedRequest): Promise<IActionInfo[]> {
+ // Because this is a mockService for handling permissions, we will not write real logic in it. We will only return an empty array to ensure that the permissions originally set by the user are not modified.
+ // If you want to achieve persistence of permissions, you can modify the logic here.
+ return Promise.resolve([]);
}
- async batchAllowed(config: IAllowedRequest[]): Promise<IBatchAllowedResponse['objectActions']> {
- const user = this._userManagerService.getCurrentUser();
- const defaultValue = config.map((item) => ({
- unitID: item.unitID,
- objectID: item.objectID,
- actions: item.actions.map((action) => ({
- action,
- allowed: false,
- })),
- }));
- if (!user) {
- return defaultValue;
- }
- if (isDevRole(user.userID, UnitRole.Owner) || isDevRole(user.userID, UnitRole.Editor)) {
- return config.map((item) => ({
- unitID: item.unitID,
- objectID: item.objectID,
- actions: item.actions.map((action) => ({
- action,
- allowed: true,
- })),
- }));
- }
- return defaultValue;
+ async batchAllowed(_config: IAllowedRequest[]): Promise<IBatchAllowedResponse['objectActions']> {
+ return Promise.resolve([]);
}
+ // eslint-disable-next-line max-lines-per-function
async list(config: IListPermPointRequest): Promise<IPermissionPoint[]> {
const result: IPermissionPoint[] = [];
config.objectIDs.forEach((objectID) => {
@@ -155,16 +123,68 @@ export class AuthzIoLocalService implements IAuthzIoService {
name: rule!.name,
shareOn: false,
shareRole: UnitRole.Owner,
- // TODO: @gggpound
shareScope: -1,
creator: createDefaultUser(UnitRole.Owner),
- strategies: [],
+ // mock data
+ strategies: [
+ {
+ action: 6,
+ role: 1,
+ },
+ {
+ action: 16,
+ role: 1,
+ },
+ {
+ action: 17,
+ role: 1,
+ },
+ {
+ action: 18,
+ role: 1,
+ },
+ {
+ action: 19,
+ role: 1,
+ },
+ {
+ action: 33,
+ role: 1,
+ },
+ {
+ action: 34,
+ role: 1,
+ },
+ {
+ action: 35,
+ role: 1,
+ },
+ {
+ action: 36,
+ role: 1,
+ },
+ {
+ action: 37,
+ role: 1,
+ },
+ {
+ action: 38,
+ role: 1,
+ },
+ {
+ action: 39,
+ role: 1,
+ },
+ {
+ action: 40,
+ role: 1,
+ },
+ {
+ action: 41,
+ role: 1,
+ },
+ ],
actions: config.actions.map((a) => {
- // if (a === UnitAction.ManageCollaborator) {
-
- // } else {
-
- // }
return { action: a, allowed: this._getRole(UnitRole.Owner) || this._getRole(UnitRole.Editor) };
}),
};
diff --git a/packages/core/src/services/permission/permission.service.ts b/packages/core/src/services/permission/permission.service.ts
index 606f72457b..0e16779036 100644
--- a/packages/core/src/services/permission/permission.service.ts
+++ b/packages/core/src/services/permission/permission.service.ts
@@ -58,7 +58,7 @@ export class PermissionService extends Disposable implements IPermissionService
const subject = permissionPoint.getValue() as IPermissionPoint<T>;
subject.value = value;
subject.status = PermissionStatus.DONE;
- permissionPoint.next({ ...subject });
+ permissionPoint.next(subject);
this._permissionPointUpdate$.next(subject);
};
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 81be2c512d..11654b8017 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
@@ -46,7 +46,7 @@ export class SheetPermissionInitController extends Disposable {
this._initWorksheetPermissionFromSnapshot();
this._initWorksheetPermissionChange();
this._initWorksheetPermissionPointsChange();
- this.initWorkbookPermissionChange();
+ this._initWorkbookPermissionFromSnapshot();
this._initUserChange();
this._initViewModelByRangeInterceptor();
this._initViewModelBySheetInterceptor();
@@ -100,12 +100,6 @@ export class SheetPermissionInitController extends Disposable {
this._univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET).forEach((workbook) => {
initRangePermissionFunc(workbook);
});
-
- this.disposeWithMe(
- this._univerInstanceService.getTypeOfUnitAdded$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
- initRangePermissionFunc(workbook);
- })
- );
}
private _initRangePermissionChange() {
@@ -150,7 +144,10 @@ export class SheetPermissionInitController extends Disposable {
}
public initWorkbookPermissionChange(_unitId?: string) {
- const unitId = _unitId || this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getUnitId();
+ const unitId = _unitId || this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)?.getUnitId();
+ if (!unitId) {
+ return;
+ }
this._authzIoService.allowed({
objectID: unitId,
objectType: UnitObject.Workbook,
@@ -168,6 +165,12 @@ export class SheetPermissionInitController extends Disposable {
});
}
+ private _initWorkbookPermissionFromSnapshot() {
+ this._univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET).forEach((workbook) => {
+ this.initWorkbookPermissionChange(workbook.getUnitId());
+ });
+ }
+
private _initWorksheetPermissionChange() {
this.disposeWithMe(
this._worksheetProtectionRuleModel.ruleChange$.subscribe((info) => {
@@ -280,12 +283,6 @@ export class SheetPermissionInitController extends Disposable {
this._univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET).forEach((workbook) => {
initSheetPermissionFunc(workbook);
});
-
- this.disposeWithMe(
- this._univerInstanceService.getTypeOfUnitAdded$<Workbook>(UniverInstanceType.UNIVER_SHEET).subscribe((workbook) => {
- initSheetPermissionFunc(workbook);
- })
- );
}
private _initUserChange() {
@@ -333,7 +330,7 @@ export class SheetPermissionInitController extends Disposable {
});
});
- this.initWorkbookPermissionChange();
+ this._initWorkbookPermissionFromSnapshot();
this._initWorksheetPermissionFromSnapshot();
this._initRangePermissionFromSnapshot();
});
diff --git a/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-base.controller.ts b/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-base.controller.ts
index b72aa6152d..5c56b6a455 100644
--- a/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-base.controller.ts
+++ b/packages/sheets-ui/src/controllers/permission/sheet-permission-interceptor-base.controller.ts
@@ -34,9 +34,10 @@ import { ApplyFormatPainterCommand } from '../../commands/commands/set-format-pa
import { SetRangeBoldCommand, SetRangeItalicCommand, SetRangeStrickThroughCommand, SetRangeUnderlineCommand } from '../../commands/commands/inline-format.command';
import { AutoFillCommand } from '../../commands/commands/auto-fill.command';
import { PREDEFINED_HOOK_NAME } from '../../services/clipboard/clipboard.service';
+import type { IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service';
type ICellPermission = Record<UnitAction, boolean> & { ruleId?: string; ranges?: IRange[] };
-type ICheckPermissionCommandParams = IMoveRowsCommandParams | IMoveColsCommandParams | IMoveRangeCommandParams | ISetRangeValuesCommandParams | ISheetPasteParams | ISetSpecificRowsVisibleCommandParams;
+type ICheckPermissionCommandParams = IEditorBridgeServiceVisibleParam | IMoveRowsCommandParams | IMoveColsCommandParams | IMoveRangeCommandParams | ISetRangeValuesCommandParams | ISheetPasteParams | ISetSpecificRowsVisibleCommandParams;
export const SHEET_PERMISSION_PASTE_PLUGIN = 'SHEET_PERMISSION_PASTE_PLUGIN';
@@ -94,7 +95,6 @@ export class SheetPermissionInterceptorBaseController extends Disposable {
switch (id) {
case InsertCommand.id:
case IMEInputCommand.id:
- case SetCellEditVisibleOperation.id:
if (this._contextService.getContextValue(FOCUSING_EDITOR_STANDALONE) === true) {
break;
}
@@ -105,6 +105,17 @@ export class SheetPermissionInterceptorBaseController extends Disposable {
});
errorMsg = this._localeService.t('permission.dialog.editErr');
break;
+ case SetCellEditVisibleOperation.id:
+ if ((params as IEditorBridgeServiceVisibleParam).visible === false) {
+ break;
+ }
+ permission = this.permissionCheckWithoutRange({
+ workbookTypes: [WorkbookEditablePermission],
+ rangeTypes: [RangeProtectionPermissionEditPoint],
+ worksheetTypes: [WorksheetSetCellValuePermission, WorksheetEditPermission],
+ });
+ errorMsg = this._localeService.t('permission.dialog.editErr');
+ break;
case SetRangeValuesCommand.id:
if (isICellData((params as ISetRangeValuesCommandParams).value) && ((params as ISetRangeValuesCommandParams).value as ICellData).f) {
permission = this._permissionCheckWithFormula((params as ISetRangeValuesCommandParams));
diff --git a/packages/sheets-ui/src/views/permission/footer/permission-detail-footer.tsx b/packages/sheets-ui/src/views/permission/footer/permission-detail-footer.tsx
index 7904e95f49..6063002937 100644
--- a/packages/sheets-ui/src/views/permission/footer/permission-detail-footer.tsx
+++ b/packages/sheets-ui/src/views/permission/footer/permission-detail-footer.tsx
@@ -46,6 +46,7 @@ export const SheetPermissionPanelDetailFooter = () => {
if (!activeRule.name || rangeErrMsg) return;
const collaborators = sheetPermissionUserManagerService.selectUserList;
if (activeRule.viewStatus === viewState.othersCanView) {
+ // If it is readable by everyone, then all the users not in the original collaboration list will be set as readers from all the user information pulled back and added to the collaborators array
sheetPermissionUserManagerService.allUserList.forEach((user) => {
const hasInCollaborators = collaborators.some((collaborator) => collaborator.id === user.id);
if (!hasInCollaborators) {
@@ -57,19 +58,23 @@ export const SheetPermissionPanelDetailFooter = () => {
}
});
}
- // edit rule
-
const isSameCollaborators = getUserListEqual(collaborators, sheetPermissionUserManagerService.oldCollaboratorList);
+
+ // If modify existing permission information, need to determine whether the collaborators have changed. If so, create a new permissionId. If not, modify the rendering information stored in the memory.
if (activeRule.permissionId) {
const oldRule = sheetPermissionPanelModel.oldRule;
if (activeRule.unitType === oldRule?.unitType && activeRule.name === oldRule.name && activeRule.description === oldRule.description && activeRule.ranges === oldRule.ranges && !isSameCollaborators) {
+ // collaborators is changed and rendering info is not change, so call the interface to update the list of collaborators
await authzIoService.putCollaborators({
objectID: activeRule.permissionId,
unitID: activeRule.unitId,
collaborators,
});
} else {
+ // create new permissionId, broadcast to other collaborators
let newPermissionId = activeRule.permissionId;
+
+ // If the collaborator does not change, there is no need to create a new permissionId
if (!isSameCollaborators) {
if (activeRule.unitType === UnitObject.Worksheet) {
newPermissionId = await authzIoService.create({
@@ -95,6 +100,7 @@ export const SheetPermissionPanelDetailFooter = () => {
});
}
}
+
commandService.executeCommand(SetProtectionCommand.id, {
rule: {
...activeRule,
diff --git a/packages/sheets/src/model/range-protection-rule.model.ts b/packages/sheets/src/model/range-protection-rule.model.ts
index 2abca27732..aa156b1321 100644
--- a/packages/sheets/src/model/range-protection-rule.model.ts
+++ b/packages/sheets/src/model/range-protection-rule.model.ts
@@ -146,8 +146,8 @@ export class RangeProtectionRuleModel {
this._model = result;
}
- deleteUnitModel() {
- this._model.clear();
+ deleteUnitModel(unitId: string) {
+ this._model.delete(unitId);
}
createRuleId(unitId: string, subUnitId: string) {
diff --git a/packages/sheets/src/services/permission/range-permission/range-protection.service.ts b/packages/sheets/src/services/permission/range-permission/range-protection.service.ts
index df4185d67e..cd3422cdca 100644
--- a/packages/sheets/src/services/permission/range-permission/range-protection.service.ts
+++ b/packages/sheets/src/services/permission/range-permission/range-protection.service.ts
@@ -121,8 +121,8 @@ export class RangeProtectionService extends Disposable {
});
});
},
- onUnLoad: () => {
- this._selectionProtectionRuleModel.deleteUnitModel();
+ onUnLoad: (unitId: string) => {
+ this._selectionProtectionRuleModel.deleteUnitModel(unitId);
},
})
);
diff --git a/packages/sheets/src/services/permission/worksheet-permission/worksheet-permission-point.model.ts b/packages/sheets/src/services/permission/worksheet-permission/worksheet-permission-point.model.ts
index bb695f3395..1ddd981e41 100644
--- a/packages/sheets/src/services/permission/worksheet-permission/worksheet-permission-point.model.ts
+++ b/packages/sheets/src/services/permission/worksheet-permission/worksheet-permission-point.model.ts
@@ -82,8 +82,8 @@ export class WorksheetProtectionPointModel {
this._model = result;
}
- deleteUnitModel() {
- this._model.clear();
+ deleteUnitModel(unitId: string) {
+ this._model.delete(unitId);
}
private _ensureSubUnitMap(unitId: string) {
diff --git a/packages/sheets/src/services/permission/worksheet-permission/worksheet-permission-rule.model.ts b/packages/sheets/src/services/permission/worksheet-permission/worksheet-permission-rule.model.ts
index ed30f0fb94..2b3dc4d9cc 100644
--- a/packages/sheets/src/services/permission/worksheet-permission/worksheet-permission-rule.model.ts
+++ b/packages/sheets/src/services/permission/worksheet-permission/worksheet-permission-rule.model.ts
@@ -112,8 +112,8 @@ export class WorksheetProtectionRuleModel {
this._model = result;
}
- deleteUnitModel() {
- this._model.clear();
+ deleteUnitModel(unitId: string) {
+ this._model.delete(unitId);
}
private _ensureSubUnitMap(unitId: string) {
diff --git a/packages/sheets/src/services/permission/worksheet-permission/worksheet-permission.service.ts b/packages/sheets/src/services/permission/worksheet-permission/worksheet-permission.service.ts
index 6d56ade2b7..c0a42b38b0 100644
--- a/packages/sheets/src/services/permission/worksheet-permission/worksheet-permission.service.ts
+++ b/packages/sheets/src/services/permission/worksheet-permission/worksheet-permission.service.ts
@@ -155,8 +155,8 @@ export class WorksheetPermissionService extends RxDisposable {
});
this._worksheetProtectionRuleModel.changeRuleInitState(true);
},
- onUnLoad: () => {
- this._worksheetProtectionRuleModel.deleteUnitModel();
+ onUnLoad: (unitId: string) => {
+ this._worksheetProtectionRuleModel.deleteUnitModel(unitId);
},
})
);
@@ -191,8 +191,8 @@ export class WorksheetPermissionService extends RxDisposable {
});
});
},
- onUnLoad: () => {
- this._worksheetProtectionPointRuleModel.deleteUnitModel();
+ onUnLoad: (unitId: string) => {
+ this._worksheetProtectionPointRuleModel.deleteUnitModel(unitId);
},
})
);