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/ipxeconfigs.js | 135 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 130 insertions(+), 5 deletions(-) (limited to 'server/api/ipxeconfigs.js') diff --git a/server/api/ipxeconfigs.js b/server/api/ipxeconfigs.js index 64f2c37..3c6f6eb 100644 --- a/server/api/ipxeconfigs.js +++ b/server/api/ipxeconfigs.js @@ -6,6 +6,7 @@ var express = require('express') const { decorateApp } = require('@awaitjs/express') var router = decorateApp(express.Router()) const HttpResponse = require(path.join(__appdir, 'lib', 'httpresponse')) +const log = require(path.join(__appdir, 'lib', 'log')) // ############################################################################ // ########################### GET requests ################################# @@ -62,18 +63,96 @@ router.getAsync('/:id/clients', async (req, res) => { router.postAsync(['', '/:id'], async (req, res) => { if (req.query.delete !== undefined && req.query.delete !== 'false') { if (!Array.isArray(req.body.ids)) return HttpResponse.invalidBodyValue('ids', 'an array').send(res) - const count = await db.config.destroy({ where: { id: req.body.ids } }) - HttpResponse.successBatch('deleted', 'ipxe config', count).send(res) + + const user = await db.user.findOne({ where: { id: req.user.id } }) + // Only need to log batch request if there is more than one ipxeconfig to delete. + if (req.body.ids.length > 1) { + await log({ + category: 'IPXECONFIG_BATCH_DELETE', + description: 'Batch deletion of ' + req.body.ids.length + ' ipxe configs initiated by user.', + user, + userId: req.user.id + }) + } + + let deletionCounter = 0 + // Delete every config on its own, to get a better log + for (let index in req.body.ids) { + const config = await db.config.findOne({ where: { id: req.body.ids[index] } }) + const count = await db.config.destroy({ where: { id: req.body.ids[index] } }) + if (count !== 1) { + await log({ + category: 'ERROR_IPXECONFIG_DELETE', + description: '[' + config.id + '] ' + config.name + ': Ipxe config could not be deleted.\n' + + 'ID: ' + config.id + '\n' + + 'Name: ' + config.name + '\n' + + 'Description: ' + config.description + '\n' + + 'Default Entry: ' + config.defaultEntry + '\n' + + 'Timeout: ' + config.timeout + '\n' + + 'Default Config: ' + config.isDefault, + user, + userId: req.user.id + }) + } else { + await log({ + category: 'IPXECONFIG_DELETE', + description: '[' + config.id + '] ' + config.name + ': Ipxe config successfully deleted.\n' + + 'ID: ' + config.id + '\n' + + 'Name: ' + config.name + '\n' + + 'Description: ' + config.description + '\n' + + 'Default Entry: ' + config.defaultEntry + '\n' + + 'Timeout: ' + config.timeout + '\n' + + 'Default Config: ' + config.isDefault, + user, + userId: req.user.id + }) + deletionCounter++ + } + } + if (req.body.ids.length > 1) { + log({ + category: 'IPXECONFIG_BATCH_DELETE', + description: deletionCounter + '/' + req.body.ids.length + ' ipxe configs successfully deleted.', + user, + userId: req.user.id + }) + } + + HttpResponse.successBatch('deleted', 'ipxe config', deletionCounter).send(res) } else { let config let action = 'updated' if (req.params.id === undefined) { config = await db.config.create(req.body.data) + log({ + category: 'IPXECONFIG_CREATE', + description: '[' + config.id + '] ' + config.name + ': Ipxe config successfully created.\n' + + 'ID: ' + config.id + '\n' + + 'Name: ' + config.name + '\n' + + 'Description: ' + config.description + '\n' + + 'Default Entry: ' + config.defaultEntry + '\n' + + 'Timeout: ' + config.timeout + '\n' + + 'Default Config: ' + config.isDefault, + userId: req.user.id + }) action = 'created' } else if (req.params.id > 0) { config = await db.config.findOne({ where: { id: req.params.id } }) if (!config) return HttpResponse.notFound(req.params.id).send(res) - else await config.update(req.body.data) + else { + await config.update(req.body.data) + log({ + category: 'IPXECONFIG_EDIT', + description: '[' + config.id + '] ' + config.name + ': Ipxe config successfully edited.\n' + + 'ID: ' + config.id + '\n' + + 'Name: ' + config.name + '\n' + + 'Description: ' + config.description + '\n' + + 'Default Entry: ' + config.defaultEntry + '\n' + + 'Timeout: ' + config.timeout + '\n' + + 'Default Config: ' + config.isDefault, + userId: req.user.id + }) + } } else { return HttpResponse.invalidId().send(res) } @@ -99,6 +178,18 @@ router.putAsync('/:id/groups', async (req, res) => { if (config) { await config.setGroups(req.body.ids) HttpResponse.success('set', 'clients of ipxe config', config.id).send(res) + log({ + category: 'IPXECONFIG_SET_GROUPS', + description: '[' + config.id + '] ' + config.name + ': ' + req.body.ids.length + ' groups successfully set.\n' + + 'ID: ' + config.id + '\n' + + 'Name: ' + config.name + '\n' + + 'Description: ' + config.description + '\n' + + 'Default Entry: ' + config.defaultEntry + '\n' + + 'Timeout: ' + config.timeout + '\n' + + 'Default Config: ' + config.isDefault + '\n' + + 'Groups: ' + req.body.ids, + userId: req.user.id + }) } else { HttpResponse.notFound(req.params.id).send(res) } @@ -110,6 +201,18 @@ router.putAsync('/:id/clients', async (req, res) => { const config = await db.config.findOne({ where: { id: req.params.id } }) if (config) { await config.setClients(req.body.ids) + log({ + category: 'IPXECONFIG_SET_CLIENTS', + description: '[' + config.id + '] ' + config.name + ': ' + req.body.ids.length + ' clients successfully set.\n' + + 'ID: ' + config.id + '\n' + + 'Name: ' + config.name + '\n' + + 'Description: ' + config.description + '\n' + + 'Default Entry: ' + config.defaultEntry + '\n' + + 'Timeout: ' + config.timeout + '\n' + + 'Default Config: ' + config.isDefault + '\n' + + 'Clients: ' + req.body.ids, + userId: req.user.id + }) HttpResponse.success('set', 'clients of ipxe config', config.id).send(res) } else { HttpResponse.notFound(req.params.id).send(res) @@ -119,9 +222,17 @@ router.putAsync('/:id/clients', async (req, res) => { router.putAsync('/:id/default', async (req, res) => { if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res) const config = await db.config.findOne({ where: { id: req.params.id } }) + const oldDefault = await db.config.findOne({ where: { isDefault: true } }) if (config) { await db.config.update({ isDefault: false }, { where: { isDefault: true } }) await config.update({ isDefault: true }) + log({ + category: 'IPXECONFIG_SET_DEFAULT', + description: '[' + config.id + '] ' + config.name + ': Config successfully set as default.\n' + + 'Old Default: [' + oldDefault.id + '] ' + oldDefault.name + '\n' + + 'New Default: [' + config.id + '] ' + config.name, + userId: req.user.id + }) HttpResponse.success('set as default:', 'config', config.id).send(res) } else { HttpResponse.notFound(req.params.id).send(res) @@ -133,9 +244,23 @@ router.putAsync('/:id/default', async (req, res) => { router.deleteAsync('/:id', async (req, res) => { if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res) + const config = await db.config.findOne({ where: { id: req.params.id } }) const count = await db.config.destroy({ where: { id: req.params.id } }) - if (count) res.status(200).end() - else HttpResponse.notFound(req.params.id).send(res) + + if (count) { + log({ + category: 'IPXECONFIG_DELETE', + description: '[' + config.id + '] ' + config.name + ': Ipxe config successfully deleted.\n' + + 'ID: ' + config.id + '\n' + + 'Name: ' + config.name + '\n' + + 'Description: ' + config.description + '\n' + + 'Default Entry: ' + config.defaultEntry + '\n' + + 'Timeout: ' + config.timeout + '\n' + + 'Default Config: ' + config.isDefault, + userId: req.user.id + }) + HttpResponse.success('deleted', 'config', req.params.id).send(res) + } else HttpResponse.notFound(req.params.id).send(res) }) // ############################################################################ -- cgit v1.2.3-55-g7522