2017-12-12 00:47:46 +03:00
|
|
|
var _ = require('lodash'),
|
2014-07-15 19:22:06 +04:00
|
|
|
ghostBookshelf = require('./base'),
|
2017-12-12 00:47:46 +03:00
|
|
|
Promise = require('bluebird'),
|
|
|
|
common = require('../lib/common'),
|
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
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
users: function users() {
|
2014-07-13 15:17:18 +04:00
|
|
|
return this.belongsToMany('User');
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
permissions: function permissions() {
|
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
|
|
|
}, {
|
|
|
|
/**
|
2017-12-12 00:47:46 +03: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.
|
|
|
|
*/
|
2015-06-14 18:58:49 +03:00
|
|
|
permittedOptions: function permittedOptions(methodName) {
|
2014-05-06 05:45:08 +04:00
|
|
|
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 = {
|
2016-04-08 12:09:26 +03:00
|
|
|
findOne: ['withRelated'],
|
|
|
|
findAll: ['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
|
|
|
},
|
|
|
|
|
2017-09-26 19:06:14 +03:00
|
|
|
permissible: function permissible(roleModelOrId, action, context, unsafeAttrs, 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);
|
2016-10-04 18:33:43 +03:00
|
|
|
|
2015-12-02 10:28:36 +03:00
|
|
|
// Get the actual role model
|
2015-06-14 18:58:49 +03:00
|
|
|
return this.findOne({id: roleModelOrId, status: 'all'}).then(function then(foundRoleModel) {
|
2014-07-15 19:22:06 +04:00
|
|
|
// 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);
|
2016-10-04 18:33:43 +03:00
|
|
|
});
|
2014-07-15 19:22:06 +04:00
|
|
|
}
|
|
|
|
|
2014-07-25 02:22:27 +04:00
|
|
|
if (action === 'assign' && loadedPermissions.user) {
|
2016-06-11 21:23:27 +03:00
|
|
|
if (_.some(loadedPermissions.user.roles, {name: 'Owner'})) {
|
2018-02-07 12:46:22 +03:00
|
|
|
checkAgainst = ['Owner', 'Administrator', 'Editor', 'Author', 'Contributor'];
|
2016-06-11 21:23:27 +03:00
|
|
|
} else if (_.some(loadedPermissions.user.roles, {name: 'Administrator'})) {
|
2018-02-07 12:46:22 +03:00
|
|
|
checkAgainst = ['Administrator', 'Editor', 'Author', 'Contributor'];
|
2016-06-11 21:23:27 +03:00
|
|
|
} else if (_.some(loadedPermissions.user.roles, {name: 'Editor'})) {
|
2018-02-07 12:46:22 +03:00
|
|
|
checkAgainst = ['Author', 'Contributor'];
|
2014-07-25 02:22:27 +04:00
|
|
|
}
|
2014-07-15 19:22:06 +04:00
|
|
|
|
2014-07-25 02:22:27 +04:00
|
|
|
// Role in the list of permissible roles
|
2016-06-11 21:23:27 +03:00
|
|
|
hasUserPermission = roleModelOrId && _.includes(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
|
|
|
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.NoPermissionError({message: common.i18n.t('errors.models.role.notEnoughPermission')}));
|
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
|
|
|
};
|