Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove config endpoints #449

Merged
merged 9 commits into from Jul 6, 2018
25 changes: 1 addition & 24 deletions dadi/lib/controller/apiConfig.js
@@ -1,13 +1,10 @@
const acl = require('./../model/acl')
const config = require('./../../../config')
const fs = require('fs-extra')
const help = require('./../help')
const path = require('path')

const ApiConfig = function (server) {
server.app.routeMethods('/api/config', {
get: this.get.bind(this),
post: this.post.bind(this)
get: this.get.bind(this)
})
}

Expand All @@ -21,24 +18,4 @@ ApiConfig.prototype.get = function (req, res, next) {
return help.sendBackJSON(200, res, next)(null, config.getProperties())
}

ApiConfig.prototype.post = function (req, res, next) {
if (!acl.client.isAdmin(req.dadiApiClient)) {
return help.sendBackJSON(null, res, next)(
acl.createError(req.dadiApiClient)
)
}

let configPath = path.resolve(config.configPath())
let newConfig = Object.assign({}, config.getProperties(), req.body || {})

return fs.writeJson(configPath, newConfig, {
spaces: 4
}).then(() => {
return help.sendBackJSON(200, res, next)(null, {
success: true,
message: 'Server restart required'
})
})
}

module.exports = server => new ApiConfig(server)
84 changes: 0 additions & 84 deletions dadi/lib/controller/createCollection.js

This file was deleted.

47 changes: 0 additions & 47 deletions dadi/lib/controller/createEndpoint.js

This file was deleted.

83 changes: 20 additions & 63 deletions dadi/lib/controller/documents.js
Expand Up @@ -2,7 +2,6 @@ const acl = require('./../model/acl')
const config = require('./../../../config')
const Controller = require('./index')
const debug = require('debug')('api:controller')
const fs = require('fs')
const help = require('./../help')
const url = require('url')

Expand Down Expand Up @@ -84,10 +83,11 @@ Collection.prototype.delete = function (req, res, next) {
Collection.prototype.get = function (req, res, next) {
let path = url.parse(req.url, true)
let options = path.query
let callback = options.callback || this.model.settings.callback

// determine if this is jsonp
let done = options.callback
? help.sendBackJSONP(options.callback, res, next)
// Determine if this is JSONP.
let done = callback
? help.sendBackJSONP(callback, res, next)
: help.sendBackJSON(200, res, next)
let query = this._prepareQuery(req)
let queryOptions = this._prepareQueryOptions(options)
Expand Down Expand Up @@ -194,66 +194,23 @@ Collection.prototype.registerRoutes = function (route, filePath) {

let method = req.method && req.method.toLowerCase()

switch (method) {
case 'get':
// The client can read the schema if they have any type of access (i.e. create,
// delete, read or update) to the collection resource.
let aclKey = this.model.aclKey

return acl.access.get(req.dadiApiClient, aclKey).then(access => {
if (!access.create || !access.delete || !access.read || !access.update) {
return help.sendBackJSON(401, res, next)(
new Error('UNAUTHORISED')
)
}

return help.sendBackJSON(200, res, next)(null, require(filePath))
})

case 'delete':
// A client can delete the collection schema if they have root access.
if (!acl.client.isAdmin(req.dadiApiClient)) {
return help.sendBackJSON(null, res, next)(
acl.createError(req.dadiApiClient)
)
}

this.server.removeComponent(route)
this.unregisterRoutes(route)

return fs.unlink(filePath, err => {
help.sendBackJSON(200, res, next)(err, {
success: false,
message: `Collection deleted: ${this.model.name}`
})
})

case 'post':
// A client can update the collection schema if they have root access.
if (!acl.client.isAdmin(req.dadiApiClient)) {
return help.sendBackJSON(null, res, next)(
acl.createError(req.dadiApiClient)
)
}

let schema = typeof req.body === 'object'
? req.body
: JSON.parse(req.body)

schema.settings.lastModifiedAt = Date.now()

let payload = JSON.stringify(schema, null, 2)

return fs.writeFile(filePath, payload, err => {
help.sendBackJSON(200, res, next)(err, {
success: true,
message: `Collection updated: ${this.model.name}`
})
})

default:
next()
if (method !== 'get') {
return next()
}

// The client can read the schema if they have any type of access (i.e. create,
// delete, read or update) to the collection resource.
let aclKey = this.model.aclKey

return acl.access.get(req.dadiApiClient, aclKey).then(access => {
if (!access.create || !access.delete || !access.read || !access.update) {
return help.sendBackJSON(401, res, next)(
new Error('UNAUTHORISED')
)
}

return help.sendBackJSON(200, res, next)(null, require(filePath))
})
})

// Creating generic route.
Expand Down
30 changes: 0 additions & 30 deletions dadi/lib/controller/endpoint.js
@@ -1,5 +1,4 @@
const acl = require('./../model/acl')
const fs = require('fs')
const help = require('./../help')

const Endpoint = function (component, server, aclKey) {
Expand Down Expand Up @@ -51,35 +50,6 @@ Endpoint.prototype.isAuthenticated = function () {
}

Endpoint.prototype.registerRoutes = function (route, filePath) {
// Creating config route.
this.server.app.use(`${route}/config`, (req, res, next) => {
if (!filePath) {
return next()
}

let method = req.method && req.method.toLowerCase()

if (method !== 'post') {
return next()
}

if (!acl.client.isAdmin(req.dadiApiClient)) {
return help.sendBackJSON(null, res, next)(
acl.createError(req.dadiApiClient)
)
}

return fs.writeFile(filePath, req.body, err => {
if (err) return next(err)

help.sendBackJSON(200, res, next)(null, {
success: true,
message: 'Endpoint updated'
})
})
})

// Creating generic route.
this.server.app.use(route, (req, res, next) => {
try {
// Map request method to controller method.
Expand Down