Skip to content

Commit

Permalink
remove layer folder; move mvt mod; update getLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
dbauszus-glx committed Oct 27, 2023
1 parent cafa265 commit 69c5363
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 94 deletions.
4 changes: 2 additions & 2 deletions api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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
Expand Down
50 changes: 0 additions & 50 deletions mod/layer/_layer.js

This file was deleted.

24 changes: 14 additions & 10 deletions mod/layer/mvt.js → mod/mvt.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 3 additions & 7 deletions mod/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 12 additions & 12 deletions mod/workspace/_workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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
Expand All @@ -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.`)
Expand All @@ -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 || []

Expand All @@ -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.`)
Expand Down Expand Up @@ -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({})

Expand Down
20 changes: 7 additions & 13 deletions mod/workspace/getLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {

Expand All @@ -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
}

0 comments on commit 69c5363

Please sign in to comment.