From 79b143478e0df7d34dd5090326ae07b77bce8a46 Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Mon, 8 Jun 2020 10:19:24 +0200 Subject: [PATCH 1/2] log web access --- back/models/account.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/back/models/account.js b/back/models/account.js index 64b59e704..28b426862 100644 --- a/back/models/account.js +++ b/back/models/account.js @@ -12,10 +12,26 @@ module.exports = Self => { message: `A client with that Web User name already exists` }); - Self.observe('before save', (ctx, next) => { + Self.observe('before save', async function(ctx, next) { if (ctx.currentInstance && ctx.currentInstance.id && ctx.data && ctx.data.password) ctx.data.password = md5(ctx.data.password); + if (ctx.data && (ctx.data.name || ctx.data.active)) { + let instance = JSON.parse(JSON.stringify(ctx.currentInstance)); + let userId = ctx.options.accessToken.userId; + console.log('ctx.data', ctx.data); + console.log('ctx.currentInstance', ctx.currentInstance); + let logRecord = { + originFk: ctx.currentInstance.id, + userFk: userId, + action: 'update', + changedModel: 'Account', + oldInstance: {name: instance.name, active: instance.active}, + newInstance: ctx.data + }; + await Self.app.models.ClientLog.create(logRecord); + } + next(); }); From 2a53d4bca5e7aa8f631497c54b4164c1f9ccffe3 Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Mon, 8 Jun 2020 12:18:33 +0200 Subject: [PATCH 2/2] create hook in account --- back/models/account.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/back/models/account.js b/back/models/account.js index 28b426862..7cc196852 100644 --- a/back/models/account.js +++ b/back/models/account.js @@ -12,15 +12,13 @@ module.exports = Self => { message: `A client with that Web User name already exists` }); - Self.observe('before save', async function(ctx, next) { + Self.observe('before save', async function(ctx) { if (ctx.currentInstance && ctx.currentInstance.id && ctx.data && ctx.data.password) ctx.data.password = md5(ctx.data.password); - if (ctx.data && (ctx.data.name || ctx.data.active)) { + if (!ctx.isNewInstance && ctx.data && (ctx.data.name || ctx.data.active)) { let instance = JSON.parse(JSON.stringify(ctx.currentInstance)); let userId = ctx.options.accessToken.userId; - console.log('ctx.data', ctx.data); - console.log('ctx.currentInstance', ctx.currentInstance); let logRecord = { originFk: ctx.currentInstance.id, userFk: userId, @@ -31,8 +29,6 @@ module.exports = Self => { }; await Self.app.models.ClientLog.create(logRecord); } - - next(); }); Self.remoteMethod('getCurrentUserData', {