Merge branch 'dev' of https://gitea.verdnatura.es/verdnatura/salix into 6274-loginWorkerTimeControl
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Jorge Penadés 2023-11-30 08:44:41 +01:00
commit 5442e3b4e0
1 changed files with 34 additions and 12 deletions

View File

@ -26,24 +26,46 @@ module.exports = Self => {
}); });
Self.sync = async function(userName, password, force, options) { Self.sync = async function(userName, password, force, options) {
const models = Self.app.models;
const myOptions = {}; const myOptions = {};
let tx;
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
const models = Self.app.models; if (!myOptions.transaction) {
const user = await models.VnUser.findOne({ tx = await Self.beginTransaction({});
fields: ['id', 'password'], myOptions.transaction = tx;
where: {name: userName} };
}, myOptions);
if (user && password && !await user.hasPassword(password)) try {
throw new ForbiddenError('Wrong password'); const user = await models.VnUser.findOne({
fields: ['id', 'password'],
where: {name: userName}
}, myOptions);
const isSync = !await models.UserSync.exists(userName, myOptions); if (user && password && !await user.hasPassword(password))
throw new ForbiddenError('Wrong password');
if (!force && isSync && user) return; const isSync = !await models.UserSync.exists(userName, myOptions);
await models.AccountConfig.syncUser(userName, password);
await models.UserSync.destroyById(userName, myOptions); if (!force && isSync && user) {
if (tx) await tx.rollback();
return;
}
await Self.rawSql(`
SELECT id
FROM account.user
WHERE id = ?
FOR UPDATE`, [user.id], myOptions);
await models.AccountConfig.syncUser(userName, password);
await models.UserSync.destroyById(userName, myOptions);
if (tx) await tx.commit();
} catch (err) {
if (tx) await tx.rollback();
throw err;
}
}; };
}; };