From 0dcbc17552e4f99692ad9daa793b78909e497b32 Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Thu, 24 Oct 2019 14:40:41 +0000 Subject: [groups] Add first implementation of the new config path ui --- server/api/configloader.js | 18 ++++++++++++++++-- server/lib/confighelper.js | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 6 deletions(-) (limited to 'server') diff --git a/server/api/configloader.js b/server/api/configloader.js index 4059e5b..f6c4597 100644 --- a/server/api/configloader.js +++ b/server/api/configloader.js @@ -9,8 +9,21 @@ const url = config.https.host + ':' + config.https.port const configHelper = require(path.join(__appdir, 'lib', 'confighelper')) -// if client in db -> load script (default if none is found), else load registration script -noAuthRouter.getAsync('/test/:uuid', async (req, res) => { + +noAuthRouter.getAsync(['/test/group/:id', '/test/group/'], async (req, res) => { + const list = req.query.list !== undefined && req.query.list !== 'false' + const config = await configHelper.getGroupConfig(req.params.id, list) + if (!config) return res.status(404).end() + if (!list) { + res.set('Content-Type', 'text/plain') + res.send(config.script) + } else { + res.send(config) + } +}) + + +noAuthRouter.getAsync(['/test/:uuid', '/test/'], async (req, res) => { const list = req.query.list !== undefined && req.query.list !== 'false' const config = await configHelper.getConfig(req.params.uuid, list) if (!list) { @@ -21,6 +34,7 @@ noAuthRouter.getAsync('/test/:uuid', async (req, res) => { } }) + // if client in db -> load script (default if none is found), else load registration script noAuthRouter.getAsync('/:uuid', async (req, res) => { const uuid = req.params.uuid diff --git a/server/lib/confighelper.js b/server/lib/confighelper.js index 2771596..0e152a8 100644 --- a/server/lib/confighelper.js +++ b/server/lib/confighelper.js @@ -8,12 +8,12 @@ const url = config.https.host + ':' + config.https.port async function getConfig (uuid, list) { - const client = await db.client.findOne({ where: { uuid: uuid }, include: ['groups', 'events'] }) + const client = await db.client.findOne({ where: { uuid: uuid } }) let configPath = [] // client not known, start registration if (client === null) { - let config = { id: 'REGISTRATION', name: 'Registration Script' } + let config = { id: 'REGISTRATION', name: 'Client Registration' } if (!list) { config.script = fs.readFileSync(path.join(__appdir, 'ipxe', 'registration.ipxe'), 'utf8') return config @@ -49,7 +49,35 @@ async function getConfig (uuid, list) { else if (eventList.length) return eventList[0] else if (configList.length) return configList[0] } else { - configPath = configPath.concat(importantList, eventList, configList) + configPath.push(...importantList, ...eventList, ...configList) + } + + // No config found, use default config + let config = await _prepareConfig({ id: 'DEFAULT', source: { type: 'DEFAULT' } }) + if (!list) return config + + configPath.push(config) + return configPath +} + + +async function getGroupConfig (groupId, list) { + const group = await db.group.findOne({ where: { id: groupId } }) + if (!group) return + let configPath = [] + + // Check for event or direct configs + let importantList = [] + let eventList = [] + let configList = [] + await _checkParentConfigs([group.id], importantList, eventList, configList, !list) + + if (!list) { + if (importantList.length) return importantList[0] + else if (eventList.length) return eventList[0] + else if (configList.length) return configList[0] + } else { + configPath.push(...importantList, ...eventList, ...configList) } // No config found, use default config @@ -183,6 +211,8 @@ async function _createDynamicMenu (configInfos, noScript) { const ids = [] const idSourceMap = {} + console.log(configInfos) + configInfos.forEach(configInfo => { ids.push(configInfo.id) idSourceMap[configInfo.id] = configInfo.source @@ -192,6 +222,7 @@ async function _createDynamicMenu (configInfos, noScript) { const result = { merged: true, id: JSON.stringify(configs.map(x => x.id)), + name: configs.map(x => x.name).join(' + '), configs: configs.map(x => ({ id: x.id, name: x.name, source: idSourceMap[x.id] })) } if (noScript) return result @@ -224,4 +255,4 @@ async function _createDynamicMenu (configInfos, noScript) { } -module.exports = { getConfig } \ No newline at end of file +module.exports = { getConfig, getGroupConfig } \ No newline at end of file -- cgit v1.2.3-55-g7522