summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorUdo Walter2019-03-15 05:47:37 +0100
committerUdo Walter2019-03-15 05:47:37 +0100
commit204e6f1f57e6fc4ab80f607b4881df868bb1ebe7 (patch)
tree6ec32ad0852383453cffa2689290a8bf94a69ab7 /server
parent[server/log] add log lib (diff)
parent[external-backends] Small bugfix to save the switch again & add tooltips to t... (diff)
downloadbas-204e6f1f57e6fc4ab80f607b4881df868bb1ebe7.tar.gz
bas-204e6f1f57e6fc4ab80f607b4881df868bb1ebe7.tar.xz
bas-204e6f1f57e6fc4ab80f607b4881df868bb1ebe7.zip
merge
Diffstat (limited to 'server')
-rw-r--r--server/api/backends.js85
-rw-r--r--server/api/backendtypes.js2
-rw-r--r--server/api/ipxe.js13
-rw-r--r--server/lib/external-backends/backendhelper.js2
-rw-r--r--server/lib/external-backends/backends/idoit-backend.js8
-rw-r--r--server/lib/external-backends/index.js25
6 files changed, 86 insertions, 49 deletions
diff --git a/server/api/backends.js b/server/api/backends.js
index 1f9535b..7738ec7 100644
--- a/server/api/backends.js
+++ b/server/api/backends.js
@@ -3,20 +3,18 @@ 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()
-var noAuthRouter = express.Router()
+const { decorateApp } = require('@awaitjs/express')
+var router = decorateApp(express.Router())
+var noAuthRouter = decorateApp(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)
- })
+router.getAsync('/', async (req, res) => {
+ const backends = await db.backend.findAll({ attributes: ['id', 'name', 'type'] })
+ res.status(200).send(backends)
})
/*
@@ -24,15 +22,35 @@ router.get('/', (req, res) => {
*
* @return: Returns the information of a backend.
*/
-router.get('/:id', (req, res) => {
+router.getAsync('/:id', async (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
- })
+ const backend = await db.backend.findOne({ where: { id: id } })
+
+ // Remove password values from credentials.
+ const externalBackends = new ExternalBackends()
+ const instance = externalBackends.getInstance(backend.type)
+ let credentialTypes = instance.getCredentials()
+
+ // Get the ids of the 'password' fieldds
+ let censorIds = []
+ credentialTypes.forEach(function f (element) {
+ if (element.type === 'switch') {
+ element.elements.forEach(f)
+ } else if (element.type === 'password') censorIds.push(element.id)
+ })
+
+ // Filter the password values. No need for the frontend to have those.
+ let credentials = JSON.parse(backend.credentials)
+ credentials.forEach(function x (e) {
+ if (e.elements) e.elements.forEach(x)
+ else if (censorIds.includes(e.id)) e.value = ''
+ })
+
+ res.status(200).send({
+ id: id,
+ name: backend.name,
+ type: backend.type,
+ credentials: credentials
})
})
@@ -50,7 +68,7 @@ router.get('/:id/objects', (req, res) => {
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.' })
+ } else res.status(500).send({ error: 'INVALID_BACKEND_ID', message: 'The provided backend id is invalid.' })
})
})
@@ -70,7 +88,7 @@ router.get('/:id/objects/:oid', (req, res) => {
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.' })
+ } else res.status(500).send({ error: 'INVALID_BACKEND_ID', message: 'The provided backend id is invalid.' })
})
})
@@ -175,7 +193,7 @@ router.get('/:id/import', (req, res) => {
objectPromise.then(result => {
// Check for the not implemented exception
- if (result.status === 'NOT_IMPLEMENTED_EXCEPTION') res.status(501).send(result)
+ if (result.error === 'NOT_IMPLEMENTED_EXCEPTION') res.status(501).send(result)
// Filter those objects in groups / clients
var groupObjects = []
@@ -227,7 +245,7 @@ router.get('/:id/import', (req, res) => {
promise.then(data => {
// Check for the not implemented exception
- if (data.status) res.status(501).send(data)
+ if (data.error) res.status(501).send(data)
data.forEach(obj => {
var groupChildsToAdd = []
@@ -275,7 +293,7 @@ router.get('/:id/import', (req, res) => {
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({ error: 'INVALID_BACKEND_ID', message: 'The provided backend id is invalid.' })
})
})
@@ -295,7 +313,7 @@ noAuthRouter.get('/:id/:uuid/files', (req, res) => {
res.send({ success: true, data: { backendId: id, clientUUID: uuid, externalId: externalId, fileList: fileList } })
})
} else {
- res.send({ success: false, status: 'CLIENT_NOT_FOUND', error: 'Couldn\'t find the client' })
+ res.send({ success: false, error: 'CLIENT_NOT_FOUND', message: 'Couldn\'t find the client' })
}
})
})
@@ -315,7 +333,7 @@ noAuthRouter.get('/:id/:uuid/files/:filename', (req, res) => {
res.send(file)
})
} else {
- res.send({ success: false, status: 'CLIENT_NOT_FOUND', error: 'Couldn\'t find the client' })
+ res.send({ success: false, error: 'CLIENT_NOT_FOUND', message: 'Couldn\'t find the client' })
}
})
})
@@ -359,19 +377,34 @@ router.post('/:id/connection', (req, res) => {
*
* Creates or updates the backend.
*/
-router.put('/:id', (req, res) => {
+router.putAsync('/:id', async (req, res) => {
const id = req.params.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.
+ const credentialString = JSON.stringify(backend.credentials)
db.backend.create({ name: backend.name, type: backend.type, credentials: credentialString })
} else {
+ const backendDb = await db.backend.findOne({ where: { id: id } })
+ if (!backendDb) return res.status(404).send({ error: 'BACKEND_NOT_FOUND', message: 'No backend was found with the provided backend id.' })
+
+ // Merge the old and the new credential values (passwords are only send if they changed)
+ let credentials = JSON.parse(backendDb.credentials)
+ let newAttributes = {}
+ backend.credentials.forEach(function x (e) {
+ if (e.elements) e.elements.forEach(x)
+ newAttributes[e.id] = e.value
+ })
+ credentials.forEach(function f (element) {
+ if (element.elements) element.elements.forEach(f)
+ if (newAttributes.hasOwnProperty(element.id)) element.value = newAttributes[element.id]
+ })
+
// Update an existing backend in the db.
- db.backend.update({ name: backend.name, type: backend.type, credentials: credentialString }, { where: { id: id } })
+ db.backend.update({ name: backend.name, type: backend.type, credentials: JSON.stringify(credentials) }, { where: { id: id } })
}
res.status(200).send('success')
diff --git a/server/api/backendtypes.js b/server/api/backendtypes.js
index 8e2665e..ef371d8 100644
--- a/server/api/backendtypes.js
+++ b/server/api/backendtypes.js
@@ -30,7 +30,7 @@ router.get('/:type', (req, res) => {
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(500).send({ error: 'TYPE_INVALID', message: 'The provided backend type is invalid.' })
}
res.status(200).send(instance.getCredentials())
})
diff --git a/server/api/ipxe.js b/server/api/ipxe.js
index 6367246..2e89e32 100644
--- a/server/api/ipxe.js
+++ b/server/api/ipxe.js
@@ -28,12 +28,17 @@ router.get('/:version/console', (req, res) => {
})
router.get('/:version/log', async (req, res) => {
+ const max = req.query.max ? req.query.max : -1
res.setHeader('content-type', 'text/plain')
const filepath = path.join(__appdir, 'ipxe', 'log_' + req.params.version + '.txt')
- res.sendFile(filepath, err => {
- if (err) {
- res.end()
- }
+
+ fs.readFile(filepath, 'utf-8', function (err, content) {
+ if (err) res.end()
+ if (max !== -1 && content) {
+ let c = content.split('\n')
+ c = c.splice(-max)
+ res.send(c.join('\n'))
+ } else res.send(content)
})
})
diff --git a/server/lib/external-backends/backendhelper.js b/server/lib/external-backends/backendhelper.js
index 752ccf7..ae1af3d 100644
--- a/server/lib/external-backends/backendhelper.js
+++ b/server/lib/external-backends/backendhelper.js
@@ -32,7 +32,7 @@ module.exports = {
if (client.parents) {
var elements = backend.mappedGroups.filter(x => client.parents.includes(x.id))
if (elements.length > 1) {
- // TODO ADD MERGE CONFLICT
+ // Conflict occured!
const conflict = await db.conflict.create({ description: 'Multiple parents found' })
// Add backend to the conflict.
diff --git a/server/lib/external-backends/backends/idoit-backend.js b/server/lib/external-backends/backends/idoit-backend.js
index 43ab434..d3d992f 100644
--- a/server/lib/external-backends/backends/idoit-backend.js
+++ b/server/lib/external-backends/backends/idoit-backend.js
@@ -12,16 +12,16 @@ class IdoitBackend extends ExternalBackends {
*/
getCredentials () {
return [
- { type: 'text', id: 1, name: 'API url', icon: 'link' },
- { type: 'password', id: 2, name: 'API token', icon: 'vpn_key', show: false },
+ { type: 'text', id: 1, name: 'API URL', icon: 'link' },
+ { type: 'password', id: 2, name: 'API Token', icon: 'vpn_key', show: false },
{
type: 'switch',
id: 3,
name: 'Login',
icon: 'lock_open',
elements: [
- { type: 'text', id: 4, name: 'username', icon: 'person_outline' },
- { type: 'password', id: 5, name: 'password', icon: 'lock', show: false }
+ { type: 'text', id: 4, name: 'Username', icon: 'person_outline' },
+ { type: 'password', id: 5, name: 'Password', icon: 'lock', show: false }
]
}
]
diff --git a/server/lib/external-backends/index.js b/server/lib/external-backends/index.js
index a0ba25b..92e0f07 100644
--- a/server/lib/external-backends/index.js
+++ b/server/lib/external-backends/index.js
@@ -22,8 +22,7 @@ class ExternalBackends {
* { type: 'select', id: 4, name: '<NAME>', icon: '<ICON_NAME>' }, ...]
*/
getCredentials () {
- console.log('If this method gets called the backend class has NOT IMPLEMENTED the getCredentials method!')
- return null
+ return { error: 'NOT_IMPLEMENTED_EXCEPTION', message: 'The provided backend does not have a getCredentials method' }
}
/*
@@ -62,7 +61,7 @@ class ExternalBackends {
* return:
*/
async getClient (credentials, client) {
- return { status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a getClient method' }
+ return { error: 'NOT_IMPLEMENTED_EXCEPTION', message: 'The provided backend does not have a getClient method' }
}
/*
@@ -71,7 +70,7 @@ class ExternalBackends {
* Returns a list of all objects in the backend.
*/
async getObjects (credendtials) {
- return { status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a getObjects method' }
+ return { error: 'NOT_IMPLEMENTED_EXCEPTION', message: 'The provided backend does not have a getObjects method' }
}
/*
@@ -81,7 +80,7 @@ class ExternalBackends {
* Call the API of the backend and returns the information to the object including a list of childs.
*/
async getObject (credentials, oid) {
- return { status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a getObject method' }
+ return { error: 'NOT_IMPLEMENTED_EXCEPTION', message: 'The provided backend does not have a getObject method' }
}
/*
@@ -91,7 +90,7 @@ class ExternalBackends {
* Deletes the objecs from the backend.
*/
async deleteObjects (credentials, objectIds) {
- return { status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a deleteObject method' }
+ return { error: 'NOT_IMPLEMENTED_EXCEPTION', message: 'The provided backend does not have a deleteObject method' }
}
/*
@@ -102,7 +101,7 @@ class ExternalBackends {
* return: [{ gid: <GROUP_ID>, childs: [{ id: <EXTERNAL_ID>, }, ...]}, ...]
*/
async getDataTree (credendtials, objects) {
- return { status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a getDataTree method' }
+ return { error: 'NOT_IMPLEMENTED_EXCEPTION', message: 'The provided backend does not have a getDataTree method' }
}
/*
@@ -111,7 +110,7 @@ class ExternalBackends {
* return: { success: <boolean>, status: '<STATUS_CODE_IF_ERROR>', error: '<ERROR_MESSAGE>' }
*/
async checkConnection (backend) {
- return { success: false, status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a checkConnection method' }
+ return { success: false, error: 'NOT_IMPLEMENTED_EXCEPTION', message: 'The provided backend does not have a checkConnection method' }
}
/* Returns an empty array [] if the backends doesn't have such a function.
@@ -133,7 +132,7 @@ class ExternalBackends {
* }
*/
async addClient (credentials, client) {
- return { success: false, status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have an addClient method' }
+ return { success: false, error: 'NOT_IMPLEMENTED_EXCEPTION', message: 'The provided backend does not have an addClient method' }
}
/*
@@ -151,18 +150,18 @@ class ExternalBackends {
* }
*/
async updateClient (credentials, client) {
- return { success: false, status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have an updateClient method' }
+ return { success: false, error: 'NOT_IMPLEMENTED_EXCEPTION', message: 'The provided backend does not have an updateClient method' }
}
async uploadFiles (credentials, externalId, files) {
- return { success: false, status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have an uploadFiles method' }
+ return { success: false, error: 'NOT_IMPLEMENTED_EXCEPTION', message: 'The provided backend does not have an uploadFiles method' }
}
async getFileList (credentials, externalId) {
- return { success: false, status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have an getFileList method' }
+ return { success: false, error: 'NOT_IMPLEMENTED_EXCEPTION', message: 'The provided backend does not have an getFileList method' }
}
async getFile (credentials, externalId, filename) {
- return { success: false, status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have an getFile method' }
+ return { success: false, error: 'NOT_IMPLEMENTED_EXCEPTION', message: 'The provided backend does not have an getFile method' }
}
}