2020-11-17 07:04:44 +13:00
|
|
|
const { getRoutingInfo } = require("../../utilities/routing")
|
2020-11-19 04:12:42 +13:00
|
|
|
const {
|
2020-12-03 02:20:56 +13:00
|
|
|
getUserRoleHierarchy,
|
|
|
|
BUILTIN_ROLE_IDS,
|
2022-01-11 08:33:00 +13:00
|
|
|
} = require("@budibase/backend-core/roles")
|
2020-11-17 07:04:44 +13:00
|
|
|
|
2020-11-20 02:39:17 +13:00
|
|
|
const URL_SEPARATOR = "/"
|
|
|
|
|
|
|
|
function Routing() {
|
|
|
|
this.json = {}
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:31:09 +12:00
|
|
|
Routing.prototype.getTopLevel = function (fullpath) {
|
2020-11-20 02:39:17 +13:00
|
|
|
if (fullpath.charAt(0) !== URL_SEPARATOR) {
|
|
|
|
fullpath = URL_SEPARATOR + fullpath
|
|
|
|
}
|
|
|
|
// replace the first value with the home route
|
|
|
|
return URL_SEPARATOR + fullpath.split(URL_SEPARATOR)[1]
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:31:09 +12:00
|
|
|
Routing.prototype.getScreensProp = function (fullpath) {
|
2020-11-20 02:39:17 +13:00
|
|
|
const topLevel = this.getTopLevel(fullpath)
|
|
|
|
if (!this.json[topLevel]) {
|
|
|
|
this.json[topLevel] = {
|
|
|
|
subpaths: {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!this.json[topLevel].subpaths[fullpath]) {
|
|
|
|
this.json[topLevel].subpaths[fullpath] = {
|
|
|
|
screens: {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.json[topLevel].subpaths[fullpath].screens
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:31:09 +12:00
|
|
|
Routing.prototype.addScreenId = function (fullpath, roleId, screenId) {
|
2020-12-03 02:20:56 +13:00
|
|
|
this.getScreensProp(fullpath)[roleId] = screenId
|
2020-11-20 02:39:17 +13:00
|
|
|
}
|
|
|
|
|
2020-11-19 04:12:42 +13:00
|
|
|
/**
|
|
|
|
* Gets the full routing structure by querying the routing view and processing the result into the tree.
|
|
|
|
* @returns {Promise<object>} The routing structure, this is the full structure designed for use in the builder,
|
2020-12-03 02:20:56 +13:00
|
|
|
* if the client routing is required then the updateRoutingStructureForUserRole should be used.
|
2020-11-19 04:12:42 +13:00
|
|
|
*/
|
2022-01-28 13:05:39 +13:00
|
|
|
async function getRoutingStructure() {
|
|
|
|
const screenRoutes = await getRoutingInfo()
|
2020-11-20 02:39:17 +13:00
|
|
|
const routing = new Routing()
|
|
|
|
|
2020-11-18 07:12:21 +13:00
|
|
|
for (let screenRoute of screenRoutes) {
|
2020-11-20 02:39:17 +13:00
|
|
|
let fullpath = screenRoute.routing.route
|
2020-12-03 02:20:56 +13:00
|
|
|
const roleId = screenRoute.routing.roleId
|
|
|
|
routing.addScreenId(fullpath, roleId, screenRoute.id)
|
2020-11-18 07:12:21 +13:00
|
|
|
}
|
|
|
|
|
2020-11-20 02:39:17 +13:00
|
|
|
return { routes: routing.json }
|
2020-11-19 04:12:42 +13:00
|
|
|
}
|
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.fetch = async ctx => {
|
2022-01-28 13:05:39 +13:00
|
|
|
ctx.body = await getRoutingStructure()
|
2020-11-17 07:04:44 +13:00
|
|
|
}
|
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.clientFetch = async ctx => {
|
2022-01-29 04:43:51 +13:00
|
|
|
const routing = await getRoutingStructure()
|
2020-12-03 02:20:56 +13:00
|
|
|
let roleId = ctx.user.role._id
|
2022-01-28 07:18:31 +13:00
|
|
|
const roleIds = await getUserRoleHierarchy(roleId)
|
2020-11-20 02:39:17 +13:00
|
|
|
for (let topLevel of Object.values(routing.routes)) {
|
|
|
|
for (let subpathKey of Object.keys(topLevel.subpaths)) {
|
|
|
|
let found = false
|
|
|
|
const subpath = topLevel.subpaths[subpathKey]
|
2020-12-03 02:20:56 +13:00
|
|
|
const roleOptions = Object.keys(subpath.screens)
|
|
|
|
if (roleOptions.length === 1 && !roleOptions[0]) {
|
|
|
|
subpath.screenId = subpath.screens[roleOptions[0]]
|
|
|
|
subpath.roleId = BUILTIN_ROLE_IDS.BASIC
|
2020-11-20 02:39:17 +13:00
|
|
|
found = true
|
|
|
|
} else {
|
2020-12-03 02:20:56 +13:00
|
|
|
for (let roleId of roleIds) {
|
|
|
|
if (roleOptions.indexOf(roleId) !== -1) {
|
|
|
|
subpath.screenId = subpath.screens[roleId]
|
|
|
|
subpath.roleId = roleId
|
2020-11-20 02:39:17 +13:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete subpath.screens
|
|
|
|
if (!found) {
|
|
|
|
delete topLevel.subpaths[subpathKey]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.body = routing
|
2020-11-17 07:04:44 +13:00
|
|
|
}
|