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