2014-07-23 10:13:20 +04:00
|
|
|
// # Roles API
|
|
|
|
// RESTful API for the Role resource
|
2017-09-12 18:31:14 +03:00
|
|
|
var Promise = require('bluebird'),
|
2017-12-14 00:20:02 +03:00
|
|
|
pipeline = require('../lib/promise/pipeline'),
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils = require('./utils'),
|
2017-09-12 18:31:14 +03:00
|
|
|
canThis = require('../permissions').canThis,
|
|
|
|
models = require('../models'),
|
|
|
|
docName = 'roles',
|
2014-07-15 19:22:06 +04:00
|
|
|
|
|
|
|
roles;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ## Roles API Methods
|
|
|
|
*
|
2017-12-12 00:47:46 +03:00
|
|
|
* **See:** [API Methods](index.js.html#api%20methods)
|
2014-07-15 19:22:06 +04:00
|
|
|
*/
|
|
|
|
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-01 21:17:56 +03:00
|
|
|
var permittedOptions = ['permissions'],
|
|
|
|
tasks;
|
2014-07-15 19:22:06 +04:00
|
|
|
|
2015-07-02 17:38:31 +03:00
|
|
|
/**
|
|
|
|
* ### Model Query
|
|
|
|
* Make the call to the Model layer
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Object} options
|
|
|
|
*/
|
|
|
|
function modelQuery(options) {
|
2017-09-27 11:31:41 +03:00
|
|
|
return models.Role.findAll(options)
|
|
|
|
.then(function onModelResponse(models) {
|
|
|
|
var roles = models.map(function (role) {
|
|
|
|
return role.toJSON();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (options.permissions !== 'assign') {
|
|
|
|
return {roles: roles};
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
2015-07-02 17:38:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push all of our tasks into a `tasks` array in the correct order
|
2015-07-01 21:17:56 +03:00
|
|
|
tasks = [
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils.validate(docName, {opts: permittedOptions}),
|
|
|
|
localUtils.handlePermissions(docName, 'browse'),
|
2015-07-01 21:17:56 +03:00
|
|
|
modelQuery
|
|
|
|
];
|
2015-07-02 17:38:31 +03:00
|
|
|
|
|
|
|
// Pipeline calls each task passing the result of one to be the arguments for the next
|
2017-09-27 11:31:41 +03:00
|
|
|
return pipeline(tasks, options);
|
2014-07-15 19:22:06 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = roles;
|