fix(plugin-auth): support to trigger workflow when change password (#6248)

* fix(plugin-auth): support to trigger workflow when change password

* fix(plugin-auth): fix await
This commit is contained in:
Junyi 2025-02-19 23:02:01 +08:00 committed by GitHub
parent e827c5823f
commit 43e71edf69
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 4 deletions

View File

@ -49,7 +49,8 @@ export default {
} else { } else {
key = 'email'; key = 'email';
} }
const user = await ctx.db.getRepository('users').findOne({ const UserRepo = ctx.db.getRepository('users');
const user = await UserRepo.findOne({
where: { where: {
[key]: currentUser[key], [key]: currentUser[key],
}, },
@ -59,8 +60,12 @@ export default {
if (!isValid) { if (!isValid) {
ctx.throw(401, ctx.t('The password is incorrect, please re-enter', { ns: namespace })); ctx.throw(401, ctx.t('The password is incorrect, please re-enter', { ns: namespace }));
} }
user.password = newPassword; await UserRepo.update({
await user.save(); filterByTk: user.id,
values: {
password: newPassword,
},
});
ctx.body = currentUser; ctx.body = currentUser;
await next(); await next();
}, },

View File

@ -28,7 +28,7 @@ describe('workflow > triggers > collection', () => {
beforeEach(async () => { beforeEach(async () => {
app = await getApp({ app = await getApp({
plugins: ['error-handler', 'data-source-main', 'users', 'auth'], plugins: ['error-handler', 'data-source-main', 'users', 'auth', 'system-settings'],
}); });
db = app.db; db = app.db;
@ -316,6 +316,32 @@ describe('workflow > triggers > collection', () => {
const executions = await workflow.getExecutions(); const executions = await workflow.getExecutions();
expect(executions.length).toBe(0); expect(executions.length).toBe(0);
}); });
it('password changed in users', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
sync: true,
type: 'collection',
config: {
mode: 2,
collection: 'users',
changed: ['passwordChangeTz'],
},
});
const res = await (await app.agent().login(1)).resource('auth').changePassword({
values: {
oldPassword: 'admin123',
newPassword: 'abc123',
confirmPassword: 'abc123',
},
});
expect(res.status).toBe(200);
const executions = await workflow.getExecutions();
expect(executions.length).toBe(1);
});
}); });
describe('config.condition', () => { describe('config.condition', () => {