2014-07-23 10:13:20 +04:00
|
|
|
// # Roles API
|
|
|
|
// RESTful API for the Role resource
|
2014-08-17 10:17:23 +04:00
|
|
|
var Promise = require('bluebird'),
|
2014-07-15 19:22:06 +04:00
|
|
|
canThis = require('../permissions').canThis,
|
|
|
|
dataProvider = require('../models'),
|
|
|
|
errors = require('../errors'),
|
2015-07-02 17:38:31 +03:00
|
|
|
pipeline = require('../utils/pipeline'),
|
|
|
|
utils = require('./utils'),
|
|
|
|
docName = 'roles',
|
2014-07-15 19:22:06 +04:00
|
|
|
|
|
|
|
roles;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ## Roles API Methods
|
|
|
|
*
|
|
|
|
* **See:** [API Methods](index.js.html#api%20methods)
|
|
|
|
*/
|
|
|
|
roles = {
|
|
|
|
/**
|
|
|
|
* ### Browse
|
|
|
|
* Find all roles
|
|
|
|
*
|
2014-07-23 10:13:20 +04:00
|
|
|
* If a 'permissions' property is passed in the options object then
|
|
|
|
* the results will be filtered based on whether or not the context user has the given
|
|
|
|
* permission on a role.
|
2014-07-15 19:22:06 +04:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* @public
|
2014-07-23 10:13:20 +04:00
|
|
|
* @param {{context, permissions}} options (optional)
|
2014-07-15 19:22:06 +04:00
|
|
|
* @returns {Promise(Roles)} Roles Collection
|
|
|
|
*/
|
|
|
|
browse: function browse(options) {
|
2015-07-02 17:38:31 +03:00
|
|
|
var tasks;
|
2014-07-15 19:22:06 +04:00
|
|
|
|
2015-07-02 17:38:31 +03:00
|
|
|
/**
|
|
|
|
* ### 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();
|
|
|
|
});
|
2014-07-15 19:22:06 +04:00
|
|
|
|
2015-07-02 17:38:31 +03:00
|
|
|
if (options.permissions !== 'assign') {
|
|
|
|
return {roles: roles};
|
|
|
|
}
|
2015-05-11 04:48:32 +03:00
|
|
|
|
2015-07-02 17:38:31 +03:00
|
|
|
return Promise.filter(roles.map(function (role) {
|
|
|
|
return canThis(options.context).assign.role(role)
|
2015-05-11 04:48:32 +03:00
|
|
|
.return(role)
|
|
|
|
.catch(function () {});
|
2015-07-02 17:38:31 +03:00
|
|
|
}), function (value) {
|
|
|
|
return value && value.name !== 'Owner';
|
|
|
|
}).then(function (roles) {
|
|
|
|
return {roles: roles};
|
2014-07-15 19:22:06 +04:00
|
|
|
});
|
2015-07-02 17:38:31 +03:00
|
|
|
});
|
2014-07-15 19:22:06 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = roles;
|