2014-07-15 19:22:06 +04:00
|
|
|
var _ = require('lodash'),
|
|
|
|
errors = require('../errors'),
|
|
|
|
ghostBookshelf = require('./base'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2014-02-19 17:57:26 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
Role,
|
|
|
|
Roles;
|
|
|
|
|
2013-09-23 02:20:08 +04:00
|
|
|
Role = ghostBookshelf.Model.extend({
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2013-09-14 23:01:46 +04:00
|
|
|
tableName: 'roles',
|
2013-08-25 14:49:31 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
users: function () {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.belongsToMany('User');
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
permissions: function () {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.belongsToMany('Permission');
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2014-05-06 05:45:08 +04:00
|
|
|
}, {
|
|
|
|
/**
|
|
|
|
* Returns an array of keys permitted in a method's `options` hash, depending on the current method.
|
|
|
|
* @param {String} methodName The name of the method to check valid options for.
|
|
|
|
* @return {Array} Keys allowed in the `options` hash of the model's method.
|
|
|
|
*/
|
|
|
|
permittedOptions: function (methodName) {
|
|
|
|
var options = ghostBookshelf.Model.permittedOptions(),
|
|
|
|
|
|
|
|
// whitelists for the `options` hash argument on methods, by method name.
|
|
|
|
// these are the only options that can be passed to Bookshelf / Knex.
|
|
|
|
validOptions = {
|
2014-07-15 15:03:12 +04:00
|
|
|
findOne: ['withRelated']
|
2014-05-06 05:45:08 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
if (validOptions[methodName]) {
|
|
|
|
options = options.concat(validOptions[methodName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
2014-07-15 19:22:06 +04:00
|
|
|
},
|
|
|
|
|
2014-07-23 22:17:29 +04:00
|
|
|
permissible: function (roleModelOrId, action, context, loadedPermissions, hasUserPermission, hasAppPermission) {
|
2014-07-15 19:22:06 +04:00
|
|
|
var self = this,
|
|
|
|
checkAgainst = [],
|
|
|
|
origArgs;
|
|
|
|
|
|
|
|
// If we passed in an id instead of a model, get the model
|
|
|
|
// then check the permissions
|
|
|
|
if (_.isNumber(roleModelOrId) || _.isString(roleModelOrId)) {
|
|
|
|
// Grab the original args without the first one
|
|
|
|
origArgs = _.toArray(arguments).slice(1);
|
|
|
|
// Get the actual post model
|
|
|
|
return this.findOne({id: roleModelOrId, status: 'all'}).then(function (foundRoleModel) {
|
|
|
|
// Build up the original args but substitute with actual model
|
|
|
|
var newArgs = [foundRoleModel].concat(origArgs);
|
|
|
|
|
2014-07-23 22:17:29 +04:00
|
|
|
return self.permissible.apply(self, newArgs);
|
2014-07-15 19:22:06 +04:00
|
|
|
}, errors.logAndThrowError);
|
|
|
|
}
|
|
|
|
|
2014-07-25 02:22:27 +04:00
|
|
|
if (action === 'assign' && loadedPermissions.user) {
|
2014-09-10 08:06:24 +04:00
|
|
|
if (_.any(loadedPermissions.user.roles, {name: 'Owner'})) {
|
2014-07-30 19:40:30 +04:00
|
|
|
checkAgainst = ['Owner', 'Administrator', 'Editor', 'Author'];
|
2014-09-10 08:06:24 +04:00
|
|
|
} else if (_.any(loadedPermissions.user.roles, {name: 'Administrator'})) {
|
2014-07-15 19:22:06 +04:00
|
|
|
checkAgainst = ['Administrator', 'Editor', 'Author'];
|
2014-09-10 08:06:24 +04:00
|
|
|
} else if (_.any(loadedPermissions.user.roles, {name: 'Editor'})) {
|
2014-07-25 02:22:27 +04:00
|
|
|
checkAgainst = ['Author'];
|
|
|
|
}
|
2014-07-15 19:22:06 +04:00
|
|
|
|
2014-07-25 02:22:27 +04:00
|
|
|
// Role in the list of permissible roles
|
|
|
|
hasUserPermission = roleModelOrId && _.contains(checkAgainst, roleModelOrId.get('name'));
|
2014-07-15 19:22:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hasUserPermission && hasAppPermission) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.resolve();
|
2014-07-15 19:22:06 +04:00
|
|
|
}
|
2014-08-17 10:17:23 +04:00
|
|
|
|
|
|
|
return Promise.reject();
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
|
|
|
|
2013-09-23 02:20:08 +04:00
|
|
|
Roles = ghostBookshelf.Collection.extend({
|
2013-06-25 15:43:15 +04:00
|
|
|
model: Role
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
2014-07-13 15:17:18 +04:00
|
|
|
Role: ghostBookshelf.model('Role', Role),
|
|
|
|
Roles: ghostBookshelf.collection('Roles', Roles)
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|