summaryrefslogtreecommitdiffstats
path: root/server/lib
diff options
context:
space:
mode:
authorJannik Schönartz2019-03-07 20:20:25 +0100
committerJannik Schönartz2019-03-07 20:20:25 +0100
commit0999302d99156200ff174d5ec56d6831c8afd332 (patch)
tree56dfcf175c3d5b15dd44eb8d156e4448caaa5455 /server/lib
parent[server/ipranges] Forgot to commit the lib. ¯\_(ツ)_/¯ (diff)
downloadbas-0999302d99156200ff174d5ec56d6831c8afd332.tar.gz
bas-0999302d99156200ff174d5ec56d6831c8afd332.tar.xz
bas-0999302d99156200ff174d5ec56d6831c8afd332.zip
[server] New clients are automaticly added to the groups of the fitting subranges
Add conflict models Sequelize string operators depricated fix IPv4 is now saved as decimal in the database Add host to config instead of hardcoding Rename ip.js lib to iphelper.js
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/external-backends/backendhelper.js21
-rw-r--r--server/lib/external-backends/backends/idoit-backend.js4
-rw-r--r--server/lib/ip.js21
-rw-r--r--server/lib/iphelper.js62
-rw-r--r--server/lib/permissions/index.js2
-rw-r--r--server/lib/permissions/permissionutil.js2
-rw-r--r--server/lib/sequelize.js8
7 files changed, 91 insertions, 29 deletions
diff --git a/server/lib/external-backends/backendhelper.js b/server/lib/external-backends/backendhelper.js
index dc4324c..752ccf7 100644
--- a/server/lib/external-backends/backendhelper.js
+++ b/server/lib/external-backends/backendhelper.js
@@ -28,9 +28,24 @@ module.exports = {
}
// Convert the parent group id to the external backend parentId.
- if (client.parentId) {
- var element = backend.mappedGroups.find(x => x.id === parseInt(client.parentId))
- if (element) tmpClient['parentId'] = element.backend_x_group.externalId
+ // if (client.parentId) {
+ if (client.parents) {
+ var elements = backend.mappedGroups.filter(x => client.parents.includes(x.id))
+ if (elements.length > 1) {
+ // TODO ADD MERGE CONFLICT
+ const conflict = await db.conflict.create({ description: 'Multiple parents found' })
+
+ // Add backend to the conflict.
+ conflict.createObject({ objectType: 'BACKEND', objectId: backend.id })
+
+ // Add the groups to the conflict.
+ for (let element of elements) {
+ conflict.createObject({ objectType: 'GROUP', objectId: element.id })
+ }
+ } else if (elements.length === 1) tmpClient['parentId'] = elements[0].backend_x_group.externalId
+
+ // var element = backend.mappedGroups.find(x => x.id === parseInt(client.parentId))
+ // if (element) tmpClient['parentId'] = element.backend_x_group.externalId
}
var addClient = await instance.addClient(backend.credentials, tmpClient)
diff --git a/server/lib/external-backends/backends/idoit-backend.js b/server/lib/external-backends/backends/idoit-backend.js
index 81a955b..43ab434 100644
--- a/server/lib/external-backends/backends/idoit-backend.js
+++ b/server/lib/external-backends/backends/idoit-backend.js
@@ -659,9 +659,9 @@ class IdoitBackend extends ExternalBackends {
}
}
try {
- var requestUpdate = await axios.post(c.url, bodies, config)
+ var requestUpdate = await axios.post(c.url, bodies, config)
} catch (error) {
- console.log(error)
+ console.log(error)
}
// 10 is the idoit object id for clients.
var result = {
diff --git a/server/lib/ip.js b/server/lib/ip.js
deleted file mode 100644
index 87c2eb5..0000000
--- a/server/lib/ip.js
+++ /dev/null
@@ -1,21 +0,0 @@
-module.exports = { toInt, toString, isIpv4 }
-
-// Takes an ip address and converts it to an integer.
-function toInt(ipString) {
- const ipArray = ipString.split('.')
- if (ipArray.length !== 4) return false
-
- let result = parseInt(ipArray[0]) * 256**3 + parseInt(ipArray[1]) * 256**2 + parseInt(ipArray[2]) * 256 + parseInt(ipArray[3])
- return result
-}
-
-// Converts an integer value in a human readable typical ip address.
-function toString(ipInt) {
- return [(ipInt>>24)&0xff, (ipInt>>16)&0xff, (ipInt>>8)&0xff, ipInt&0xff].join('.');
-}
-
-// Sanity check for an ipv4.
-function isIpv4(ipString) {
- const re = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
- return re.test(ipString)
-} \ No newline at end of file
diff --git a/server/lib/iphelper.js b/server/lib/iphelper.js
new file mode 100644
index 0000000..5c40313
--- /dev/null
+++ b/server/lib/iphelper.js
@@ -0,0 +1,62 @@
+/* global __appdir */
+var path = require('path')
+var db = require(path.join(__appdir, 'lib', 'sequelize'))
+module.exports = { toDecimal, toString, toIPv4, getGroups }
+
+// Finds the groups where the ip fits best in the subnets.
+async function getGroups (ipString) {
+ const ipInt = toDecimal(ipString)
+
+ let fittingIpRanges = await db.iprange.findAll({ where: { startIp: { [db.Op.lte]: ipInt }, endIp: { [db.Op.gte]: ipInt } } })
+ fittingIpRanges = fittingIpRanges.map(x => x.groupId)
+
+ // List with all groups mapped so that groups[id] = [parents]
+ let groups = await db.group.findAll({ include: ['parents'] })
+ let parentMap = {}
+ groups.forEach(group => {
+ parentMap[group.id] = group.parents
+ })
+
+ // foreach fittingIpRanges go through groups and eliminate parents in the fittingIpRanges
+ let result = fittingIpRanges
+ fittingIpRanges.forEach(groupid => {
+ result = eliminateParents(groupid, parentMap, result)
+ })
+
+ return result
+}
+
+function eliminateParents (groupId, groupMap, eliminateArray, alreadyChecked = []) {
+ // Check for cycles and return if there is one.
+ if (alreadyChecked.includes(groupId)) return eliminateArray
+ alreadyChecked.push(groupId)
+
+ const parents = groupMap[groupId]
+ parents.forEach(parent => {
+ const index = eliminateArray.indexOf(parent.id)
+ if (index !== -1) eliminateArray.splice(index, 1)
+ if (groupMap[parent.id].length > 0) eliminateArray = eliminateParents(parent.id, groupMap, eliminateArray, alreadyChecked)
+ })
+ return eliminateArray
+}
+
+// Takes an ip address and converts it to an integer.
+function toDecimal (ipString) {
+ if (isIPv4(ipString)) return false
+ const ipArray = ipString.split('.')
+ if (ipArray.length !== 4) return false
+
+ let result = parseInt(ipArray[0]) * 256 ** 3 + parseInt(ipArray[1]) * 256 ** 2 + parseInt(ipArray[2]) * 256 + parseInt(ipArray[3])
+ return result
+}
+
+// Converts an integer value in a human readable typical ip address.
+function toIPv4 (ipInt) {
+ return [(ipInt >> 24) & 0xff, (ipInt >> 16) & 0xff, (ipInt >> 8) & 0xff, ipInt & 0xff].join('.')
+}
+
+// Sanity check for an ipv4.
+function isIPv4 (ipString) {
+ const re = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
+ return re.test(ipString)
+}
diff --git a/server/lib/permissions/index.js b/server/lib/permissions/index.js
index 79ce6b1..a0af9d4 100644
--- a/server/lib/permissions/index.js
+++ b/server/lib/permissions/index.js
@@ -19,7 +19,7 @@ function updatePermissionDatabase () {
// Delete entries from Database which are not in the permission.json
db.permission.destroy(
- { where: { $not: { name: permissionNames } } }
+ { where: { [db.Op.not]: { name: permissionNames } } }
)
}
diff --git a/server/lib/permissions/permissionutil.js b/server/lib/permissions/permissionutil.js
index 532af6c..8e91af9 100644
--- a/server/lib/permissions/permissionutil.js
+++ b/server/lib/permissions/permissionutil.js
@@ -31,7 +31,7 @@ async function hasPermission (userid, permissionName) {
var user
if (permission[1] === '*') {
user = await db.user.findOne({
- where: { id: userid, '$roles.permissions.name$': { $like: permission[0] + '%' } },
+ where: { id: userid, '$roles.permissions.name$': { [db.Op.like]: permission[0] + '%' } },
include: [{ as: 'roles', model: db.role, include: ['permissions'] }]
})
} else {
diff --git a/server/lib/sequelize.js b/server/lib/sequelize.js
index 3624723..b6b99dd 100644
--- a/server/lib/sequelize.js
+++ b/server/lib/sequelize.js
@@ -7,7 +7,12 @@ var Sequelize = require('sequelize')
var config = require(path.join(__appdir, 'config', 'database'))
var db = {}
-var sequelize = new Sequelize(config.database, config.username, config.password, config)
+var sequelize = new Sequelize(config.database, config.username, config.password, {
+ host: config.host,
+ dialect: config.dialect,
+ operatorsAliases: false
+})
+const Op = Sequelize.Op
fs
.readdirSync(path.join(__dirname, '/../models'))
@@ -24,5 +29,6 @@ Object.keys(db).forEach(modelName => {
db.sequelize = sequelize
db.Sequelize = Sequelize
+db.Op = Op
module.exports = db