diff --git a/packages/client/src/user/SwitchRole.tsx b/packages/client/src/user/SwitchRole.tsx index a3548a217..86415ec97 100644 --- a/packages/client/src/user/SwitchRole.tsx +++ b/packages/client/src/user/SwitchRole.tsx @@ -2,23 +2,26 @@ import { useCookieState } from 'ahooks'; import { Menu, Select } from 'antd'; import React from 'react'; import { useTranslation } from 'react-i18next'; +import { useACLRoleContext } from '../acl'; import { useAPIClient } from '../api-client'; import { useCurrentUserContext } from './CurrentUserProvider'; const useCurrentRoles = () => { + const { allowAnonymous } = useACLRoleContext(); const { data } = useCurrentUserContext(); - return [ - ...(data?.data?.roles || []).map(item => { - return { - title: item.title, - name: item.name, - } - }), - { + const options = (data?.data?.roles || []).map((item) => { + return { + title: item.title, + name: item.name, + }; + }); + if (allowAnonymous) { + options.push({ title: 'Anonymous', name: 'anonymous', - }, - ]; + }); + } + return options; }; export const SwitchRole = () => { diff --git a/packages/plugin-acl/src/actions/role-check.ts b/packages/plugin-acl/src/actions/role-check.ts index 927d6009b..e188ea7b1 100644 --- a/packages/plugin-acl/src/actions/role-check.ts +++ b/packages/plugin-acl/src/actions/role-check.ts @@ -16,6 +16,12 @@ export async function checkAction(ctx, next) { appends: ['menuUiSchemas'], }); + const anonymous = await ctx.db.getRepository('roles').findOne({ + filter: { + name: 'anonymous', + }, + }); + const role = ctx.app.acl.getRole(currentRole); ctx.body = { @@ -25,6 +31,7 @@ export async function checkAction(ctx, next) { allowAll: currentRole === 'root', allowConfigure: roleInstance.get('allowConfigure'), allowMenuItemIds: roleInstance.get('menuUiSchemas').map((uiSchema) => uiSchema.get('x-uid')), + allowAnonymous: !!anonymous, }; } diff --git a/packages/plugin-acl/src/collections/roles.ts b/packages/plugin-acl/src/collections/roles.ts index dd72107c2..289e7fb1b 100644 --- a/packages/plugin-acl/src/collections/roles.ts +++ b/packages/plugin-acl/src/collections/roles.ts @@ -7,6 +7,7 @@ export default { model: 'RoleModel', filterTargetKey: 'name', // targetKey: 'name', + sortable: true, fields: [ { type: 'uid',