refs #5036 hasGrabUser out of function

This commit is contained in:
Alexandre Riera 2023-03-07 14:11:03 +01:00
parent 21c7b18de9
commit b00a481016
1 changed files with 11 additions and 14 deletions

View File

@ -316,6 +316,8 @@ class VnMySQL extends MySQL {
} }
async invokeMethodP(method, args, model, ctx, opts) { async invokeMethodP(method, args, model, ctx, opts) {
const Model = this.getModelDefinition(model).model;
const settings = Model.definition.settings;
let tx; let tx;
if (!opts.transaction) { if (!opts.transaction) {
tx = await Transaction.begin(this, {}); tx = await Transaction.begin(this, {});
@ -325,8 +327,10 @@ class VnMySQL extends MySQL {
try { try {
// Fetch old values (update|delete) or login // Fetch old values (update|delete) or login
let where, id, data, idName, limit, op, oldInstances, newInstances; let where, id, data, idName, limit, op, oldInstances, newInstances;
const hasGrabUser = await this.grabUserLog(model, opts, 'login'); const hasGrabUser = settings.log && settings.log.grabUser;
if (!hasGrabUser) { if(hasGrabUser)
await this.grabUserLog(Model, opts, 'login');
else {
where = ctx.where; where = ctx.where;
id = ctx.id; id = ctx.id;
data = ctx.data; data = ctx.data;
@ -358,7 +362,9 @@ class VnMySQL extends MySQL {
super[method].apply(this, fnArgs); super[method].apply(this, fnArgs);
}); });
if (!hasGrabUser) { if(hasGrabUser)
await this.grabUserLog(Model, opts, 'logout');
else {
// Fetch new values // Fetch new values
const ids = []; const ids = [];
@ -386,7 +392,6 @@ class VnMySQL extends MySQL {
} }
} }
await this.grabUserLog(model, opts, 'logout');
await this.createLogRecord(oldInstances, newInstances, model, opts); await this.createLogRecord(oldInstances, newInstances, model, opts);
} }
if (tx) await tx.commit(); if (tx) await tx.commit();
@ -397,23 +402,15 @@ class VnMySQL extends MySQL {
} }
} }
async grabUserLog(model, opts, action) { async grabUserLog(Model, opts, action) {
const Model = this.getModelDefinition(model).model;
const settings = Model.definition.settings;
if (!(settings.log && settings.log.grabUser))
return false;
if(action == 'login'){ if(action == 'login'){
const userId = opts.httpCtx && opts.httpCtx.active.accessToken.userId; const userId = opts.httpCtx && opts.httpCtx.active.accessToken.userId;
const user = await Model.app.models.Account.findById(userId, {fields: ['name']}, opts); const user = await Model.app.models.Account.findById(userId, {fields: ['name']}, opts);
await this.executeP(`CALL account.myUser_loginWithName(?)`, [user.name], opts); await this.executeP(`CALL account.myUser_loginWithName(?)`, [user.name], opts);
} }
else if(action == 'logout'){ else if(action == 'logout')
await this.executeP(`CALL account.myUser_logout()`, null, opts); await this.executeP(`CALL account.myUser_logout()`, null, opts);
}
return true;
} }
buildSelectStmt(op, data, idName, model, where, limit) { buildSelectStmt(op, data, idName, model, where, limit) {