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.

chore(user-manager): add generic constraints and decouple from protocols (#2513)

Gpound.liu4802936b93bc
diff --git a/examples/src/sheets/main.ts b/examples/src/sheets/main.ts
index 3156413c56..c207767e75 100644
--- a/examples/src/sheets/main.ts
+++ b/examples/src/sheets/main.ts
@@ -140,8 +140,7 @@ univer.registerPlugin(UniverDebuggerPlugin);
const injector = univer.__getInjector();
const userManagerService = injector.get(UserManagerService);
-userManagerService.currentUser = mockUser;
-userManagerService.addUser(mockUser);
+userManagerService.setCurrentUser(mockUser);
declare global {
// eslint-disable-next-line ts/naming-convention
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 6e3475a18d..adf9588b48 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -108,7 +108,7 @@ export {
} from './services/undoredo/undoredo.service';
export * from './shared';
export { fromCallback } from './shared/rxjs';
-export { UserManagerService } from './services/user-manager/user-manager.service';
+export { UserManagerService, type IUser } from './services/user-manager/user-manager.service';
// #region sheet
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 ee84ccaa28..926dc1f234 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
@@ -40,7 +40,7 @@ export class AuthzIoLocalService implements IAuthzIoService {
}
private _getRole(type: UnitRole) {
- const user = this._userManagerService.currentUser;
+ const user = this._userManagerService.getCurrentUser();
if (!user) {
return false;
}
@@ -83,14 +83,7 @@ export class AuthzIoLocalService implements IAuthzIoService {
}
case UnitObject.Worksheet: {
const params = config.worksheetObject!;
- const user = this._userManagerService.currentUser;
this._permissionMap.set(id, { ...params, objectType: config.objectType });
- // const actions = params.strategies.map((s) => {
- // return {
- // action: s.action,
- // allowed: isDevRole(user.userID, UnitRole.Owner) || s.role === UnitRole.Editor,
- // };
- // });
}
}
@@ -115,7 +108,7 @@ export class AuthzIoLocalService implements IAuthzIoService {
}
async batchAllowed(config: IAllowedRequest[]): Promise<IBatchAllowedResponse['objectActions']> {
- const user = this._userManagerService.currentUser;
+ const user = this._userManagerService.getCurrentUser();
const defaultValue = config.map((item) => ({
unitID: item.unitID,
objectID: item.objectID,
diff --git a/packages/core/src/services/user-manager/index.ts b/packages/core/src/services/user-manager/index.ts
deleted file mode 100644
index 1054df5975..0000000000
--- a/packages/core/src/services/user-manager/index.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-/**
- * Copyright 2023-present DreamNum Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-export { createDefaultUser } from './const';
diff --git a/packages/core/src/services/user-manager/user-manager.service.ts b/packages/core/src/services/user-manager/user-manager.service.ts
index fe5205c771..6403a8b22f 100644
--- a/packages/core/src/services/user-manager/user-manager.service.ts
+++ b/packages/core/src/services/user-manager/user-manager.service.ts
@@ -14,12 +14,13 @@
* limitations under the License.
*/
-import type { IUser } from '@univerjs/protocol';
-
import { BehaviorSubject, Subject } from 'rxjs';
import { LifecycleStages, OnLifecycle } from '../lifecycle/lifecycle';
import { createDefaultUser } from './const';
+export interface IUser {
+ userID: string; name: string; avatar?: string;
+};
@OnLifecycle(LifecycleStages.Starting, UserManagerService)
export class UserManagerService {
private _model = new Map<string, IUser>();
@@ -31,22 +32,22 @@ export class UserManagerService {
* @memberof UserManagerService
*/
public currentUser$ = this._currentUser$.asObservable();
-
- get currentUser() {
- return this._currentUser$.getValue();
+ getCurrentUser<T extends IUser>() {
+ return this._currentUser$.getValue() as T;
}
- set currentUser(user: IUser) {
+ setCurrentUser<T extends IUser>(user: T) {
+ this.addUser(user);
this._currentUser$.next(user);
}
- addUser(user: IUser) {
+ addUser<T extends IUser>(user: T) {
this._model.set(user.userID, user);
this._userChange$.next({ type: 'add', user });
}
- getUser(userId: string, callBack?: () => void) {
- const user = this._model.get(userId);
+ getUser<T extends IUser>(userId: string, callBack?: () => void) {
+ const user = this._model.get(userId) as T;
if (user) {
return user;
}
diff --git a/packages/debugger/src/commands/operations/change-user.operation.ts b/packages/debugger/src/commands/operations/change-user.operation.ts
index 7b9e6cfd9c..30e401abc4 100644
--- a/packages/debugger/src/commands/operations/change-user.operation.ts
+++ b/packages/debugger/src/commands/operations/change-user.operation.ts
@@ -38,7 +38,7 @@ export const ChangeUserCommand: ICommand = {
handler: async (accessor: IAccessor, params: IChangeUserParams) => {
const userManagerService = accessor.get(UserManagerService);
const univerInstanceService = accessor.get(IUniverInstanceService);
- userManagerService.currentUser = createDefaultUser(params.value);
+ userManagerService.setCurrentUser(createDefaultUser(params.value));
const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
const permissionService = accessor.get(IPermissionService);
const unitId = workbook.getUnitId();
diff --git a/packages/sheets-ui/src/views/permission/panel-list/index.tsx b/packages/sheets-ui/src/views/permission/panel-list/index.tsx
index 253fcdcd39..ba42bd48b7 100644
--- a/packages/sheets-ui/src/views/permission/panel-list/index.tsx
+++ b/packages/sheets-ui/src/views/permission/panel-list/index.tsx
@@ -55,7 +55,7 @@ export const SheetPermissionPanelList = () => {
const authzIoService = useDependency(IAuthzIoService);
const permissionService = useDependency(IPermissionService);
const usesManagerService = useDependency(UserManagerService);
- const currentUser = usesManagerService.currentUser;
+ const currentUser = usesManagerService.getCurrentUser();
const [currentRuleRanges, currentRuleRangesSet] = useState<IRange[]>([]);
useObservable(worksheetProtectionModel.ruleRefresh$);
diff --git a/packages/thread-comment-ui/src/views/thread-comment-panel/index.tsx b/packages/thread-comment-ui/src/views/thread-comment-panel/index.tsx
index 90973294fe..9282849aec 100644
--- a/packages/thread-comment-ui/src/views/thread-comment-panel/index.tsx
+++ b/packages/thread-comment-ui/src/views/thread-comment-panel/index.tsx
@@ -53,7 +53,7 @@ export const ThreadCommentPanel = (props: IThreadCommentPanelProps) => {
const update = useObservable(threadCommentModel.commentUpdate$);
const commandService = useDependency(ICommandService);
const subUnitId = useObservable(subUnitId$);
- const currentUser = userService.currentUser;
+ const currentUser = userService.getCurrentUser();
const shouldScroll = useRef(true);
const prefix = 'panel';
const comments = useMemo(() => {
diff --git a/packages/thread-comment-ui/src/views/thread-comment-tree/index.tsx b/packages/thread-comment-ui/src/views/thread-comment-tree/index.tsx
index 4c7c07493b..bbc7f198fc 100644
--- a/packages/thread-comment-ui/src/views/thread-comment-tree/index.tsx
+++ b/packages/thread-comment-ui/src/views/thread-comment-tree/index.tsx
@@ -19,11 +19,10 @@ import type { IAddCommentCommandParams, IThreadComment, IUpdateCommentCommandPar
import { AddCommentCommand, DeleteCommentCommand, DeleteCommentTreeCommand, ResolveCommentCommand, ThreadCommentModel, UpdateCommentCommand } from '@univerjs/thread-comment';
import React, { useRef, useState } from 'react';
import { DeleteSingle, MoreHorizontalSingle, ReplyToCommentSingle, ResolvedSingle, SolveSingle } from '@univerjs/icons';
-import { ICommandService, LocaleService, Tools, type UniverInstanceType, UserManagerService } from '@univerjs/core';
+import { ICommandService, type IUser, LocaleService, Tools, type UniverInstanceType, UserManagerService } from '@univerjs/core';
import { useObservable } from '@univerjs/ui';
import dayjs from 'dayjs';
import { Dropdown, Menu, MenuItem } from '@univerjs/design';
-import type { IUser } from '@univerjs/protocol';
import cs from 'clsx';
import type { IThreadCommentEditorInstance } from '../thread-comment-editor';
import { ThreadCommentEditor } from '../thread-comment-editor';