From dc25bd7de72aa574767876341e5792733c2ee0e0 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Sun, 1 Dec 2019 15:03:55 +0000 Subject: [server/log] Add logging to all modules Logging with snapshots: Client: create / edit / delete / added to group / removed from group Group: create / edit / delete / added to group / removed from group Logging without snapshot: Wake-on-lan: wakup Ipxe-Builder: build / clear / cancel / script save IP-Ranges: create / edit / delete Logging: with info in description: User: create / edit / delete / grant role / revoke role Event: create / edit / delete Permission-Manager-Role: create / edit / delete Registration-Hook: create / delete / edit / change order Ipxe Configuration: create / delete / edit Backend: create / edit / delete --- server/api/registration.js | 73 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 7 deletions(-) (limited to 'server/api/registration.js') diff --git a/server/api/registration.js b/server/api/registration.js index 202d836..5744369 100644 --- a/server/api/registration.js +++ b/server/api/registration.js @@ -11,6 +11,7 @@ const config = require(path.join(__appdir, 'config', 'config')) // Ipxe needs the url without the port because ipxe can't handle port requests const url = config.https.host // + ':' + config.https.port const log = require(path.join(__appdir, 'lib', 'log')) +const HttpResponse = require(path.join(__appdir, 'lib', 'httpresponse')) // GET requests. @@ -36,11 +37,19 @@ router.postAsync('/hookorder', async (req, res) => { idSortvalueMap[id] = index }) var hooks = await db.registrationhook.findAll() + const oldOrder = hooks.sort((a, b) => (a.sortvalue > b.sortvalue)) var promises = [] hooks.forEach(hook => { promises.push(hook.update({ sortvalue: idSortvalueMap[hook.id] })) }) await Promise.all(promises) + log({ + category: 'REGISTATIONHOOK_EDIT_ORDER', + description: 'Registration hook order successfully edited.\n' + + 'Old-Order: ' + '\n\t' + oldOrder.map(x => { return '[' + x.id + '] ' + x.name }).toString().replace(/,/g, '\n\t') + '\n' + + 'New-Order: ' + '\n\t' + req.body.ids.map(x => { return '[' + x + '] ' + oldOrder.filter(y => y.id === x)[0].name }).toString().replace(/,/g, '\n\t') + }) + res.end() }) @@ -54,11 +63,35 @@ router.postAsync(['/hooks', '/hooks/:id'], async (req, res) => { var hook = null if (req.params.id > 0) { hook = await db.registrationhook.findOne({ where: { id: req.params.id } }) - if (hook) await hook.update(item) + if (hook) { + await hook.update(item) + log({ + category: 'REGISTATIONHOOK_EDIT', + description: '[' + hook.id + '] ' + hook.name + ': Registration hook successfully edited.\n' + + 'ID: ' + hook.id + '\n' + + 'Name: ' + hook.name + '\n' + + 'Type: ' + hook.type + '\n' + + 'Description: ' + hook.description + '\n' + + 'Sortvalue: ' + hook.sortvalue + '\n' + + 'Groups: ' + req.body.groups, + userId: req.user.id + }) + } } else { var maxSortvalue = await db.registrationhook.max('sortvalue') item.sortvalue = maxSortvalue ? maxSortvalue + 1 : 1 hook = await db.registrationhook.create(item) + log({ + category: 'REGISTATIONHOOK_CREATE', + description: '[' + hook.id + '] ' + hook.name + ': Registration hook successfully created.\n' + + 'ID: ' + hook.id + '\n' + + 'Name: ' + hook.name + '\n' + + 'Type: ' + hook.type + '\n' + + 'Description: ' + hook.description + '\n' + + 'Sortvalue: ' + hook.sortvalue + '\n' + + 'Groups: ' + req.body.groups, + userId: req.user.id + }) } if (hook) { hook.setGroups(req.body.groups) @@ -67,12 +100,31 @@ router.postAsync(['/hooks', '/hooks/:id'], async (req, res) => { res.end() }) -// DELETE requests. - -router.delete(['/hooks', '/hooks/:id'], (req, res) => { - db.registrationhook.destroy({ where: { id: req.params.id || req.body.ids } }).then(count => { res.send({ count }) }) +// ############################################################################ +// ########################## DELETE requests ############################### + +router.deleteAsync('/hooks/:id', async (req, res) => { + if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res) + const hook = await db.registrationhook.findOne({ where: { id: req.params.id } }) + const count = await db.registrationhook.destroy({ where: { id: req.params.id } }) + + if (count) { + log({ + category: 'REGISTATIONHOOK_DELETE', + description: 'Registration hook successfully deleted.\n' + + 'ID: ' + hook.id + '\n' + + 'Name: ' + hook.name + '\n' + + 'Type: ' + hook.type + '\n' + + 'Sortvalue: ' + hook.sortvalue, + userId: req.user.id + }) + HttpResponse.success('deleted', 'hook', req.params.id).send(res) + } else HttpResponse.notFound(req.params.id).send(res) }) +// ############################################################################ +// ############################################################################ + module.exports.router = router // GET requests. @@ -164,7 +216,10 @@ noAuthRouter.postAsync('/clients', async (req, res) => { // Client ip set successfully client.networks[0].ip = setIp.ip } else { - log({ category: 'ERROR_DHCP', description: `[${dhcp.backend.id}] Error setting ip ${network.ip} for mac ${network.mac}\nError: ${setIp.msg}` }) + log({ + category: 'ERROR_DHCP', + description: `[${dhcp.backend.id}] Error setting ip ${network.ip} for mac ${network.mac}\nError: ${setIp.msg}` + }) } } } @@ -227,7 +282,11 @@ noAuthRouter.postAsync('/clients', async (req, res) => { // Add groups to the client. if (client.parents.length === 0) client.parents = await ipHelper.getGroups(client.networks[0].ip) client.parents.forEach(pid => { newClient.addGroup(pid) }) - log({ category: 'REGISTRATION', description: 'Client added successfully.', clientId: newClient.id }) + log({ + category: 'REGISTRATION', + description: 'Client added successfully.', + clientId: newClient.id + }) // Add the client to the backends. const result = backendHelper.addClient(client) -- cgit v1.2.3-55-g7522