Skip to content

Commit

Permalink
Pipeline roles and slugs API
Browse files Browse the repository at this point in the history
refs #5508
  • Loading branch information
halfdan committed Jul 2, 2015
1 parent 71e8bdf commit 7f5250a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 33 deletions.
63 changes: 46 additions & 17 deletions core/server/api/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ var Promise = require('bluebird'),
canThis = require('../permissions').canThis,
dataProvider = require('../models'),
errors = require('../errors'),
pipeline = require('../utils/pipeline'),
utils = require('./utils'),
docName = 'roles',

roles;

Expand All @@ -27,29 +30,55 @@ roles = {
* @returns {Promise(Roles)} Roles Collection
*/
browse: function browse(options) {
options = options || {};
var tasks;

return canThis(options.context).browse.role().then(function () {
return dataProvider.Role.findAll(options).then(function (results) {
var roles = results.map(function (r) {
return r.toJSON();
});
/**
* ### Handle Permissions
* We need to be an authorised user.
* @param {Object} options
* @returns {Object} options
*/
function handlePermissions(options) {
return canThis(options.context).browse.role().then(function () {
return options;
}).catch(function handleError(error) {
return errors.handleAPIError(error, 'You do not have permission to browse roles.');
});
}

/**
* ### Model Query
* Make the call to the Model layer
* @param {Object} options
* @returns {Object} options
*/
function modelQuery(options) {
return dataProvider.Role.findAll(options);
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName), 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) {
var roles = results.map(function (r) {
return r.toJSON();
});

if (options.permissions !== 'assign') {
return {roles: roles};
}
if (options.permissions !== 'assign') {
return {roles: roles};
}

return Promise.filter(roles.map(function (role) {
return canThis(options.context).assign.role(role)
return Promise.filter(roles.map(function (role) {
return canThis(options.context).assign.role(role)
.return(role)
.catch(function () {});
}), function (value) {
return value && value.name !== 'Owner';
}).then(function (roles) {
return {roles: roles};
});
}), function (value) {
return value && value.name !== 'Owner';
}).then(function (roles) {
return {roles: roles};
});
}).catch(errors.logAndThrowError);
});
}
};

Expand Down
53 changes: 37 additions & 16 deletions core/server/api/slugs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ var canThis = require('../permissions').canThis,
dataProvider = require('../models'),
errors = require('../errors'),
Promise = require('bluebird'),
pipeline = require('../utils/pipeline'),
utils = require('./utils'),
docName = 'slugs',

slugs,
allowedTypes;
Expand All @@ -23,7 +26,7 @@ slugs = {
* @returns {Promise(String)} Unique string
*/
generate: function (options) {
options = options || {};
var tasks;

// `allowedTypes` is used to define allowed slug types and map them against its model class counterpart
allowedTypes = {
Expand All @@ -33,27 +36,45 @@ slugs = {
app: dataProvider.App
};

return canThis(options.context).generate.slug().then(function () {
if (allowedTypes[options.type] === undefined) {
return Promise.reject(new errors.BadRequestError('Unknown slug type \'' + options.type + '\'.'));
}

return dataProvider.Base.Model.generateSlug(allowedTypes[options.type], options.name, {status: 'all'}).then(function (slug) {
if (!slug) {
return Promise.reject(new errors.InternalServerError('Could not generate slug.'));
/**
* ### Handle Permissions
* We need to be an authorized user and use an allowedType
* @param {Object} options
* @returns {Object} options
*/
function handlePermissions(options) {
return canThis(options.context).generate.slug().then(function () {
if (allowedTypes[options.type] === undefined) {
return Promise.reject(new errors.BadRequestError('Unknown slug type \'' + options.type + '\'.'));
}

return {slugs: [{slug: slug}]};
return options;
}).catch(function handleError(error) {
return errors.handleAPIError(error, 'You do not have permission to generate a slug.');
});
}).catch(function (err) {
if (err) {
return Promise.reject(err);
}

/**
* ### Model Query
* Make the call to the Model layer
* @param {Object} options
* @returns {Object} options
*/
function modelQuery(options) {
return dataProvider.Base.Model.generateSlug(allowedTypes[options.type], options.name, {status: 'all'});
}

// Push all of our tasks into a `tasks` array in the correct order
tasks = [utils.validate(docName), 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) {
if (!slug) {
return Promise.reject(new errors.InternalServerError('Could not generate slug.'));
}

return Promise.reject(new errors.NoPermissionError('You do not have permission to generate a slug.'));
return {slugs: [{slug: slug}]};
});
}

};

module.exports = slugs;

0 comments on commit 7f5250a

Please sign in to comment.