From 86cd5f52375188286ce8d5ab8bdaeb8e3a2a656d Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 2 Jun 2023 15:07:02 +0200 Subject: [PATCH] refs #5472 fix sync --- back/models/vn-user.js | 25 ++++++++++--------- .../back/methods/account/sync-by-id.js | 6 ++--- modules/account/back/methods/account/sync.js | 8 +++--- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/back/models/vn-user.js b/back/models/vn-user.js index 3b869907e..fb3279353 100644 --- a/back/models/vn-user.js +++ b/back/models/vn-user.js @@ -1,7 +1,6 @@ const vnModel = require('vn-loopback/common/models/vn-model'); const LoopBackContext = require('loopback-context'); const {Email} = require('vn-print'); -const UserError = require('vn-loopback/util/user-error'); module.exports = function(Self) { vnModel(Self); @@ -109,10 +108,12 @@ module.exports = function(Self) { return email.send(); }); - const _setPassword = Self.setPassword; - Self.setPassword = async function(id, newPassword, options) { - if (typeof options === 'function') + const _setPassword = Self.prototype.setPassword; + Self.prototype.setPassword = async function(newPassword, options, cb) { + if (cb === undefined && typeof options === 'function') { + cb = options; options = undefined; + } const myOptions = {}; let tx; @@ -127,15 +128,15 @@ module.exports = function(Self) { options = myOptions; try { - await Self.rawSql(`CALL account.user_setPassword(?, ?)`, [id, newPassword], options); - await _setPassword.call(this, id, newPassword, options); - const user = await Self.findById(id, {fields: ['id', 'name']}, options); - await user.updateAttribute('passExpired', null, options); - await models.Account.sync(user.name, newPassword); - if (tx) await tx.commit(); + await Self.rawSql(`CALL account.user_checkPassword(?)`, [newPassword], options); + await _setPassword.call(this, newPassword, options); + await this.updateAttribute('passExpired', null, options); + await Self.app.models.Account.sync(this.name, newPassword, null, options); + tx && await tx.commit(); + cb && cb(); } catch (err) { - if (tx) await tx.rollback(); - throw err; + tx && await tx.rollback(); + if (cb) cb(err); else throw err; } }; diff --git a/modules/account/back/methods/account/sync-by-id.js b/modules/account/back/methods/account/sync-by-id.js index 538bc09bd..e1c631395 100644 --- a/modules/account/back/methods/account/sync-by-id.js +++ b/modules/account/back/methods/account/sync-by-id.js @@ -24,8 +24,8 @@ module.exports = Self => { } }); - Self.syncById = async function(id, password, force) { - let user = await Self.app.models.VnUser.findById(id, {fields: ['name']}); - await Self.sync(user.name, password, force); + Self.syncById = async function(id, password, force, options) { + let user = await Self.app.models.VnUser.findById(id, {fields: ['name']}, options); + await Self.sync(user.name, password, force, options); }; }; diff --git a/modules/account/back/methods/account/sync.js b/modules/account/back/methods/account/sync.js index 8668be343..95454a600 100644 --- a/modules/account/back/methods/account/sync.js +++ b/modules/account/back/methods/account/sync.js @@ -24,17 +24,17 @@ module.exports = Self => { } }); - Self.sync = async function(userName, password, force) { + Self.sync = async function(userName, password, force, options) { const models = Self.app.models; const user = await models.VnUser.findOne({ fields: ['id'], where: {name: userName} - }); - const isSync = !await models.UserSync.exists(userName); + }, options); + const isSync = !await models.UserSync.exists(userName, options); if (!force && isSync && user) return; await models.AccountConfig.syncUser(userName, password); - await models.UserSync.destroyById(userName); + await models.UserSync.destroyById(userName, options); }; };