mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 18:31:57 +03:00
273e220327
refs 829e8ed010
- i18n is used everywhere but only requires shared or external packages, therefore it's a good candidate for living in shared
- this reduces invalid requires across frontend and server, and lets us use it everywhere until we come up with a better option
114 lines
3.9 KiB
JavaScript
114 lines
3.9 KiB
JavaScript
const _ = require('lodash');
|
|
const ghostBookshelf = require('./base');
|
|
const Promise = require('bluebird');
|
|
const i18n = require('../../shared/i18n');
|
|
const errors = require('@tryghost/errors');
|
|
let Role;
|
|
let Roles;
|
|
|
|
Role = ghostBookshelf.Model.extend({
|
|
|
|
tableName: 'roles',
|
|
|
|
relationships: ['permissions'],
|
|
|
|
relationshipBelongsTo: {
|
|
permissions: 'permissions'
|
|
},
|
|
|
|
users: function users() {
|
|
return this.belongsToMany('User');
|
|
},
|
|
|
|
permissions: function permissions() {
|
|
return this.belongsToMany('Permission');
|
|
},
|
|
|
|
api_keys: function apiKeys() {
|
|
return this.hasMany('ApiKey');
|
|
}
|
|
}, {
|
|
/**
|
|
* 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 permittedOptions(methodName) {
|
|
let options = ghostBookshelf.Model.permittedOptions.call(this, methodName);
|
|
|
|
// whitelists for the `options` hash argument on methods, by method name.
|
|
// these are the only options that can be passed to Bookshelf / Knex.
|
|
const validOptions = {
|
|
findOne: ['withRelated'],
|
|
findAll: ['withRelated']
|
|
};
|
|
|
|
if (validOptions[methodName]) {
|
|
options = options.concat(validOptions[methodName]);
|
|
}
|
|
|
|
return options;
|
|
},
|
|
|
|
permissible: function permissible(roleModelOrId, action, context, unsafeAttrs, loadedPermissions, hasUserPermission, hasApiKeyPermission) {
|
|
// If we passed in an id instead of a model, get the model
|
|
// then check the permissions
|
|
if (_.isNumber(roleModelOrId) || _.isString(roleModelOrId)) {
|
|
// Get the actual role model
|
|
return this.findOne({id: roleModelOrId, status: 'all'})
|
|
.then((foundRoleModel) => {
|
|
if (!foundRoleModel) {
|
|
throw new errors.NotFoundError({
|
|
message: i18n.t('errors.models.role.roleNotFound')
|
|
});
|
|
}
|
|
|
|
// Grab the original args without the first one
|
|
const origArgs = _.toArray(arguments).slice(1);
|
|
|
|
return this.permissible(foundRoleModel, ...origArgs);
|
|
});
|
|
}
|
|
|
|
const roleModel = roleModelOrId;
|
|
|
|
if (action === 'assign' && loadedPermissions.user) {
|
|
let checkAgainst;
|
|
if (_.some(loadedPermissions.user.roles, {name: 'Owner'})) {
|
|
checkAgainst = ['Owner', 'Administrator', 'Editor', 'Author', 'Contributor'];
|
|
} else if (_.some(loadedPermissions.user.roles, {name: 'Administrator'})) {
|
|
checkAgainst = ['Administrator', 'Editor', 'Author', 'Contributor'];
|
|
} else if (_.some(loadedPermissions.user.roles, {name: 'Editor'})) {
|
|
checkAgainst = ['Author', 'Contributor'];
|
|
}
|
|
|
|
// Role in the list of permissible roles
|
|
hasUserPermission = roleModelOrId && _.includes(checkAgainst, roleModel.get('name'));
|
|
}
|
|
|
|
if (action === 'assign' && loadedPermissions.apiKey) {
|
|
// apiKey cannot 'assign' the 'Owner' role
|
|
if (roleModel.get('name') === 'Owner') {
|
|
return Promise.reject(new errors.NoPermissionError({
|
|
message: i18n.t('errors.models.role.notEnoughPermission')
|
|
}));
|
|
}
|
|
}
|
|
|
|
if (hasUserPermission && hasApiKeyPermission) {
|
|
return Promise.resolve();
|
|
}
|
|
|
|
return Promise.reject(new errors.NoPermissionError({message: i18n.t('errors.models.role.notEnoughPermission')}));
|
|
}
|
|
});
|
|
|
|
Roles = ghostBookshelf.Collection.extend({
|
|
model: Role
|
|
});
|
|
|
|
module.exports = {
|
|
Role: ghostBookshelf.model('Role', Role),
|
|
Roles: ghostBookshelf.collection('Roles', Roles)
|
|
};
|