fix(plugin-api-keys): use currentRoles instead of get roles from roles:list (#2163)
This commit is contained in:
parent
9f4fc777b0
commit
677442c844
@ -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<any, any> & {
|
||||
objectValue?: boolean;
|
||||
|
@ -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({
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
|
@ -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 (
|
||||
<CollectionManagerProvider collections={[apiKeysCollection]}>
|
||||
<RecursionField schema={configurationSchema} />
|
||||
<SchemaComponentOptions scope={{ currentRoles }}>
|
||||
<RecursionField schema={configurationSchema} />
|
||||
</SchemaComponentOptions>
|
||||
</CollectionManagerProvider>
|
||||
);
|
||||
};
|
||||
|
@ -0,0 +1,9 @@
|
||||
import { useCurrentRoles } from '@nocobase/client';
|
||||
|
||||
export const useCurrentRolesProps = () => {
|
||||
const options = useCurrentRoles();
|
||||
|
||||
return {
|
||||
options,
|
||||
};
|
||||
};
|
@ -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 }}',
|
||||
},
|
||||
},
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user