summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJannik Schönartz2018-08-10 04:41:57 +0200
committerJannik Schönartz2018-08-10 04:41:57 +0200
commit17591fc7edf4104895b5842d9bf3b0e8eff98304 (patch)
tree619f44ce618f41771a617a8a5c289263c04346d7
parent[permissions] fix bug with recursive-mode-switch (diff)
downloadbas-17591fc7edf4104895b5842d9bf3b0e8eff98304.tar.gz
bas-17591fc7edf4104895b5842d9bf3b0e8eff98304.tar.xz
bas-17591fc7edf4104895b5842d9bf3b0e8eff98304.zip
[server/router] Rework router
Reworked the router to match more design guidlines with the REST apis
-rw-r--r--server/api/backends.js642
-rw-r--r--server/api/backendtypes.js40
-rw-r--r--server/api/ipxe.js17
-rw-r--r--server/router.js15
-rw-r--r--webapp/src/components/BackendModuleEdit.vue19
-rw-r--r--webapp/src/components/BackendModuleSync.vue8
-rw-r--r--webapp/src/components/BackendModuleTable.vue3
-rw-r--r--webapp/src/store/backends.js2
8 files changed, 393 insertions, 353 deletions
diff --git a/server/api/backends.js b/server/api/backends.js
index 2fae6bd..f828aea 100644
--- a/server/api/backends.js
+++ b/server/api/backends.js
@@ -2,367 +2,343 @@
const path = require('path')
const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends'))
var db = require(path.join(__appdir, 'lib', 'sequelize'))
-
-// GET requests
-module.exports.get = {
-
- /*
- * ?id=<BACKEND_ID>
- *
- * @return: Returns the credentials structure and fields of a backend type.
- */
- getCredentialsByType: function (req, res) {
- const backendType = req.query.type
- const b = new ExternalBackends()
- const instance = b.getInstance(backendType)
- if (instance === null) {
- res.status(500).send({ auth: false, status: 'TYPE_INVALID', error_message: 'The provided backend type is invalid.' })
- }
- res.status(200).send(instance.getCredentials())
- },
-
- /*
- * ?id=<BACKEND_ID>
- *
- * @return: Returns the information of a backend.
- */
- getInfoById: function (req, res) {
- const backendId = req.query.id
- db.backend.findOne({ where: { id: backendId } }).then(backend => {
- const b = {
- backendId: backendId,
- backendName: backend.name,
- backendType: backend.type,
- backendCredentials: backend.credentials
- }
- res.status(200).send(b)
- })
- },
-
- /*
- * @return: Returns a list of all available backend types.
- */
- getTypes: function (req, res) {
- const backends = new ExternalBackends()
- var files = backends.getBackends()
-
- for (var i = 0; i < files.length; i++) {
- // Cut off -backends.js
- files[i] = files[i].slice(0, -11)
- }
-
- res.status(200).send(files)
- },
-
- /*
- * @return: Returns a list of all backends saved in the db.
- */
- getList: function (req, res) {
- db.backend.findAll({
- attributes: ['id', 'name', 'type']
- }).then(function (backends) {
- res.status(200).send(backends)
- })
- },
-
- /*
- * ?id=<Backend_ID>
- *
- * @return: Returns a list with all objects of the backend.
- */
- getObjects: function (req, res) {
- const id = req.query.id
- db.backend.findOne({ where: { id: id } }).then(backend => {
- if (backend) {
- const ba = new ExternalBackends()
- const instance = ba.getInstance(backend.type)
- instance.getObjects(backend.credentials).then(result => {
- res.status(200).send(result)
- })
- } else res.status(500).send({ status: 'INVALID_BACKEND_ID', error: 'The provided backend id is invalid.' })
- })
- },
-
- /*
- * ?id=<Backend_ID>
- * ?oid=<OBJECT_ID>
- *
- * @return: Returns information about a given object and all childs.
- */
- getObject: function (req, res) {
- const id = req.query.id
- const oid = req.query.oid
- db.backend.findOne({ where: { id: id } }).then(backend => {
- if (backend) {
- const ba = new ExternalBackends()
- const instance = ba.getInstance(backend.type)
- instance.getObject(backend.credentials, oid).then(result => {
- res.status(200).send(result)
- })
- } else res.status(500).send({ status: 'INVALID_BACKEND_ID', error: 'The provided backend id is invalid.' })
+var express = require('express')
+var router = express.Router()
+
+// GET requests.
+
+/*
+ * @return: Returns a list of all backends saved in the db.
+ */
+router.get('/', (req, res) => {
+ db.backend.findAll({
+ attributes: ['id', 'name', 'type']
+ }).then(function (backends) {
+ res.status(200).send(backends)
+ })
+})
+
+
+/*
+ * ?id=<BACKEND_ID>
+ *
+ * @return: Returns the information of a backend.
+ */
+router.get('/:id', (req, res) => {
+ const id = req.params.id
+ db.backend.findOne({ where: { id: id } }).then(backend => {
+ res.status(200).send({
+ id: id,
+ name: backend.name,
+ type: backend.type,
+ credentials: backend.credentials
})
- },
-
- /*
- * ?id=<Backend_ID>
- *
- * @return: Returns a list of all the object types of the given backend. [{id: <id>, title: <title>}, ...]
- */
- getObjectTypes: function (req, res) {
- const id = req.query.id
- db.backend.findOne({ where: { id: id } }).then(backend => {
+ })
+})
+
+/*
+ * ?id=<Backend_ID>
+ *
+ * @return: Returns a list with all objects of the backend.
+ */
+router.get('/:id/objects', (req, res) => {
+ const id = req.params.id
+ db.backend.findOne({ where: { id: id } }).then(backend => {
+ if (backend) {
const ba = new ExternalBackends()
const instance = ba.getInstance(backend.type)
- instance.getObjectTypes(backend.credentials).then(result => {
+ instance.getObjects(backend.credentials).then(result => {
res.status(200).send(result)
})
- })
- },
-
- /*
- * ?id=<Backend_ID>
- *
- * @return: Returns the sync settings saved in the db.
- */
- getSyncSettings: function (req, res) {
- const id = req.query.id
- var types = {}
- db.backend.findOne({ where: { id: id } }).then(backend => {
- types.groups = JSON.parse(backend.groupTypes)
- types.clients = JSON.parse(backend.clientTypes)
- types.sync = backend.sync
- res.status(200).send(types)
- })
- },
-
- /*
- * ?id=<Backend_ID>
- *
- * @return: Returns a list of sync types the backend supports.
- */
- getSyncTypes: function (req, res) {
- const id = req.query.id
- db.backend.findOne({ where: { id: id } }).then(backend => {
+ } else res.status(500).send({ status: 'INVALID_BACKEND_ID', error: 'The provided backend id is invalid.' })
+ })
+})
+
+/*
+ * ?id=<Backend_ID>
+ * ?oid=<OBJECT_ID>
+ *
+ * @return: Returns information about a given object and all childs.
+ */
+router.get('/:id/objects/:oid', (req, res) => {
+ const id = req.params.id
+ const oid = req.params.oid
+ db.backend.findOne({ where: { id: id } }).then(backend => {
+ if (backend) {
const ba = new ExternalBackends()
const instance = ba.getInstance(backend.type)
- res.status(200).send(instance.getSyncTypes())
+ instance.getObject(backend.credentials, oid).then(result => {
+ res.status(200).send(result)
+ })
+ } else res.status(500).send({ status: 'INVALID_BACKEND_ID', error: 'The provided backend id is invalid.' })
+ })
+})
+
+
+/*
+ * ?id=<Backend_ID>
+ *
+ * @return: Returns a list of all the object types of the given backend. [{id: <id>, title: <title>}, ...]
+ */
+router.get('/:id/objecttypes', (req, res) => {
+ const id = req.params.id
+ db.backend.findOne({ where: { id: id } }).then(backend => {
+ const ba = new ExternalBackends()
+ const instance = ba.getInstance(backend.type)
+ instance.getObjectTypes(backend.credentials).then(result => {
+ res.status(200).send(result)
})
- },
-
- /*
- * ?id: <BACKEND_ID>
- * id: <BACKEND_ID>
- */
- importObjects: function (req, res) {
- const id = req.query.id
- // const id = req.body.id
-
- // Get the backend where the objects are importet from.
- db.backend.findOne({ where: { id: id } }).then(backend => {
- if (backend) {
- var endRequest = []
- const ba = new ExternalBackends()
- const instance = ba.getInstance(backend.type)
- const groups = JSON.parse(backend.groupTypes).map(x => x.id)
- const clients = JSON.parse(backend.clientTypes).map(x => x.id)
-
- // Get a list with all objects in the backend.
- const objectPromise = new Promise(function (resolve, reject) {
- resolve(instance.getObjects(backend.credentials))
- })
+ })
+})
+
+/*
+ * ?id=<Backend_ID>
+ *
+ * @return: Returns the sync settings saved in the db.
+ */
+router.get('/:id/syncsettings', (req, res) => {
+ const id = req.params.id
+ var types = {}
+ db.backend.findOne({ where: { id: id } }).then(backend => {
+ types.groups = JSON.parse(backend.groupTypes)
+ types.clients = JSON.parse(backend.clientTypes)
+ types.sync = backend.sync
+ res.status(200).send(types)
+ })
+})
+
+/*
+ * ?id=<Backend_ID>
+ *
+ * @return: Returns a list of sync types the backend supports.
+ */
+router.get('/:id/synctypes', (req, res) => {
+ const id = req.params.id
+ db.backend.findOne({ where: { id: id } }).then(backend => {
+ const ba = new ExternalBackends()
+ const instance = ba.getInstance(backend.type)
+ res.status(200).send(instance.getSyncTypes())
+ })
+})
- objectPromise.then(result => {
- // Check for the not implemented exception
- if (result.status) res.status(501).send(result)
+// POST requests
- // Filter those objects in groups / clients
- var groupObjects = []
- var clientObjects = []
- result.objects.filter(obj => {
- if (groups.find(x => x === obj.type)) groupObjects.push({ id: obj.id, name: obj.title, type: obj.type, typeName: obj.type_title, sysid: obj.sysid })
- else if (clients.find(y => y === obj.type)) clientObjects.push({ id: obj.id, name: obj.title, type: obj.type, typeName: obj.type_title, sysid: obj.sysid })
- })
+/*
+ * id: <BACKEND_ID>
+ */
+router.get('/:id/import', (req, res) => {
+ const id = req.params.id
- var promises = []
- var promises2 = []
+ // Get the backend where the objects are importet from.
+ db.backend.findOne({ where: { id: id } }).then(backend => {
+ if (backend) {
+ var endRequest = []
+ const ba = new ExternalBackends()
+ const instance = ba.getInstance(backend.type)
+ const groups = JSON.parse(backend.groupTypes).map(x => x.id)
+ const clients = JSON.parse(backend.clientTypes).map(x => x.id)
- // Add all groups in the database.
- groupObjects.forEach(group => {
- // Insert the group.
- promises.push(db.group.create({ name: group.name, description: group.typeName }).then(g => {
- // Insert the backend_x_group relation.
- promises2.push(backend.addMappedGroups(g, { through: { externalId: group.id, externalType: group.type } }))
- }))
- })
+ // Get a list with all objects in the backend.
+ const objectPromise = new Promise(function (resolve, reject) {
+ resolve(instance.getObjects(backend.credentials))
+ })
- // Add all clients in the databse.
- clientObjects.forEach(client => {
- // Insert the client.
- promises.push(db.client.create({ name: client.name, description: client.typeName }).then(c => {
- // Insert the backend_x_client relation.
- promises2.push(backend.addMappedClients(c, { through: { externalId: client.id, externalType: client.type } }))
- }))
- })
+ objectPromise.then(result => {
+ // Check for the not implemented exception
+ if (result.status) res.status(501).send(result)
- // Wait till all clients / groups are created and all mapping operations are done. Then add childs.
- Promise.all(promises).then(() => {
- Promise.all(promises2).then(() => {
- // Get the backend with all the mapped groups. ! Only groups can have childs !
- db.backend.findOne({ where: { id: backend.id }, include: ['mappedGroups'] }).then(b => {
- var objectData = []
- // Put all groups in the array to make a one session call which returns all needed informations.
- b.mappedGroups.forEach(mGroup => {
- const mG = mGroup.backend_x_group
- const eid = mG.externalId
- const gid = mGroup.id
- objectData.push({ eid: eid, gid: gid })
- })
+ // Filter those objects in groups / clients
+ var groupObjects = []
+ var clientObjects = []
+ result.objects.filter(obj => {
+ if (groups.find(x => x === obj.type)) groupObjects.push({ id: obj.id, name: obj.title, type: obj.type, typeName: obj.type_title, sysid: obj.sysid })
+ else if (clients.find(y => y === obj.type)) clientObjects.push({ id: obj.id, name: obj.title, type: obj.type, typeName: obj.type_title, sysid: obj.sysid })
+ })
- // Get all the information needed from the backend instance. (For all object ids in the array)
- var promise = new Promise(function (resolve) {
- resolve(instance.getDataTree(backend.credentials, objectData))
- })
+ var promises = []
+ var promises2 = []
- promise.then(data => {
- // Check for the not implemented exception
- if (data.status) res.status(501).send(data)
-
- data.forEach(obj => {
- var groupChildsToAdd = []
- var clientChildsToAdd = []
- var prom = []
-
- // Put all clientChilds in the clientList and all groupChilds in the groupList.
- obj.childs.forEach(child => {
- if (groups.find(x => x === child.type)) {
- // Get the group id out of the externalId.
- prom.push(db.backend.findOne({ where: { id: backend.id, '$mappedGroups.backend_x_group.externalId$': child.id }, include: ['mappedGroups'] }).then(ba => {
- // The externalId should only be once in the db.
- if (ba.mappedGroups.length === 1) {
- groupChildsToAdd.push(ba.mappedGroups[0].backend_x_group.groupId)
- }
- }))
- } else if (clients.find(x => x === child.type)) {
- // Get the client id out of the externalId.
- prom.push(db.backend.findOne({ where: { id: backend.id, '$mappedClients.backend_x_client.externalId$': child.id }, include: ['mappedClients'] }).then(ba => {
- // The externalId should only be once in the db.
- if (ba.mappedClients.length === 1) {
- clientChildsToAdd.push(ba.mappedClients[0].backend_x_client.clientId)
- }
- }))
- }
- })
-
- // After all the group and client ids are collected. Add them as subgroup / client
- Promise.all(prom).then(() => {
- endRequest.push(db.group.findOne({ where: { id: obj.gid } }).then(group => {
- if (group) {
- group.addSubgroups(groupChildsToAdd)
- group.addClients(clientChildsToAdd)
+ // Add all groups in the database.
+ groupObjects.forEach(group => {
+ // Insert the group.
+ promises.push(db.group.create({ name: group.name, description: group.typeName }).then(g => {
+ // Insert the backend_x_group relation.
+ promises2.push(backend.addMappedGroups(g, { through: { externalId: group.id, externalType: group.type } }))
+ }))
+ })
+
+ // Add all clients in the databse.
+ clientObjects.forEach(client => {
+ // Insert the client.
+ promises.push(db.client.create({ name: client.name, description: client.typeName }).then(c => {
+ // Insert the backend_x_client relation.
+ promises2.push(backend.addMappedClients(c, { through: { externalId: client.id, externalType: client.type } }))
+ }))
+ })
+
+ // Wait till all clients / groups are created and all mapping operations are done. Then add childs.
+ Promise.all(promises).then(() => {
+ Promise.all(promises2).then(() => {
+ // Get the backend with all the mapped groups. ! Only groups can have childs !
+ db.backend.findOne({ where: { id: backend.id }, include: ['mappedGroups'] }).then(b => {
+ var objectData = []
+ // Put all groups in the array to make a one session call which returns all needed informations.
+ b.mappedGroups.forEach(mGroup => {
+ const mG = mGroup.backend_x_group
+ const eid = mG.externalId
+ const gid = mGroup.id
+ objectData.push({ eid: eid, gid: gid })
+ })
+
+ // Get all the information needed from the backend instance. (For all object ids in the array)
+ var promise = new Promise(function (resolve) {
+ resolve(instance.getDataTree(backend.credentials, objectData))
+ })
+
+ promise.then(data => {
+ // Check for the not implemented exception
+ if (data.status) res.status(501).send(data)
+
+ data.forEach(obj => {
+ var groupChildsToAdd = []
+ var clientChildsToAdd = []
+ var prom = []
+
+ // Put all clientChilds in the clientList and all groupChilds in the groupList.
+ obj.childs.forEach(child => {
+ if (groups.find(x => x === child.type)) {
+ // Get the group id out of the externalId.
+ prom.push(db.backend.findOne({ where: { id: backend.id, '$mappedGroups.backend_x_group.externalId$': child.id }, include: ['mappedGroups'] }).then(ba => {
+ // The externalId should only be once in the db.
+ if (ba.mappedGroups.length === 1) {
+ groupChildsToAdd.push(ba.mappedGroups[0].backend_x_group.groupId)
+ }
+ }))
+ } else if (clients.find(x => x === child.type)) {
+ // Get the client id out of the externalId.
+ prom.push(db.backend.findOne({ where: { id: backend.id, '$mappedClients.backend_x_client.externalId$': child.id }, include: ['mappedClients'] }).then(ba => {
+ // The externalId should only be once in the db.
+ if (ba.mappedClients.length === 1) {
+ clientChildsToAdd.push(ba.mappedClients[0].backend_x_client.clientId)
}
}))
- })
+ }
+ })
+
+ // After all the group and client ids are collected. Add them as subgroup / client
+ Promise.all(prom).then(() => {
+ endRequest.push(db.group.findOne({ where: { id: obj.gid } }).then(group => {
+ if (group) {
+ group.addSubgroups(groupChildsToAdd)
+ group.addClients(clientChildsToAdd)
+ }
+ }))
})
})
})
})
})
+ })
- // If all requests are fullfilled. End the request.
- Promise.all(endRequest).then(() => {
- res.status(200).send({ status: 'SUCCESS' })
- })
+ // If all requests are fullfilled. End the request.
+ Promise.all(endRequest).then(() => {
+ res.status(200).send({ status: 'SUCCESS' })
})
- } else res.status(500).send({ status: 'INVALID_BACKEND_ID', error: 'The provided backend id is invalid.' })
+ })
+ } else res.status(500).send({ status: 'INVALID_BACKEND_ID', error: 'The provided backend id is invalid.' })
+ })
+})
+
+// POST requests
+
+/*
+ * id: <BACKEND_ID>
+ * <OR>
+ * name: <BACKEND_NAME>
+ * type: <BACKEND_TYPE>
+ * credentials: <BACKEND_CREDENTIALS>
+ *
+ * If the id is set, the backend in the db ist testet.
+ * Else the backend is postet in the request.
+ */
+router.post('/:id/connection', (req, res) => {
+ const id = req.params.id
+
+ var getBackend = null
+ if (id !== '0') getBackend = db.backend.findOne({ where: { id: id } })
+ else getBackend = new Promise(resolve => resolve(req.body))
+
+ getBackend.then(backend => {
+ // Creating the backend instance and calling the specific checkConnection.
+ const b = new ExternalBackends()
+ const instance = b.getInstance(backend.type)
+ instance.checkConnection(backend.credentials).then(connection => {
+ res.status(200).send({ success: connection.success, error: connection.error })
})
+ })
+})
+
+// PUT requests
+
+/*
+ * id: <BACKEND_ID>
+ * name: <BACKEND_NAME>
+ * type: <BACKEND_TYPE>
+ * credentials: <BACKEND_CREDENTIALS>
+ *
+ * Creates or updates the backend.
+ */
+router.put('/:id', (req, res) => {
+ const id = req.params.id
+ console.log(id)
+ // Save credentials in the db.
+ const backend = req.body
+ const credentialString = JSON.stringify(backend.credentials)
+
+ if (id === '0') {
+ // Insert new backend in the db.
+ db.backend.create({ name: backend.name, type: backend.type, credentials: credentialString })
+ } else {
+ // Update an existing backend in the db.
+ db.backend.update({ name: backend.name, type: backend.type, credentials: credentialString }, { where: { id: id } })
}
-}
+ res.status(200).send('success')
+})
+
+/*
+ * id: <BACKEND_ID>
+ * groups: <JSON_OF_ASSIGNED_GROUPS> (list of backend types)
+ * clients: <JSON_OF_ASSIGNED_CLIENTS> (list of backend types)
+ * sync: <SYNC_OPTION>
+ *
+ * Saves the group / clients assigned object types in the database and sync type.
+ */
+router.put('/:id/syncsettings', (req, res) => {
+ const id = req.params.id
+ const groups = JSON.stringify(req.body.groups)
+ const clients = JSON.stringify(req.body.clients)
+ const sync = req.body.sync
+ db.backend.findOne({ where: { id: id } }).then(backend => {
+ db.backend.update({ groupTypes: groups, clientTypes: clients, sync: sync }, { where: { id: id } }).then(() => {
+ res.status(200).send()
+ })
+ })
+})
-// POST requests
-module.exports.post = {
-
- /*
- * id: <BACKEND_ID>
- * name: <BACKEND_NAME>
- * type: <BACKEND_TYPE>
- * credentials: <BACKEND_CREDENTIALS>
- *
- * Creates or updates the backend.
- */
- save: function (req, res) {
- // Save credentials in the db.
- const backend = req.body
- const credentialString = JSON.stringify(backend.credentials)
-
- if (backend.id === 0) {
- // Insert new backend in the db.
- db.backend.create({ name: backend.name, type: backend.type, credentials: credentialString })
- } else {
- // Update an existing backend in the db.
- db.backend.update({ name: backend.name, type: backend.type, credentials: credentialString }, { where: { id: backend.id } })
- }
+// DELETE requests // TODO:
+/*
+ * id: <BACKEND_ID>
+ *
+ * Deletes the backend to the given id.
+ */
+router.post('/delete', (req, res) => {
+ const backendIds = req.body.id
+
+ db.backend.destroy({ where: { id: backendIds } }).then(function () {
res.status(200).send('success')
- },
-
- /*
- * id: <BACKEND_ID>
- *
- * Deletes the backend to the given id.
- */
- delete: function (req, res) {
- const backendIds = req.body.id
-
- db.backend.destroy({ where: { id: backendIds } }).then(function () {
- res.status(200).send('success')
- })
- },
-
- /*
- * id: <BACKEND_ID>
- * <OR>
- * name: <BACKEND_NAME>
- * type: <BACKEND_TYPE>
- * credentials: <BACKEND_CREDENTIALS>
- *
- * If the id is set, the backend in the db ist testet.
- * Else the backend is postet in the request.
- */
- checkConnection: function (req, res) {
- const id = req.body.id
-
- var getBackend = null
- if (id) getBackend = db.backend.findOne({ where: { id: id } })
- else getBackend = new Promise(resolve => resolve(req.body))
-
- getBackend.then(backend => {
- // Creating the backend instance and calling the specific checkConnection.
- const b = new ExternalBackends()
- const instance = b.getInstance(backend.type)
- instance.checkConnection(backend.credentials).then(connection => {
- res.status(200).send({ success: connection.success, error: connection.error })
- })
- })
- },
-
- /*
- * id: <BACKEND_ID>
- * groups: <JSON_OF_ASSIGNED_GROUPS> (list of backend types)
- * clients: <JSON_OF_ASSIGNED_CLIENTS> (list of backend types)
- * sync: <SYNC_OPTION>
- *
- * Saves the group / clients assigned object types in the database and sync type.
- */
- saveSyncSettings: function (req, res) {
- const id = req.body.id
- const groups = JSON.stringify(req.body.groups)
- const clients = JSON.stringify(req.body.clients)
- const sync = req.body.sync
- db.backend.findOne({ where: { id: id } }).then(backend => {
- db.backend.update({ groupTypes: groups, clientTypes: clients, sync: sync }, { where: { id: id } }).then(() => {
- res.status(200).send()
- })
- })
- }
-}
+ })
+})
+
+module.exports.router = router
diff --git a/server/api/backendtypes.js b/server/api/backendtypes.js
new file mode 100644
index 0000000..1bb1047
--- /dev/null
+++ b/server/api/backendtypes.js
@@ -0,0 +1,40 @@
+/* global __appdir */
+const path = require('path')
+const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends'))
+var db = require(path.join(__appdir, 'lib', 'sequelize'))
+var express = require('express')
+var router = express.Router()
+
+
+// GET requests.
+
+/*
+ * @return: Returns a list of all available backend types.
+ */
+router.get('/', (req, res) => {
+ const backends = new ExternalBackends()
+ var files = backends.getBackends()
+
+ for (var i = 0; i < files.length; i++) {
+ // Cut off -backends.js
+ files[i] = files[i].slice(0, -11)
+ }
+ res.status(200).send(files)
+})
+
+/*
+ * ?type=<BACKEND_TYPE>
+ *
+ * @return: Returns the credentials structure and fields of a backend type.
+ */
+router.get('/:type', (req, res) => {
+ const backendType = req.params.type
+ const b = new ExternalBackends()
+ const instance = b.getInstance(backendType)
+ if (instance === null) {
+ res.status(500).send({ auth: false, status: 'TYPE_INVALID', error_message: 'The provided backend type is invalid.' })
+ }
+ res.status(200).send(instance.getCredentials())
+})
+
+module.exports.router = router
diff --git a/server/api/ipxe.js b/server/api/ipxe.js
index 3e8dd9a..c9eb5f9 100644
--- a/server/api/ipxe.js
+++ b/server/api/ipxe.js
@@ -1,11 +1,22 @@
/* global __appdir */
var path = require('path')
var shell = require(path.join(__appdir, 'lib', 'shell'))
+var express = require('express')
+var router = express.Router()
+
+
+// GET requests.
+
+/*
+ * @return: Rebuild the ipxe.
+ */
+router.get('/build', (req, res) => {
+ shell.buildIpxe(req, res)
+})
+
+module.exports.router = router
module.exports.get = {
- build: function (req, res) {
- shell.buildIpxe(req, res)
- },
loadScript: function (req, res) {
res.setHeader('content-type', 'text/plain')
res.status(200).send(`#!ipxe
diff --git a/server/router.js b/server/router.js
index f77d09a..fbd71f2 100644
--- a/server/router.js
+++ b/server/router.js
@@ -1,6 +1,7 @@
var express = require('express')
var router = express.Router()
var path = require('path')
+var fs = require('fs');
// Authentication routes
var auth = require(path.join(__dirname, 'lib', 'authentication'))
@@ -14,6 +15,16 @@ router.post('/changepassword', auth.changePassword)
var ipxe = require(path.join(__dirname, 'api', 'ipxe'))
router.get('/ipxe/loadScript', ipxe.get.loadScript)
+// Forward routing to every api module with /<api>/...
+fs.readdirSync(path.join(__dirname, 'api')).forEach(filename => {
+ var api = require(path.join(__dirname, 'api', filename))
+ if (api.router) router.use('/' + filename.split('.')[0] + '/', auth.verifyToken, api.router)
+})
+
+// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+// !!!!!!!! TODO: DELETE IF ALL MODULES ARE REWORKED WITH exports.router !!!!!!!
+// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
// Dynamic API routes
function mapApi (method) {
return function (req, res) {
@@ -30,6 +41,10 @@ function mapApi (method) {
router.get('/:api/:action', auth.verifyToken, mapApi('get'))
router.post('/:api/:action', auth.verifyToken, mapApi('post'))
+// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
router.use('*', (req, res) => {
res.status(404).end()
})
diff --git a/webapp/src/components/BackendModuleEdit.vue b/webapp/src/components/BackendModuleEdit.vue
index dbb9487..ad06d54 100644
--- a/webapp/src/components/BackendModuleEdit.vue
+++ b/webapp/src/components/BackendModuleEdit.vue
@@ -149,8 +149,7 @@ export default {
methods: {
submit (event) {
if (this.$refs.form.validate()) {
- this.$http.post('/api/backends/save', {
- id: this.backendId,
+ this.$http.put('/api/backends/' + this.backendId, {
name: this.backendName,
type: this.backendType,
credentials: this.credentials
@@ -168,24 +167,24 @@ export default {
}
},
loadBackendTypes () {
- this.$http('/api/backends/getTypes').then(response => {
+ this.$http('/api/backendtypes').then(response => {
this.backendChoices = response.data
})
},
loadInputFields () {
if (!this.loadData) {
- this.$http('/api/backends/getCredentialsByType?type=' + this.backendType).then(response => {
+ this.$http('/api/backendtypes/' + this.backendType).then(response => {
this.elements = response.data
})
}
},
loadBackend (backendId) {
- this.$http('/api/backends/getInfoById?id=' + this.backendId).then(response => {
- this.backendName = response.data.backendName
+ this.$http('/api/backends/' + this.backendId).then(response => {
+ this.backendName = response.data.name
this.loadData = true
- this.backendType = response.data.backendType
- const credentialValues = JSON.parse(response.data.backendCredentials)
- this.$http('/api/backends/getCredentialsByType?type=' + this.backendType).then(res => {
+ this.backendType = response.data.type
+ const credentialValues = JSON.parse(response.data.credentials)
+ this.$http('/api/backendtypes/' + this.backendType).then(res => {
var credentials = res.data
// Make an array merge to combine the credentials with the values.
@@ -221,7 +220,7 @@ export default {
this.statusLabel = this.$t('progress')
// Test the credential connection.
- this.$http.post('/api/backends/checkConnection', {
+ this.$http.post('/api/backends/0/connection', {
name: this.backendName,
type: this.backendType,
credentials: JSON.stringify(this.credentials),
diff --git a/webapp/src/components/BackendModuleSync.vue b/webapp/src/components/BackendModuleSync.vue
index 1df3354..99a7126 100644
--- a/webapp/src/components/BackendModuleSync.vue
+++ b/webapp/src/components/BackendModuleSync.vue
@@ -136,7 +136,7 @@ export default {
},
methods: {
save (event) {
- this.$http.post('/api/backends/saveSyncSettings', {
+ this.$http.put('/api/backends/' + this.backendId + '/syncsettings', {
id: this.backendId,
groups: this.cbxGroups,
clients: this.cbxClients,
@@ -174,15 +174,15 @@ export default {
sync: function (value) {
if (value) {
// Load the sync types from the backend.
- this.$http('/api/backends/getSyncTypes?id=' + this.backendId).then(response => {
+ this.$http('/api/backends/' + this.backendId + '/synctypes').then(response => {
this.syncTypes = response.data
})
// Load the object types from the backend.
- this.$http('/api/backends/getObjectTypes?id=' + this.backendId).then(response => {
+ this.$http('/api/backends/' + this.backendId + '/objecttypes').then(response => {
this.objectTypes = response.data
})
- this.$http('/api/backends/getSyncSettings?id=' + this.backendId).then(response => {
+ this.$http('/api/backends/' + this.backendId + '/syncsettings').then(response => {
this.cbxGroups = response.data.groups
this.cbxClients = response.data.clients
this.syncType = response.data.sync
diff --git a/webapp/src/components/BackendModuleTable.vue b/webapp/src/components/BackendModuleTable.vue
index d4cb1ba..d975e6f 100644
--- a/webapp/src/components/BackendModuleTable.vue
+++ b/webapp/src/components/BackendModuleTable.vue
@@ -107,8 +107,7 @@ export default {
// Set to start the loading animation.
item.loading = true
// Test the credential connection.
- this.$http.post('/api/backends/checkConnection', {
- id: item.id,
+ this.$http.post('/api/backends/' + item.id + '/connection', {
headers: {
'Cache-Control': 'no-cache'
}
diff --git a/webapp/src/store/backends.js b/webapp/src/store/backends.js
index 3234682..d832b6e 100644
--- a/webapp/src/store/backends.js
+++ b/webapp/src/store/backends.js
@@ -47,7 +47,7 @@ export default {
})
},
loadData (context) {
- axios.get('/api/backends/getList').then(response => {
+ axios.get('/api/backends').then(response => {
// Needed for initializing the diffrent dynamic loading buttons.
var tmpItems = response.data
tmpItems.forEach(function (item) {