Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// __Dependencies__
var baucis = require('baucis');
var deco = require('deco');
var decorators = deco.require(__dirname, [ 'Controller', 'Api' ]).hash;
baucis.Controller.decorators(decorators.Controller);
baucis.Api.decorators(decorators.Api);
'use strict'
const debug = require('debug')('moonridge:baucis')
const baucis = require('baucis')
require('baucis-swagger')
const mapVerbToOperation = {
POST: 'create',
GET: 'read',
PUT: 'update',
DELETE: 'remove'
}
baucis.Controller.decorators(function (options, protect) {
var controller = this
const model = controller.model()
const mrOpts = model.moonridgeOpts
controller.request(function (request, response, next) {
// expects request.moonridge to be something like {user: {privilege_level: 30}}
debug(request.method)
const operation = mapVerbToOperation[request.method]
let errWhileCheckingPermissions
try {
mrOpts.checkPermission(request, operation)
} catch (err) {
errWhileCheckingPermissions = err
}
debug('errWhileCheckingPermissions ', errWhileCheckingPermissions)
if (errWhileCheckingPermissions) {
return response.status(403).send(baucis.Error.Forbidden(`You lack a privilege to ${request.method} ${model.modelName} collection`))