fix: append roles to current user (#695)
This commit is contained in:
parent
7d4796e7e0
commit
82cf951166
@ -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);
|
||||
}
|
||||
|
@ -1,5 +0,0 @@
|
||||
export async function appendRolesToUser(ctx, next) {
|
||||
ctx.state.currentUserAppends = ctx.state.currentUserAppends || [];
|
||||
ctx.state.currentUserAppends.push('roles');
|
||||
await next();
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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');
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user