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);
|
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);
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
export async function setCurrentRole(ctx, next) {
|
||||||
if (!ctx.state.currentUser) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
|
|
||||||
let currentRole = ctx.get('X-Role');
|
let currentRole = ctx.get('X-Role');
|
||||||
|
|
||||||
if (currentRole === 'anonymous') {
|
if (currentRole === 'anonymous') {
|
||||||
@ -10,20 +6,21 @@ export async function setCurrentRole(ctx, next) {
|
|||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
const RolesUsers = ctx.db.getCollection('rolesUsers').model;
|
if (!ctx.state.currentUser) {
|
||||||
const userRoles = await RolesUsers.findAll({
|
return next();
|
||||||
where: {
|
|
||||||
userId: ctx.state.currentUser.id
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
if (userRoles.length == 1) {
|
const repository = ctx.db.getRepository('users.roles', ctx.state.currentUser.id);
|
||||||
currentRole = userRoles[0].roleName;
|
const roles = await repository.find();
|
||||||
} else if (userRoles.length > 1) {
|
ctx.state.currentUser.setDataValue('roles', roles);
|
||||||
const role = userRoles.find((item) => item.roleName === currentRole);
|
|
||||||
|
if (roles.length == 1) {
|
||||||
|
currentRole = roles[0].name;
|
||||||
|
} else if (roles.length > 1) {
|
||||||
|
const role = roles.find((item) => item.name === currentRole);
|
||||||
if (!role) {
|
if (!role) {
|
||||||
const defaultRole = userRoles.find((item) => item.default);
|
const defaultRole = roles.find((item) => item?.rolesUsers?.default);
|
||||||
currentRole = (defaultRole || userRoles[0])?.roleName;
|
currentRole = (defaultRole || roles[0])?.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,6 @@ 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';
|
||||||
@ -323,7 +322,6 @@ 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');
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user