Skip to content

Commit

Permalink
Merge 1e1081a into 22d555d
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Jul 3, 2018
2 parents 22d555d + 1e1081a commit 32b9c15
Show file tree
Hide file tree
Showing 18 changed files with 665 additions and 1,801 deletions.
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.

76 changes: 16 additions & 60 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 @@ -194,66 +193,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
73 changes: 0 additions & 73 deletions dadi/lib/controller/hooks.js
Expand Up @@ -78,27 +78,6 @@ HooksController.prototype._writeHook = function (name, content) {
})
}

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

let name = req.params.hookName
let hook = this._findHooks(name)[0]

if (!hook) {
return help.sendBackJSON(404, res, next)(null)
}

this._deleteHook(name).then(() => {
return help.sendBackJSON(204, res, next)(null)
}).catch(err => {
return help.sendBackJSON(500, res, next)(err)
})
}

HooksController.prototype.get = function (req, res, next) {
if (!acl.client.isAdmin(req.dadiApiClient)) {
return help.sendBackJSON(null, res, next)(
Expand Down Expand Up @@ -140,56 +119,4 @@ HooksController.prototype.get = function (req, res, next) {
}
}

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

let name = req.params.hookName
let hook = this._findHooks(name)[0]

if (hook) {
return help.sendBackJSON(409, res, next)(null, {
err: 'Hook already exists'
})
}

return this._writeHook(name, req.body).then(() => {
return help.sendBackJSON(201, res, next)(null, {
success: true,
message: 'Hook created'
})
}).catch(err => {
return help.sendBackJSON(500, res, next)(err)
})
}

HooksController.prototype.put = function (req, res, next) {
return acl.access.get(req.dadiApiClient).then(access => {
if (access.update !== true) {
return help.sendBackJSON(null, res, next)(
acl.createError(req.dadiApiClient)
)
}

let name = req.params.hookName
let hook = this._findHooks(name)[0]

if (!hook) {
return help.sendBackJSON(404, res, next)(null)
}

return this._writeHook(name, req.body).then(() => {
return help.sendBackJSON(200, res, next)(null, {
success: true,
message: 'Hook updated'
})
}).catch(err => {
return help.sendBackJSON(500, res, next)(err)
})
})
}

module.exports = (server, hooksPath) => new HooksController(server, hooksPath)

0 comments on commit 32b9c15

Please sign in to comment.