backend/app/routing/middleware/api/DatabaseRoute.middleware.js
2020-03-01 15:37:52 -06:00

31 lines
1003 B
JavaScript

const Middleware = require('libflitter/middleware/Middleware')
/*
* DatabaseRoute Middleware
* -------------------------------------------------------------
* Put some description here!
*/
class DatabaseRoute extends Middleware {
static get services() {
return [...super.services, 'models']
}
async test(req, res, next, args = {}){
const Database = this.models.get('api:db:Database')
const id = req.params.database_id ? req.params.database_id : (req.query.database_id ? req.query.database_id : false)
if ( !id ) return res.status(400).message('Missing required: database_id').api()
const db = await Database.findOne({UUID: id})
if ( !db ) return res.status(404).message('Unable to find database with that ID.').api()
if ( !(await db.is_accessible_by(req.user)) ) return req.security.deny()
if ( !req.form ) req.form = {}
req.form.database = db
next()
}
}
module.exports = exports = DatabaseRoute