diff --git a/api/api.js b/api/api.js index 68f95144a..4b55fda9b 100644 --- a/api/api.js +++ b/api/api.js @@ -9,7 +9,7 @@ const auth = require('../mod/user/auth') const saml = process.env.SAML_ENTITY_ID && require('../mod/user/saml') const routes = { - layer: require('../mod/layer/_layer'), + mvt: require('../mod/mvt'), location: require('../mod/location/_location'), provider: require('../mod/provider/_provider'), query: require('../mod/query'), @@ -181,7 +181,7 @@ module.exports = async (req, res) => { // Layer route if (req.url.match(/(?<=\/api\/layer)/)) { - return routes.layer(req, res) + return routes.mvt(req, res) } // Location route diff --git a/mod/layer/_layer.js b/mod/layer/_layer.js deleted file mode 100644 index ef44d5242..000000000 --- a/mod/layer/_layer.js +++ /dev/null @@ -1,50 +0,0 @@ -const formats = { - mvt: require('./mvt') -} - -const workspaceCache = require('../workspace/cache') - -module.exports = async (req, res) => { - - const workspace = await workspaceCache() - - if (!req.params.layer) { - return res.send(`Failed to evaluate 'layer' param.

- Layer API`) - } - - // The format key must be an own property of the formats object. - if (!Object.hasOwn(formats, req.params.format)) { - return res.send(`Failed to evaluate 'format' param.

- Layer API`) - } - - if (req.params.locale) { - - // The locale key must be an own property of the workspace.locales, and must be an object. - if (Object.hasOwn(workspace.locales, req.params.locale) - && typeof workspace.locales[req.params.locale] === 'object') { - - // Assign layer from locale in workspace. - req.params.layer = workspace.locales[req.params.locale].layers[req.params.layer] - - } else { - - // Terminate request if locale is defined but not valid. - return res.send(`Failed to evaluate locale param.`) - } - - // A layer must be specified in the templates without a locale specifier, and must be an object - } else if (Object.hasOwn(workspace.templates, req.params.layer) - && typeof workspace.templates[req.params.layer] === 'object') { - - // Assign layer object from templates. - req.params.layer = workspace.templates[req.params.layer] - - } else { - - return res.send(`Failed to evaluate layer param.`) - } - - return formats[req.params.format](req, res) -} \ No newline at end of file diff --git a/mod/layer/mvt.js b/mod/mvt.js similarity index 90% rename from mod/layer/mvt.js rename to mod/mvt.js index 4efd33721..19c6544a2 100644 --- a/mod/layer/mvt.js +++ b/mod/mvt.js @@ -1,25 +1,29 @@ -const dbs = require('../utils/dbs')() +const dbs = require('./utils/dbs')() -const sqlFilter = require('../utils/sqlFilter') +const sqlFilter = require('./utils/sqlFilter') -const validateRequestParams = require('../utils/validateRequestParams') +const validateRequestParams = require('./utils/validateRequestParams') -const Roles = require('../utils/roles.js') +const Roles = require('./utils/roles.js') -const logger = require('../utils/logger') +const logger = require('./utils/logger') -const workspaceCache = require('../workspace/cache') +const workspaceCache = require('./workspace/cache') + +const getLayer = require('./workspace/getLayer') module.exports = async (req, res) => { const workspace = await workspaceCache() - // Check the layer.roles{} against the user.roles[] - const layer = Roles.check(req.params.layer, req.params.user?.roles) + const layer = await getLayer(req.params) - // The layer object did not pass the Roles.check() if (!layer) { - return res.status(403).send('Access prohibited.') + return res.status(403).send('Layer not found.') + } + + if (!Roles.check(layer, req.params.user?.roles)) { + return res.status(403).send('Role access denied for layer.') } // Validate URL parameter diff --git a/mod/query.js b/mod/query.js index 67484efed..84a6043a4 100644 --- a/mod/query.js +++ b/mod/query.js @@ -61,14 +61,10 @@ module.exports = async (req, res) => { return res.status(400).send('Layer not found.') } - let layer = await getLayer(req) + const layer = await getLayer(req.params) - // Get layer from locale. - layer = Roles.check(layer, req.params.user?.roles) - - if (!layer) { - - return res.status(403).send('Access prohibited.') + if (!Roles.check(layer, req.params.user?.roles)) { + return res.status(403).send('Role access denied for layer.') } // Set layer dbs as fallback param if not defined. diff --git a/mod/workspace/_workspace.js b/mod/workspace/_workspace.js index 89bac35d7..917ecf859 100644 --- a/mod/workspace/_workspace.js +++ b/mod/workspace/_workspace.js @@ -2,7 +2,7 @@ const clone = require('../utils/clone.js') const Roles = require('../utils/roles.js') -const _getLayer = require('./getLayer') +const getLayer = require('./getLayer') const workspaceCache = require('./cache') @@ -13,10 +13,10 @@ module.exports = async (req, res) => { workspace = await workspaceCache() const keys = { - layer: getLayer, - locale: getLocale, - locales: getLocales, - roles: getRoles, + layer, + locale, + locales, + roles, } // The keys object must own a user provided lookup key @@ -30,7 +30,7 @@ module.exports = async (req, res) => { return keys[req.params.key](req, res) } -async function getLayer(req, res) { +async function layer(req, res) { if (!Object.hasOwn(workspace.locales, req.params.locale)) { return res.status(400).send(`Unable to validate locale param.`) @@ -41,23 +41,23 @@ async function getLayer(req, res) { const roles = req.params.user?.roles || [] if (!Roles.check(locale, roles)) { - return res.status(403).send('Role access denied.') + return res.status(403).send('Role access denied for locale.') } if (!Object.hasOwn(locale.layers, req.params.layer)) { return res.status(400).send(`Unable to validate layer param.`) } - const layer = await _getLayer(req) + const layer = await getLayer(req.params) if (!Roles.check(layer, roles)) { - return res.status(403).send('Role access denied.') + return res.status(403).send('Role access denied for layer.') } res.json(layer) } -function getLocales(req, res) { +function locales(req, res) { const roles = req.params.user?.roles || [] @@ -71,7 +71,7 @@ function getLocales(req, res) { res.send(locales) } -function getLocale(req, res) { +function locale(req, res) { if (req.params.locale && !Object.hasOwn(workspace.locales, req.params.locale)) { return res.status(400).send(`Unable to validate locale param.`) @@ -102,7 +102,7 @@ function getLocale(req, res) { res.json(locale) } -function getRoles(req, res) { +function roles(req, res) { if (!workspace.locales) return res.send({}) diff --git a/mod/workspace/getLayer.js b/mod/workspace/getLayer.js index 7617f4eaa..575378c3f 100644 --- a/mod/workspace/getLayer.js +++ b/mod/workspace/getLayer.js @@ -6,30 +6,30 @@ const getTemplate = require('./getTemplate') const workspaceCache = require('./cache') -module.exports = async (req) => { +module.exports = async (params) => { const workspace = await workspaceCache() - if (!Object.hasOwn(workspace.locales, req.params.locale)) { + if (!Object.hasOwn(workspace.locales, params.locale)) { return new Error('Unable to validate locale param.') //400 } - const locale = workspace.locales[req.params.locale] + const locale = workspace.locales[params.locale] - const roles = req.params.user?.roles || [] + const roles = params.user?.roles || [] if (!Roles.check(locale, roles)) { return new Error('Role access denied.') //403 } - if (!Object.hasOwn(locale.layers, req.params.layer)) { + if (!Object.hasOwn(locale.layers, params.layer)) { return new Error('Unable to validate layer param.') //400 } - const layer = locale.layers[req.params.layer] + const layer = locale.layers[params.layer] // Assign key value as key on layer object. - layer.key ??= req.params.layer + layer.key ??= params.layer if (Object.hasOwn(workspace.templates, layer.template || layer.key)) { @@ -53,11 +53,5 @@ module.exports = async (req) => { // Assign layer key as name with no existing name on layer object. layer.name ??= layer.key - //const layer = clone(locale.layers[req.params.layer]) - - // if (!Roles.check(layer, roles)) { - // return res.status(403).send('Role access denied.') - // } - return layer } \ No newline at end of file