fix(plugin-api-keys): use currentRoles instead of get roles from roles:list (#2163)

This commit is contained in:
Dunqing 2023-07-02 10:36:36 +08:00 committed by GitHub
parent 9f4fc777b0
commit 677442c844
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 50 deletions

View File

@ -3,10 +3,10 @@ import { connect, mapProps, mapReadPretty } from '@formily/react';
import { isValid, toArr } from '@formily/shared'; import { isValid, toArr } from '@formily/shared';
import { isPlainObject } from '@nocobase/utils/client'; import { isPlainObject } from '@nocobase/utils/client';
import type { SelectProps } from 'antd'; 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 React from 'react';
import { ReadPretty } from './ReadPretty'; import { ReadPretty } from './ReadPretty';
import { FieldNames, defaultFieldNames, getCurrentOptions } from './utils'; import { defaultFieldNames, FieldNames, getCurrentOptions } from './utils';
type Props = SelectProps<any, any> & { type Props = SelectProps<any, any> & {
objectValue?: boolean; objectValue?: boolean;

View File

@ -1,7 +1,9 @@
import { Spin } from 'antd'; 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 { Navigate, useLocation } from 'react-router-dom';
import { useACLRoleContext } from '../acl';
import { useRequest } from '../api-client'; import { useRequest } from '../api-client';
import { useCompile } from '../schema-component';
export const CurrentUserContext = createContext(null); export const CurrentUserContext = createContext(null);
@ -9,6 +11,23 @@ export const useCurrentUserContext = () => {
return useContext(CurrentUserContext); 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) => { export const CurrentUserProvider = (props) => {
const location = useLocation(); const location = useLocation();
const result = useRequest({ const result = useRequest({

View File

@ -1,29 +1,8 @@
import { MenuProps, Select } from 'antd'; import { MenuProps, Select } from 'antd';
import React, { useMemo } from 'react'; import React, { useMemo } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useACLRoleContext } from '../acl';
import { useAPIClient } from '../api-client'; import { useAPIClient } from '../api-client';
import { useCompile } from '../schema-component'; import { useCurrentRoles } from './CurrentUserProvider';
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);
};
export const useSwitchRole = () => { export const useSwitchRole = () => {
const api = useAPIClient(); const api = useAPIClient();

View File

@ -85,16 +85,5 @@ export default {
name: 'snippets', name: 'snippets',
defaultValue: ['!ui.*', '!pm', '!pm.*'], defaultValue: ['!ui.*', '!pm', '!pm.*'],
}, },
{
type: 'belongsToMany',
name: 'users',
target: 'users',
foreignKey: 'roleName',
otherKey: 'userId',
onDelete: 'CASCADE',
sourceKey: 'name',
targetKey: 'id',
through: 'rolesUsers',
},
], ],
} as CollectionOptions; } as CollectionOptions;

View File

@ -1,13 +1,17 @@
import { RecursionField } from '@formily/react'; import { RecursionField } from '@formily/react';
import { CollectionManagerProvider } from '@nocobase/client'; import { CollectionManagerProvider, SchemaComponentOptions, useCurrentRoles } from '@nocobase/client';
import React from 'react'; import React from 'react';
import { apiKeysCollection } from '../../collections'; import { apiKeysCollection } from '../../collections';
import { configurationSchema } from './schema'; import { configurationSchema } from './schema';
export const Configuration = () => { export const Configuration = () => {
const currentRoles = useCurrentRoles();
return ( return (
<CollectionManagerProvider collections={[apiKeysCollection]}> <CollectionManagerProvider collections={[apiKeysCollection]}>
<RecursionField schema={configurationSchema} /> <SchemaComponentOptions scope={{ currentRoles }}>
<RecursionField schema={configurationSchema} />
</SchemaComponentOptions>
</CollectionManagerProvider> </CollectionManagerProvider>
); );
}; };

View File

@ -0,0 +1,9 @@
import { useCurrentRoles } from '@nocobase/client';
export const useCurrentRolesProps = () => {
const options = useCurrentRoles();
return {
options,
};
};

View File

@ -39,23 +39,14 @@ export default {
uiSchema: { uiSchema: {
type: 'object', type: 'object',
title: '{{t("Roles")}}', title: '{{t("Roles")}}',
'x-component': 'AssociationField', 'x-component': 'Select',
'x-component-props': { 'x-component-props': {
fieldNames: { fieldNames: {
label: 'title', label: 'title',
value: 'name', value: 'name',
}, },
service: { objectValue: true,
params: { options: '{{ currentRoles }}',
filter: {
$and: [
{
users: { id: { $eq: '{{$user.id}}' } },
},
],
},
},
},
}, },
}, },
}, },