Skip to content

Commit

Permalink
API Option Handling
Browse files Browse the repository at this point in the history
refs TryGhost#2758

- add a set of default options to utils
- update validation function to only pass through permitted options
- pass permitted options into validate where necessary
- setup basic validation for each known option, and generic validation for the remainder
- change slug to treat 'name' as data, rather than an option
  • Loading branch information
ErisDS committed Jul 14, 2015
1 parent 2bdaf77 commit e044136
Show file tree
Hide file tree
Showing 11 changed files with 459 additions and 186 deletions.
40 changes: 34 additions & 6 deletions core/server/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var Promise = require('bluebird'),
*
* **See:** [API Methods](index.js.html#api%20methods)
*/

posts = {
/**
* ## Browse
Expand All @@ -33,7 +34,9 @@ posts = {
* @returns {Promise<Posts>} Posts Collection with Meta
*/
browse: function browse(options) {
var tasks;
var extraOptions = ['tag', 'author', 'status', 'staticPages', 'featured'],
permittedOptions = utils.browseDefaultOptions.concat(extraOptions),
tasks;

/**
* ### Handle Permissions
Expand All @@ -60,7 +63,12 @@ posts = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName), handlePermissions, utils.convertOptions(allowedIncludes), modelQuery];
tasks = [
utils.validate(docName, {opts: permittedOptions}),
handlePermissions,
utils.convertOptions(allowedIncludes),
modelQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, options);
Expand Down Expand Up @@ -102,7 +110,12 @@ posts = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName, attrs), handlePermissions, utils.convertOptions(allowedIncludes), modelQuery];
tasks = [
utils.validate(docName, {attrs: attrs}),
handlePermissions,
utils.convertOptions(allowedIncludes),
modelQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, options).then(function formatResponse(result) {
Expand Down Expand Up @@ -152,7 +165,12 @@ posts = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName), handlePermissions, utils.convertOptions(allowedIncludes), modelQuery];
tasks = [
utils.validate(docName, {opts: utils.idDefaultOptions}),
handlePermissions,
utils.convertOptions(allowedIncludes),
modelQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, object, options).then(function formatResponse(result) {
Expand Down Expand Up @@ -208,7 +226,12 @@ posts = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName), handlePermissions, utils.convertOptions(allowedIncludes), modelQuery];
tasks = [
utils.validate(docName),
handlePermissions,
utils.convertOptions(allowedIncludes),
modelQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, object, options).then(function formatResponse(result) {
Expand Down Expand Up @@ -263,7 +286,12 @@ posts = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName), handlePermissions, utils.convertOptions(allowedIncludes), modelQuery];
tasks = [
utils.validate(docName, {opts: utils.idDefaultOptions}),
handlePermissions,
utils.convertOptions(allowedIncludes),
modelQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, options).then(function formatResponse(result) {
Expand Down
9 changes: 7 additions & 2 deletions core/server/api/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ roles = {
* @returns {Promise(Roles)} Roles Collection
*/
browse: function browse(options) {
var tasks;
var permittedOptions = ['permissions'],
tasks;

/**
* ### Handle Permissions
Expand All @@ -57,7 +58,11 @@ roles = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName), handlePermissions, modelQuery];
tasks = [
utils.validate(docName, {opts: permittedOptions}),
handlePermissions,
modelQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, options).then(function formatResponse(results) {
Expand Down
12 changes: 9 additions & 3 deletions core/server/api/slugs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ slugs = {
* @returns {Promise(String)} Unique string
*/
generate: function (options) {
var tasks;
var opts = ['type'],
attrs = ['name'],
tasks;

// `allowedTypes` is used to define allowed slug types and map them against its model class counterpart
allowedTypes = {
Expand Down Expand Up @@ -60,11 +62,15 @@ slugs = {
* @returns {Object} options
*/
function modelQuery(options) {
return dataProvider.Base.Model.generateSlug(allowedTypes[options.type], options.name, {status: 'all'});
return dataProvider.Base.Model.generateSlug(allowedTypes[options.type], options.data.name, {status: 'all'});
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName), handlePermissions, modelQuery];
tasks = [
utils.validate(docName, {opts: opts, attrs: attrs}),
handlePermissions,
modelQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, options).then(function (slug) {
Expand Down
35 changes: 30 additions & 5 deletions core/server/api/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ tags = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName), handlePermissions, utils.convertOptions(allowedIncludes), doQuery];
tasks = [
utils.validate(docName, {opts: utils.browseDefaultOptions}),
handlePermissions,
utils.convertOptions(allowedIncludes),
doQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, options);
Expand Down Expand Up @@ -91,7 +96,12 @@ tags = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName, attrs), handlePermissions, utils.convertOptions(allowedIncludes), doQuery];
tasks = [
utils.validate(docName, {attrs: attrs}),
handlePermissions,
utils.convertOptions(allowedIncludes),
doQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, options).then(function formatResponse(result) {
Expand Down Expand Up @@ -136,7 +146,12 @@ tags = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName), handlePermissions, utils.convertOptions(allowedIncludes), doQuery];
tasks = [
utils.validate(docName),
handlePermissions,
utils.convertOptions(allowedIncludes),
doQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, object, options).then(function formatResponse(result) {
Expand Down Expand Up @@ -181,7 +196,12 @@ tags = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName), handlePermissions, utils.convertOptions(allowedIncludes), doQuery];
tasks = [
utils.validate(docName, {opts: utils.idDefaultOptions}),
handlePermissions,
utils.convertOptions(allowedIncludes),
doQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, object, options).then(function formatResponse(result) {
Expand Down Expand Up @@ -234,7 +254,12 @@ tags = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName), handlePermissions, utils.convertOptions(allowedIncludes), doQuery];
tasks = [
utils.validate(docName, {opts: utils.idDefaultOptions}),
handlePermissions,
utils.convertOptions(allowedIncludes),
doQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, options);
Expand Down
77 changes: 51 additions & 26 deletions core/server/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ users = {
* @returns {Promise<Users>} Users Collection
*/
browse: function browse(options) {
var tasks;
var extraOptions = ['role', 'status'],
permittedOptions = utils.browseDefaultOptions.concat(extraOptions),
tasks;

/**
* ### Handle Permissions
Expand All @@ -100,7 +102,12 @@ users = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName), handlePermissions, utils.convertOptions(allowedIncludes), doQuery];
tasks = [
utils.validate(docName, {opts: permittedOptions}),
handlePermissions,
utils.convertOptions(allowedIncludes),
doQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, options);
Expand All @@ -112,7 +119,7 @@ users = {
* @returns {Promise<Users>} User
*/
read: function read(options) {
var attrs = ['id', 'slug', 'status', 'email'],
var attrs = ['id', 'slug', 'status', 'email', 'role'],
tasks;

/**
Expand Down Expand Up @@ -140,7 +147,12 @@ users = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName, attrs), handlePermissions, utils.convertOptions(allowedIncludes), doQuery];
tasks = [
utils.validate(docName, {attrs: attrs}),
handlePermissions,
utils.convertOptions(allowedIncludes),
doQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, options).then(function formatResponse(result) {
Expand All @@ -159,24 +171,12 @@ users = {
* @returns {Promise<User>}
*/
edit: function edit(object, options) {
var tasks;
/**
* ### Validate
* Special validation which handles roles
* @param {Post} object
* @param {Object} options
* @returns {Object} options
*/
function validate(object, options) {
options = options || {};
return utils.checkObject(object, docName, options.id).then(function (data) {
if (data.users[0].roles && data.users[0].roles[0]) {
options.editRoles = true;
}
var extraOptions = ['editRoles'],
permittedOptions = extraOptions.concat(utils.idDefaultOptions),
tasks;

options.data = data;
return options;
});
if (object.users && object.users[0] && object.users[0].roles && object.users[0].roles[0]) {
options.editRoles = true;
}

/**
Expand Down Expand Up @@ -245,7 +245,12 @@ users = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [validate, handlePermissions, utils.convertOptions(allowedIncludes), doQuery];
tasks = [
utils.validate(docName, {opts: permittedOptions}),
handlePermissions,
utils.convertOptions(allowedIncludes),
doQuery
];

return pipeline(tasks, object, options).then(function formatResponse(result) {
if (result) {
Expand Down Expand Up @@ -359,7 +364,12 @@ users = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName), handlePermissions, utils.convertOptions(allowedIncludes), doQuery];
tasks = [
utils.validate(docName),
handlePermissions,
utils.convertOptions(allowedIncludes),
doQuery
];

return pipeline(tasks, object, options);
},
Expand Down Expand Up @@ -420,7 +430,12 @@ users = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName), handlePermissions, utils.convertOptions(allowedIncludes), doQuery];
tasks = [
utils.validate(docName, {opts: utils.idDefaultOptions}),
handlePermissions,
utils.convertOptions(allowedIncludes),
doQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, options);
Expand Down Expand Up @@ -463,7 +478,12 @@ users = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate('password'), handlePermissions, utils.convertOptions(allowedIncludes), doQuery];
tasks = [
utils.validate('password'),
handlePermissions,
utils.convertOptions(allowedIncludes),
doQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, object, options).then(function formatResponse() {
Expand Down Expand Up @@ -507,7 +527,12 @@ users = {
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate('owner'), handlePermissions, utils.convertOptions(allowedIncludes), doQuery];
tasks = [
utils.validate('owner'),
handlePermissions,
utils.convertOptions(allowedIncludes),
doQuery
];

// Pipeline calls each task passing the result of one to be the arguments for the next
return pipeline(tasks, object, options).then(function formatResult(result) {
Expand Down
Loading

0 comments on commit e044136

Please sign in to comment.