summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorChristian Hofmaier2018-08-06 02:47:05 +0200
committerChristian Hofmaier2018-08-06 02:47:05 +0200
commit5a183cecd7101505e3cb0a60317ed810419b7e4e (patch)
treeaef41cba52160a18966930d5c16896edaa0e33ac /server
parent[webapp/searchtable] bugfix (diff)
downloadbas-5a183cecd7101505e3cb0a60317ed810419b7e4e.tar.gz
bas-5a183cecd7101505e3cb0a60317ed810419b7e4e.tar.xz
bas-5a183cecd7101505e3cb0a60317ed810419b7e4e.zip
language tags and function comments
Diffstat (limited to 'server')
-rw-r--r--server/api/permissions.js29
-rw-r--r--server/api/users.js19
-rw-r--r--server/app.js1
-rw-r--r--server/lib/permissions/index.js16
4 files changed, 62 insertions, 3 deletions
diff --git a/server/api/permissions.js b/server/api/permissions.js
index ef7c5e8..5ed09e0 100644
--- a/server/api/permissions.js
+++ b/server/api/permissions.js
@@ -2,7 +2,14 @@
var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
+// GET requests
module.exports.get = {
+
+ /*
+ * ?id=<ROLE_ID>
+ *
+ * @return: Returns the information about a role and it's permissions and groups.
+ */
getRoleById: function (req, res) {
db.role.findOne({ where: { id: req.query.id }, include: ['permissions', 'groups'] }).then(role => {
if (role) res.send(role)
@@ -10,6 +17,9 @@ module.exports.get = {
})
},
+ /*
+ * @return: Returns a list of all roles in the database.
+ */
getRoleList: function (req, res) {
db.role.findAll({
attributes: ['id', 'name', 'descr']
@@ -18,6 +28,9 @@ module.exports.get = {
})
},
+ /*
+ * @return: Returns a list of all permissions in the database.
+ */
getPermissionList: function (req, res) {
db.permission.findAll().then(function (permissions) {
res.status(200).send(permissions)
@@ -25,7 +38,14 @@ module.exports.get = {
}
}
+// POST requests
module.exports.post = {
+
+ /*
+ * id: <ROLE_ID>
+ *
+ * Deletes the role to the given id.
+ */
deleteRoles: function (req, res) {
const roleIds = req.body.id
@@ -34,6 +54,15 @@ module.exports.post = {
})
},
+ /*
+ * id: <ROLE_ID>
+ * name: <ROLE_NAME>
+ * descr: <ROLE_DESCRIPTION>
+ * permissions: <PERMISSION_IDS>
+ * groups: <GROUP_IDS>
+ *
+ * Creates or updates a role.
+ */
saveRole: function (req, res) {
const role = req.body
diff --git a/server/api/users.js b/server/api/users.js
index 2af6cb6..deb69e1 100644
--- a/server/api/users.js
+++ b/server/api/users.js
@@ -4,7 +4,9 @@ var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
var jwt = require('jsonwebtoken')
+// GET requests
module.exports.get = {
+
getUserInfo: function (req, res) {
// Because veryfyToken was succesfully excecuted the request has the attribute token.
const token = req.token
@@ -22,6 +24,9 @@ module.exports.get = {
})
},
+ /*
+ * @return: Returns a list of all users in the database and their given roles.
+ */
getUserList: function (req, res) {
db.user.findAll({
attributes: ['id', 'username', 'name'],
@@ -32,7 +37,15 @@ module.exports.get = {
}
}
+// POST requests
module.exports.post = {
+
+ /*
+ * roleIds: <ROLE_IDS>
+ * userIds: <USER_IDS>
+ *
+ * Adds the given roles to the given users in the database.
+ */
grantRoles: function (req, res) {
const roleIds = req.body.roleIds
const userIds = req.body.userIds
@@ -45,6 +58,12 @@ module.exports.post = {
})
},
+ /*
+ * roleIds: <ROLE_IDS>
+ * userIds: <USER_IDS>
+ *
+ * Removes the given roles from the given users in the database.
+ */
revokeRoles: function (req, res) {
const roleIds = req.body.roleIds
const userIds = req.body.userIds
diff --git a/server/app.js b/server/app.js
index 4f5e6dd..c9a270c 100644
--- a/server/app.js
+++ b/server/app.js
@@ -11,6 +11,7 @@ var app = express()
global.__appdir = __dirname
require('./lib/tftp')
+// Read permissions from JSON and update the database
require('./lib/permissions')
// ############################################################################
diff --git a/server/lib/permissions/index.js b/server/lib/permissions/index.js
index dafa4d1..12db24f 100644
--- a/server/lib/permissions/index.js
+++ b/server/lib/permissions/index.js
@@ -5,22 +5,32 @@ var db = require(path.join(__appdir, 'lib', 'sequelize'))
updatePermissionDatabase()
+ /*
+ * Update the permission-Database accordingly to the permission.json
+ */
function updatePermissionDatabase () {
var permissionNames = []
- // Insert / Update Entries in Database
+ // Insert / Update entries in Database which are in the permission.json
permissions.forEach(function (permission) {
permissionNames.push(permission.name)
upsert(db.permission, { name: permission.name, descr: permission.descr, groupdependent: permission.groupdependent }, { name: permission.name })
})
- // Delete Entries from Database
+ // Delete entries from Database which are not in the permission.json
db.permission.destroy(
{ where: { $not: { name: permissionNames } } }
)
}
-// Update or Insert function
+ /*
+ * model: <DB_MODEL>
+ * newItem: <DB_OBJECT>
+ * where: <DB_WHERECLAUSE>
+ *
+ * Updates or inserts the given newItem in the given model according to
+ * the where-clause.
+ */
function upsert (model, newItem, where) {
return model
.findOne({where: where})