diff --git a/packages/core/client/src/schema-component/antd/select/Select.tsx b/packages/core/client/src/schema-component/antd/select/Select.tsx index 0da2cb018..cb9cdfbdf 100644 --- a/packages/core/client/src/schema-component/antd/select/Select.tsx +++ b/packages/core/client/src/schema-component/antd/select/Select.tsx @@ -3,10 +3,10 @@ import { connect, mapProps, mapReadPretty } from '@formily/react'; import { isValid, toArr } from '@formily/shared'; import { isPlainObject } from '@nocobase/utils/client'; import type { SelectProps } from 'antd'; -import { Select as AntdSelect, Empty, Spin } from 'antd'; +import { Empty, Select as AntdSelect, Spin } from 'antd'; import React from 'react'; import { ReadPretty } from './ReadPretty'; -import { FieldNames, defaultFieldNames, getCurrentOptions } from './utils'; +import { defaultFieldNames, FieldNames, getCurrentOptions } from './utils'; type Props = SelectProps & { objectValue?: boolean; diff --git a/packages/core/client/src/user/CurrentUserProvider.tsx b/packages/core/client/src/user/CurrentUserProvider.tsx index e4bb12102..0fc4a2d06 100644 --- a/packages/core/client/src/user/CurrentUserProvider.tsx +++ b/packages/core/client/src/user/CurrentUserProvider.tsx @@ -1,7 +1,9 @@ import { Spin } from 'antd'; -import React, { createContext, useContext } from 'react'; +import React, { createContext, useContext, useMemo } from 'react'; import { Navigate, useLocation } from 'react-router-dom'; +import { useACLRoleContext } from '../acl'; import { useRequest } from '../api-client'; +import { useCompile } from '../schema-component'; export const CurrentUserContext = createContext(null); @@ -9,6 +11,23 @@ export const useCurrentUserContext = () => { return useContext(CurrentUserContext); }; +export const useCurrentRoles = () => { + const { allowAnonymous } = useACLRoleContext(); + const { data } = useCurrentUserContext(); + const compile = useCompile(); + const options = useMemo(() => { + const roles = data?.data?.roles || []; + if (allowAnonymous) { + roles.push({ + title: 'Anonymous', + name: 'anonymous', + }); + } + return compile(roles); + }, [allowAnonymous, data?.data?.roles]); + return options; +}; + export const CurrentUserProvider = (props) => { const location = useLocation(); const result = useRequest({ diff --git a/packages/core/client/src/user/SwitchRole.tsx b/packages/core/client/src/user/SwitchRole.tsx index a56f1f7e0..5903ef4f9 100644 --- a/packages/core/client/src/user/SwitchRole.tsx +++ b/packages/core/client/src/user/SwitchRole.tsx @@ -1,29 +1,8 @@ import { MenuProps, Select } from 'antd'; import React, { useMemo } from 'react'; import { useTranslation } from 'react-i18next'; -import { useACLRoleContext } from '../acl'; import { useAPIClient } from '../api-client'; -import { useCompile } from '../schema-component'; -import { useCurrentUserContext } from './CurrentUserProvider'; - -const useCurrentRoles = () => { - const { allowAnonymous } = useACLRoleContext(); - const { data } = useCurrentUserContext(); - const compile = useCompile(); - const options = (data?.data?.roles || []).map((item) => { - return { - title: item.title, - name: item.name, - }; - }); - if (allowAnonymous) { - options.push({ - title: 'Anonymous', - name: 'anonymous', - }); - } - return compile(options); -}; +import { useCurrentRoles } from './CurrentUserProvider'; export const useSwitchRole = () => { const api = useAPIClient(); diff --git a/packages/plugins/acl/src/collections/roles.ts b/packages/plugins/acl/src/collections/roles.ts index c6d533ad5..8e1b67fcd 100644 --- a/packages/plugins/acl/src/collections/roles.ts +++ b/packages/plugins/acl/src/collections/roles.ts @@ -85,16 +85,5 @@ export default { name: 'snippets', defaultValue: ['!ui.*', '!pm', '!pm.*'], }, - { - type: 'belongsToMany', - name: 'users', - target: 'users', - foreignKey: 'roleName', - otherKey: 'userId', - onDelete: 'CASCADE', - sourceKey: 'name', - targetKey: 'id', - through: 'rolesUsers', - }, ], } as CollectionOptions; diff --git a/packages/plugins/api-keys/src/client/Configuration/index.tsx b/packages/plugins/api-keys/src/client/Configuration/index.tsx index 2c1b1ed38..408e0881d 100644 --- a/packages/plugins/api-keys/src/client/Configuration/index.tsx +++ b/packages/plugins/api-keys/src/client/Configuration/index.tsx @@ -1,13 +1,17 @@ import { RecursionField } from '@formily/react'; -import { CollectionManagerProvider } from '@nocobase/client'; +import { CollectionManagerProvider, SchemaComponentOptions, useCurrentRoles } from '@nocobase/client'; import React from 'react'; import { apiKeysCollection } from '../../collections'; import { configurationSchema } from './schema'; export const Configuration = () => { + const currentRoles = useCurrentRoles(); + return ( - + + + ); }; diff --git a/packages/plugins/api-keys/src/client/Configuration/roles.ts b/packages/plugins/api-keys/src/client/Configuration/roles.ts new file mode 100644 index 000000000..00cb23733 --- /dev/null +++ b/packages/plugins/api-keys/src/client/Configuration/roles.ts @@ -0,0 +1,9 @@ +import { useCurrentRoles } from '@nocobase/client'; + +export const useCurrentRolesProps = () => { + const options = useCurrentRoles(); + + return { + options, + }; +}; diff --git a/packages/plugins/api-keys/src/collections/api-keys.ts b/packages/plugins/api-keys/src/collections/api-keys.ts index e430ef564..49725f9ee 100644 --- a/packages/plugins/api-keys/src/collections/api-keys.ts +++ b/packages/plugins/api-keys/src/collections/api-keys.ts @@ -39,23 +39,14 @@ export default { uiSchema: { type: 'object', title: '{{t("Roles")}}', - 'x-component': 'AssociationField', + 'x-component': 'Select', 'x-component-props': { fieldNames: { label: 'title', value: 'name', }, - service: { - params: { - filter: { - $and: [ - { - users: { id: { $eq: '{{$user.id}}' } }, - }, - ], - }, - }, - }, + objectValue: true, + options: '{{ currentRoles }}', }, }, },