diff --git a/packages/core/resourcer/src/middleware.ts b/packages/core/resourcer/src/middleware.ts index 957b05121..5d9f6d90b 100644 --- a/packages/core/resourcer/src/middleware.ts +++ b/packages/core/resourcer/src/middleware.ts @@ -1,7 +1,7 @@ -import { ActionName } from './action'; -import { requireModule } from './utils'; -import { HandlerType } from './resourcer'; import compose from 'koa-compose'; +import { ActionName } from './action'; +import { HandlerType } from './resourcer'; +import { requireModule } from './utils'; export type MiddlewareType = string | string[] | HandlerType | HandlerType[] | MiddlewareOptions | MiddlewareOptions[]; @@ -82,6 +82,10 @@ export class MiddlewareManager { return (ctx, next) => compose(this.middlewares)(ctx, next); } + unshift(middleware: HandlerType) { + this.middlewares.unshift(middleware); + } + use(middleware: HandlerType) { this.middlewares.push(middleware); } diff --git a/packages/plugins/acl/src/middlewares/appendRolesToUser.ts b/packages/plugins/acl/src/middlewares/appendRolesToUser.ts new file mode 100644 index 000000000..de625866e --- /dev/null +++ b/packages/plugins/acl/src/middlewares/appendRolesToUser.ts @@ -0,0 +1,5 @@ +export async function appendRolesToUser(ctx, next) { + ctx.state.currentUserAppends = ctx.state.currentUserAppends || []; + ctx.state.currentUserAppends.push('roles'); + await next(); +} diff --git a/packages/plugins/acl/src/server.ts b/packages/plugins/acl/src/server.ts index f6e20cacf..7f82d4ad6 100644 --- a/packages/plugins/acl/src/server.ts +++ b/packages/plugins/acl/src/server.ts @@ -7,6 +7,7 @@ import { availableActionResource } from './actions/available-actions'; import { checkAction } from './actions/role-check'; import { roleCollectionsResource } from './actions/role-collections'; import { setDefaultRole } from './actions/user-setDefaultRole'; +import { appendRolesToUser } from './middlewares/appendRolesToUser'; import { setCurrentRole } from './middlewares/setCurrentRole'; import { RoleModel } from './model/RoleModel'; import { RoleResourceActionModel } from './model/RoleResourceActionModel'; @@ -322,6 +323,7 @@ export class PluginACL extends Plugin { const usersPlugin = this.app.pm.get('@nocobase/plugin-users') as UsersPlugin; usersPlugin.tokenMiddleware.use(setCurrentRole); + usersPlugin.tokenMiddleware.unshift(appendRolesToUser); this.app.acl.allow('users', 'setDefaultRole', 'loggedIn'); diff --git a/packages/plugins/users/src/middlewares/parseToken.ts b/packages/plugins/users/src/middlewares/parseToken.ts index 23e0db0fc..8c55f6429 100644 --- a/packages/plugins/users/src/middlewares/parseToken.ts +++ b/packages/plugins/users/src/middlewares/parseToken.ts @@ -19,18 +19,17 @@ async function findUserByToken(ctx: Context, plugin: UsersPlugin) { if (!token) { return null; } - try { const { userId } = await plugin.jwtService.decode(token); const collection = ctx.db.getCollection('users'); - const appends = []; + ctx.state.currentUserAppends = ctx.state.currentUserAppends || []; for (const [, field] of collection.fields) { if (field.type === 'belongsTo') { - appends.push(field.name); + ctx.state.currentUserAppends.push(field.name); } } return await ctx.db.getRepository('users').findOne({ - appends, + appends: ctx.state.currentUserAppends, filter: { id: userId, },