From 9b27fa955a654b926bf1b794db7514268f1b5922 Mon Sep 17 00:00:00 2001 From: Junyi Date: Wed, 20 Mar 2024 23:22:22 +0800 Subject: [PATCH] feat(plugin-user): add model method desensitize() to filter hidden field (#3768) * feat(plugin-user): add model method desensitize() to filter hidden field * fix(plugin-workflow-action-trigger): fix user fields in context --- .../src/server/__tests__/model.test.ts | 27 +++++++++++++++++++ .../src/server/models/UserModel.ts | 15 +++++++++++ .../plugin-users/src/server/server.ts | 5 +++- .../src/server/ActionTrigger.ts | 3 ++- 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 packages/plugins/@nocobase/plugin-users/src/server/__tests__/model.test.ts create mode 100644 packages/plugins/@nocobase/plugin-users/src/server/models/UserModel.ts diff --git a/packages/plugins/@nocobase/plugin-users/src/server/__tests__/model.test.ts b/packages/plugins/@nocobase/plugin-users/src/server/__tests__/model.test.ts new file mode 100644 index 0000000000..fb7317af9b --- /dev/null +++ b/packages/plugins/@nocobase/plugin-users/src/server/__tests__/model.test.ts @@ -0,0 +1,27 @@ +import Database from '@nocobase/database'; +import { createMockServer, MockServer } from '@nocobase/test'; +import { UserModel } from '../models/UserModel'; + +describe('models', () => { + let app: MockServer; + let db: Database; + + beforeEach(async () => { + app = await createMockServer({ + plugins: ['auth', 'users'], + }); + db = app.db; + }); + + afterEach(async () => { + await app.destroy(); + }); + + it('model registeration', async () => { + const model = db.getModel('users'); + const u1 = model.build({ nickname: 'test', password: '123' }); + expect(u1).toBeInstanceOf(UserModel); + const n = u1.desensitize(); + expect(n.password).toBeUndefined(); + }); +}); diff --git a/packages/plugins/@nocobase/plugin-users/src/server/models/UserModel.ts b/packages/plugins/@nocobase/plugin-users/src/server/models/UserModel.ts new file mode 100644 index 0000000000..93575c9dc1 --- /dev/null +++ b/packages/plugins/@nocobase/plugin-users/src/server/models/UserModel.ts @@ -0,0 +1,15 @@ +import { Model } from '@nocobase/database'; + +export class UserModel extends Model { + desensitize() { + const { fields } = (this.constructor as typeof UserModel).collection; + const result = (this.constructor as typeof UserModel).build({}, { isNewRecord: this.isNewRecord }); + for (const [name, value] of Object.entries(this.get())) { + const field = fields.get(name); + if (field && !field.options.hidden) { + result.set(name, value); + } + } + return result; + } +} diff --git a/packages/plugins/@nocobase/plugin-users/src/server/server.ts b/packages/plugins/@nocobase/plugin-users/src/server/server.ts index 359caef6e7..402bfa2883 100644 --- a/packages/plugins/@nocobase/plugin-users/src/server/server.ts +++ b/packages/plugins/@nocobase/plugin-users/src/server/server.ts @@ -5,9 +5,13 @@ import { resolve } from 'path'; import * as actions from './actions/users'; import { Cache } from '@nocobase/cache'; +import { UserModel } from './models/UserModel'; export default class PluginUsersServer extends Plugin { async beforeLoad() { + this.db.registerModels({ + UserModel, + }); this.db.registerOperators({ $isCurrentUser(_, ctx) { return { @@ -116,7 +120,6 @@ export default class PluginUsersServer extends Plugin { async load() { await this.importCollections(resolve(__dirname, 'collections')); - this.db.addMigrations({ namespace: 'users', directory: resolve(__dirname, 'migrations'), diff --git a/packages/plugins/@nocobase/plugin-workflow-action-trigger/src/server/ActionTrigger.ts b/packages/plugins/@nocobase/plugin-workflow-action-trigger/src/server/ActionTrigger.ts index d05b498765..b113924b01 100644 --- a/packages/plugins/@nocobase/plugin-workflow-action-trigger/src/server/ActionTrigger.ts +++ b/packages/plugins/@nocobase/plugin-workflow-action-trigger/src/server/ActionTrigger.ts @@ -57,8 +57,9 @@ export default class extends Trigger { const { triggerWorkflows = '', values } = context.action.params; const { currentUser, currentRole } = context.state; + const { model: UserModel } = this.workflow.db.getCollection('users'); const userInfo = { - user: toJSON(currentUser), + user: UserModel.build(currentUser).desensitize(), roleName: currentRole, };