diff --git a/packages/plugins/@nocobase/plugin-acl/src/client/RolesManagement.tsx b/packages/plugins/@nocobase/plugin-acl/src/client/RolesManagement.tsx index 49b164ad0e..45a90c9b16 100644 --- a/packages/plugins/@nocobase/plugin-acl/src/client/RolesManagement.tsx +++ b/packages/plugins/@nocobase/plugin-acl/src/client/RolesManagement.tsx @@ -1,6 +1,13 @@ import React, { useState } from 'react'; import { Card, Row, Col, Tabs, Divider } from 'antd'; -import { CollectionProvider, CollectionProvider_deprecated, ResourceActionProvider, usePlugin } from '@nocobase/client'; +import { + CollectionProvider, + CollectionProvider_deprecated, + ResourceActionProvider, + SchemaComponentContext, + usePlugin, + useSchemaComponentContext, +} from '@nocobase/client'; import { ISchema, Schema } from '@formily/react'; import { RolesMenu } from './RolesMenu'; import { useACLTranslation } from './locale'; @@ -58,53 +65,56 @@ export const RolesManagement: React.FC = () => { children: item.Component ? React.createElement(item.Component, { active: activeKey === name }) : null, })); const [role, setRole] = useState(null); + const scCtx = useSchemaComponentContext(); return ( - - - - - + + + + + - - - - - - - - - - - setActiveKey(key)} - items={[ - { - key: 'permissions', - label: t('Permissions'), - children: , - }, - ...tabs, - ]} - /> - - - - + }} + > + + + + + + + + + + + setActiveKey(key)} + items={[ + { + key: 'permissions', + label: t('Permissions'), + children: , + }, + ...tabs, + ]} + /> + + + + + ); }; diff --git a/packages/plugins/@nocobase/plugin-users/src/server/__tests__/role-users.test.ts b/packages/plugins/@nocobase/plugin-users/src/server/__tests__/role-users.test.ts new file mode 100644 index 0000000000..5bb0589abf --- /dev/null +++ b/packages/plugins/@nocobase/plugin-users/src/server/__tests__/role-users.test.ts @@ -0,0 +1,56 @@ +import { Database, Repository } from '@nocobase/database'; +import { MockServer, createMockServer } from '@nocobase/test'; + +describe('actions', () => { + let app: MockServer; + let db: Database; + let repo: Repository; + let agent: any; + + beforeAll(async () => { + app = await createMockServer({ + plugins: ['acl', 'users', 'data-source-manager'], + }); + db = app.db; + repo = db.getRepository('users'); + agent = app.agent(); + }); + + afterAll(async () => { + await app.destroy(); + }); + + it('should list users exclude role', async () => { + const res = await agent.resource('users').listExcludeRole({ + roleName: ['admin'], + }); + expect(res.status).toBe(200); + expect(res.body.data.length).toBe(0); + }); + + it('should list users exclude role with filter', async () => { + let res = await agent.resource('users').listExcludeRole({ + roleName: ['test'], + }); + expect(res.status).toBe(200); + expect(res.body.data.length).toBe(1); + + res = await agent.resource('users').listExcludeRole({ + roleName: ['test'], + filter: { + id: 1, + }, + }); + expect(res.status).toBe(200); + expect(res.body.data.length).toBe(1); + + res = await agent.resource('users').listExcludeRole({ + roleName: ['test'], + filter: { + id: 2, + }, + }); + expect(res.status).toBe(200); + expect(res.body.data.length).toBe(0); + }); +}); diff --git a/packages/plugins/@nocobase/plugin-users/src/server/actions/users.ts b/packages/plugins/@nocobase/plugin-users/src/server/actions/users.ts index fa1aadc7b6..a843ce57c1 100644 --- a/packages/plugins/@nocobase/plugin-users/src/server/actions/users.ts +++ b/packages/plugins/@nocobase/plugin-users/src/server/actions/users.ts @@ -25,19 +25,21 @@ export const listExcludeRole = async (ctx: Context, next: Next) => { }, }); const userIds = users.map((user: { id: number }) => user.id); + if (userIds.length) { + ctx.action.mergeParams({ + filter: { + id: { + $notIn: userIds, + }, + }, + }); + } + const { filter } = ctx.action.params; const [rows, count] = await repo.findAndCount({ context: ctx, offset: (page - 1) * pageSize, limit: pageSize, - // Ensure memberIds is not empty to resolve strange issue: - // notIn: [] will be translated to "WHERE id IS NULL" - filter: userIds.length - ? { - id: { - $notIn: userIds, - }, - } - : {}, + filter, }); ctx.body = { count,