* perf: add perf_hooks * perf: add cache * fix: test * feat: support bloom filter * feat: caching token black list * perf: caching i18n instance * fix: test * fix: test * chore: remove prePerfHooks on app * chore: improve i18n instances cache * chore: remove performance measure * fix: package.json * perf: optimize cache strategy * fix: test * fix: bug * test: storer of auth-manager * fix: afterDestroy hook when updating null value * fix: version * chore: fix bug and add test * fix: test * fix: test
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { Context } from '@nocobase/actions';
|
|
import { Cache } from '@nocobase/cache';
|
|
import { Model, Repository } from '@nocobase/database';
|
|
|
|
export async function setCurrentRole(ctx: Context, next) {
|
|
const currentRole = ctx.get('X-Role');
|
|
|
|
if (currentRole === 'anonymous') {
|
|
ctx.state.currentRole = currentRole;
|
|
return next();
|
|
}
|
|
|
|
if (!ctx.state.currentUser) {
|
|
return next();
|
|
}
|
|
|
|
const cache = ctx.cache as Cache;
|
|
const repository = ctx.db.getRepository('users.roles', ctx.state.currentUser.id) as unknown as Repository;
|
|
const roles = (await cache.wrap(`roles:${ctx.state.currentUser.id}`, () =>
|
|
repository.find({
|
|
raw: true,
|
|
}),
|
|
)) as Model[];
|
|
ctx.state.currentUser.roles = roles;
|
|
|
|
// 1. If the X-Role is set, use the specified role
|
|
if (currentRole) {
|
|
ctx.state.currentRole = roles.find((role) => role.name === currentRole)?.name;
|
|
}
|
|
// 2. If the X-Role is not set, use the default role
|
|
else {
|
|
const defaultRole = roles.find((item) => item?.rolesUsers?.default);
|
|
ctx.state.currentRole = (defaultRole || roles[0])?.name;
|
|
}
|
|
|
|
if (!ctx.state.currentRole) {
|
|
return ctx.throw(401, { code: 'ROLE_NOT_FOUND_ERR', message: 'The user role does not exist.' });
|
|
}
|
|
|
|
await next();
|
|
}
|