2021-02-06 04:58:25 +13:00
|
|
|
const {
|
|
|
|
BUILTIN_PERMISSIONS,
|
|
|
|
PermissionLevels,
|
|
|
|
} = require("../../utilities/security/permissions")
|
2021-02-06 07:46:15 +13:00
|
|
|
const { getRoleParams } = require("../../db/utils")
|
|
|
|
const CouchDB = require("../../db")
|
2020-12-03 06:08:25 +13:00
|
|
|
|
2021-02-06 07:46:15 +13:00
|
|
|
async function updatePermissionOnRole(
|
|
|
|
appId,
|
|
|
|
roleId,
|
|
|
|
permissions,
|
|
|
|
remove = false
|
|
|
|
) {
|
|
|
|
const db = new CouchDB(appId)
|
|
|
|
const body = await db.allDocs(
|
|
|
|
getRoleParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
const dbRoles = body.rows.map(row => row.doc)
|
|
|
|
const docUpdates = []
|
2021-02-06 04:58:25 +13:00
|
|
|
|
2021-02-06 07:46:15 +13:00
|
|
|
// now try to find any roles which need updated, e.g. removing the
|
|
|
|
// resource from another role and then adding to the new role
|
|
|
|
for (let role of dbRoles) {
|
|
|
|
if (role.permissions) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: NEED TO WORK THIS PART OUT
|
|
|
|
return await db.bulkDocs(docUpdates)
|
2021-02-06 04:58:25 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.fetchBuiltin = function(ctx) {
|
2020-12-03 06:08:25 +13:00
|
|
|
ctx.body = Object.values(BUILTIN_PERMISSIONS)
|
|
|
|
}
|
2021-02-06 04:58:25 +13:00
|
|
|
|
|
|
|
exports.fetchLevels = function(ctx) {
|
|
|
|
ctx.body = Object.values(PermissionLevels)
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.addPermission = async function(ctx) {
|
2021-02-06 07:46:15 +13:00
|
|
|
const appId = ctx.appId,
|
|
|
|
roleId = ctx.params.roleId,
|
|
|
|
resourceId = ctx.params.resourceId
|
|
|
|
ctx.body = await updatePermissionOnRole(appId, roleId, resourceId)
|
2021-02-06 04:58:25 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.removePermission = async function(ctx) {
|
2021-02-06 07:46:15 +13:00
|
|
|
const appId = ctx.appId,
|
|
|
|
roleId = ctx.params.roleId,
|
|
|
|
resourceId = ctx.params.resourceId
|
|
|
|
ctx.body = await updatePermissionOnRole(appId, roleId, resourceId, true)
|
2021-02-06 04:58:25 +13:00
|
|
|
}
|