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 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[];
|
export type MiddlewareType = string | string[] | HandlerType | HandlerType[] | MiddlewareOptions | MiddlewareOptions[];
|
||||||
|
|
||||||
@ -82,6 +82,10 @@ export class MiddlewareManager {
|
|||||||
return (ctx, next) => compose(this.middlewares)(ctx, next);
|
return (ctx, next) => compose(this.middlewares)(ctx, next);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unshift(middleware: HandlerType) {
|
||||||
|
this.middlewares.unshift(middleware);
|
||||||
|
}
|
||||||
|
|
||||||
use(middleware: HandlerType) {
|
use(middleware: HandlerType) {
|
||||||
this.middlewares.push(middleware);
|
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 { checkAction } from './actions/role-check';
|
||||||
import { roleCollectionsResource } from './actions/role-collections';
|
import { roleCollectionsResource } from './actions/role-collections';
|
||||||
import { setDefaultRole } from './actions/user-setDefaultRole';
|
import { setDefaultRole } from './actions/user-setDefaultRole';
|
||||||
|
import { appendRolesToUser } from './middlewares/appendRolesToUser';
|
||||||
import { setCurrentRole } from './middlewares/setCurrentRole';
|
import { setCurrentRole } from './middlewares/setCurrentRole';
|
||||||
import { RoleModel } from './model/RoleModel';
|
import { RoleModel } from './model/RoleModel';
|
||||||
import { RoleResourceActionModel } from './model/RoleResourceActionModel';
|
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;
|
const usersPlugin = this.app.pm.get('@nocobase/plugin-users') as UsersPlugin;
|
||||||
usersPlugin.tokenMiddleware.use(setCurrentRole);
|
usersPlugin.tokenMiddleware.use(setCurrentRole);
|
||||||
|
usersPlugin.tokenMiddleware.unshift(appendRolesToUser);
|
||||||
|
|
||||||
this.app.acl.allow('users', 'setDefaultRole', 'loggedIn');
|
this.app.acl.allow('users', 'setDefaultRole', 'loggedIn');
|
||||||
|
|
||||||
|
@ -19,18 +19,17 @@ async function findUserByToken(ctx: Context, plugin: UsersPlugin) {
|
|||||||
if (!token) {
|
if (!token) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { userId } = await plugin.jwtService.decode(token);
|
const { userId } = await plugin.jwtService.decode(token);
|
||||||
const collection = ctx.db.getCollection('users');
|
const collection = ctx.db.getCollection('users');
|
||||||
const appends = [];
|
ctx.state.currentUserAppends = ctx.state.currentUserAppends || [];
|
||||||
for (const [, field] of collection.fields) {
|
for (const [, field] of collection.fields) {
|
||||||
if (field.type === 'belongsTo') {
|
if (field.type === 'belongsTo') {
|
||||||
appends.push(field.name);
|
ctx.state.currentUserAppends.push(field.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return await ctx.db.getRepository('users').findOne({
|
return await ctx.db.getRepository('users').findOne({
|
||||||
appends,
|
appends: ctx.state.currentUserAppends,
|
||||||
filter: {
|
filter: {
|
||||||
id: userId,
|
id: userId,
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user