2014-07-15 19:22:06 +04:00
|
|
|
var _ = require('lodash'),
|
|
|
|
errors = require('../errors'),
|
|
|
|
ghostBookshelf = require('./base'),
|
|
|
|
when = require('when'),
|
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
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
permissable: function (roleModelOrId, context, loadedPermissions, hasUserPermission, hasAppPermission) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
return self.permissable.apply(self, newArgs);
|
|
|
|
}, errors.logAndThrowError);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (loadedPermissions.user) {
|
|
|
|
case 'Owner':
|
|
|
|
case 'Administrator':
|
|
|
|
checkAgainst = ['Administrator', 'Editor', 'Author'];
|
|
|
|
break;
|
|
|
|
case 'Editor':
|
|
|
|
checkAgainst = ['Editor', 'Author'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a role passed into here
|
|
|
|
if (roleModelOrId && !_.contains(checkAgainst, roleModelOrId.get('name'))) {
|
|
|
|
// Role not in the list of permissible roles
|
|
|
|
hasUserPermission = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasUserPermission && hasAppPermission) {
|
|
|
|
return when.resolve();
|
|
|
|
}
|
|
|
|
return when.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
|
|
|
};
|