tachybase_todo/packages/plugins/@nocobase/plugin-acl/src/server/actions/user-setDefaultRole.ts

48 lines
862 B
TypeScript
Raw Normal View History

import { Context, Next } from '@nocobase/actions';
export async function setDefaultRole(ctx: Context, next: Next) {
const {
values: { roleName },
} = ctx.action.params;
const {
db,
state: { currentUser },
action: {
params: { values },
},
} = ctx;
if (values.roleName == 'anonymous') {
return next();
}
const repository = db.getRepository('rolesUsers');
await db.sequelize.transaction(async (transaction) => {
await repository.update({
filter: {
userId: currentUser.get('id'),
},
values: {
default: false,
},
transaction,
});
await repository.update({
filter: {
userId: currentUser.get('id'),
roleName,
},
values: {
default: true,
},
transaction,
});
});
ctx.body = 'ok';
await next();
}