* refactor(plugin-users): improve extendibility of middlewares * fix(plugin-users): fix typo * fix: test error * fix: allowConfigure condition Co-authored-by: chenos <chenlinxh@gmail.com>
46 lines
848 B
TypeScript
46 lines
848 B
TypeScript
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();
|
|
}
|