From 82cf9511663110bccafb19d41d309cb13a428086 Mon Sep 17 00:00:00 2001 From: chenos Date: Thu, 28 Jul 2022 20:21:31 +0800 Subject: [PATCH] fix: append roles to current user (#695) --- packages/core/resourcer/src/middleware.ts | 4 --- .../acl/src/middlewares/appendRolesToUser.ts | 5 ---- .../acl/src/middlewares/setCurrentRole.ts | 29 +++++++++---------- packages/plugins/acl/src/server.ts | 2 -- 4 files changed, 13 insertions(+), 27 deletions(-) delete mode 100644 packages/plugins/acl/src/middlewares/appendRolesToUser.ts diff --git a/packages/core/resourcer/src/middleware.ts b/packages/core/resourcer/src/middleware.ts index 5d9f6d90b..56281978c 100644 --- a/packages/core/resourcer/src/middleware.ts +++ b/packages/core/resourcer/src/middleware.ts @@ -82,10 +82,6 @@ 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 deleted file mode 100644 index de625866e..000000000 --- a/packages/plugins/acl/src/middlewares/appendRolesToUser.ts +++ /dev/null @@ -1,5 +0,0 @@ -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/middlewares/setCurrentRole.ts b/packages/plugins/acl/src/middlewares/setCurrentRole.ts index 3be755be0..54e153f7a 100644 --- a/packages/plugins/acl/src/middlewares/setCurrentRole.ts +++ b/packages/plugins/acl/src/middlewares/setCurrentRole.ts @@ -1,8 +1,4 @@ export async function setCurrentRole(ctx, next) { - if (!ctx.state.currentUser) { - return next(); - } - let currentRole = ctx.get('X-Role'); if (currentRole === 'anonymous') { @@ -10,20 +6,21 @@ export async function setCurrentRole(ctx, next) { return next(); } - const RolesUsers = ctx.db.getCollection('rolesUsers').model; - const userRoles = await RolesUsers.findAll({ - where: { - userId: ctx.state.currentUser.id - } - }); + if (!ctx.state.currentUser) { + return next(); + } - if (userRoles.length == 1) { - currentRole = userRoles[0].roleName; - } else if (userRoles.length > 1) { - const role = userRoles.find((item) => item.roleName === currentRole); + const repository = ctx.db.getRepository('users.roles', ctx.state.currentUser.id); + const roles = await repository.find(); + ctx.state.currentUser.setDataValue('roles', roles); + + if (roles.length == 1) { + currentRole = roles[0].name; + } else if (roles.length > 1) { + const role = roles.find((item) => item.name === currentRole); if (!role) { - const defaultRole = userRoles.find((item) => item.default); - currentRole = (defaultRole || userRoles[0])?.roleName; + const defaultRole = roles.find((item) => item?.rolesUsers?.default); + currentRole = (defaultRole || roles[0])?.name; } } diff --git a/packages/plugins/acl/src/server.ts b/packages/plugins/acl/src/server.ts index 7f82d4ad6..f6e20cacf 100644 --- a/packages/plugins/acl/src/server.ts +++ b/packages/plugins/acl/src/server.ts @@ -7,7 +7,6 @@ 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'; @@ -323,7 +322,6 @@ 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');