mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Add new permissions to fixtures
closes #2325 - added new permissions - added relation to user roles - added updateFixtures to migrateUp - removed validation per model to fix tests
This commit is contained in:
parent
13d2d04c72
commit
c0dc8e95d2
10 changed files with 271 additions and 125 deletions
|
@ -3,7 +3,11 @@ var sequence = require('when/sequence'),
|
||||||
Post = require('../../models/post').Post,
|
Post = require('../../models/post').Post,
|
||||||
Tag = require('../../models/tag').Tag,
|
Tag = require('../../models/tag').Tag,
|
||||||
Role = require('../../models/role').Role,
|
Role = require('../../models/role').Role,
|
||||||
Permission = require('../../models/permission').Permission;
|
Permission = require('../../models/permission').Permission,
|
||||||
|
Permissions = require('../../models/permission').Permissions,
|
||||||
|
|
||||||
|
populateFixtures,
|
||||||
|
updateFixtures;
|
||||||
|
|
||||||
var fixtures = {
|
var fixtures = {
|
||||||
posts: [
|
posts: [
|
||||||
|
@ -63,12 +67,71 @@ var fixtures = {
|
||||||
"action_type": "create",
|
"action_type": "create",
|
||||||
"object_type": "post"
|
"object_type": "post"
|
||||||
}
|
}
|
||||||
|
],
|
||||||
|
|
||||||
|
permissions003: [
|
||||||
|
{
|
||||||
|
"name": "Get slug",
|
||||||
|
"action_type": "slug",
|
||||||
|
"object_type": "post"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Export database",
|
||||||
|
"action_type": "exportContent",
|
||||||
|
"object_type": "db"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Import database",
|
||||||
|
"action_type": "importContent",
|
||||||
|
"object_type": "db"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Delete all content",
|
||||||
|
"action_type": "deleteAllContent",
|
||||||
|
"object_type": "db"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Browse users",
|
||||||
|
"action_type": "browse",
|
||||||
|
"object_type": "user"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Read users",
|
||||||
|
"action_type": "read",
|
||||||
|
"object_type": "user"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Edit users",
|
||||||
|
"action_type": "edit",
|
||||||
|
"object_type": "user"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Add users",
|
||||||
|
"action_type": "add",
|
||||||
|
"object_type": "user"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Browse settings",
|
||||||
|
"action_type": "browse",
|
||||||
|
"object_type": "setting"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Read settings",
|
||||||
|
"action_type": "read",
|
||||||
|
"object_type": "setting"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Edit settings",
|
||||||
|
"action_type": "edit",
|
||||||
|
"object_type": "setting"
|
||||||
|
}
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
populateFixtures: function () {
|
populateFixtures = function () {
|
||||||
var ops = [];
|
var ops = [],
|
||||||
|
relations = [];
|
||||||
|
|
||||||
_.each(fixtures.posts, function (post) {
|
_.each(fixtures.posts, function (post) {
|
||||||
ops.push(function () {return Post.add(post); });
|
ops.push(function () {return Post.add(post); });
|
||||||
|
@ -81,24 +144,150 @@ module.exports = {
|
||||||
_.each(fixtures.roles, function (role) {
|
_.each(fixtures.roles, function (role) {
|
||||||
ops.push(function () {return Role.add(role); });
|
ops.push(function () {return Role.add(role); });
|
||||||
});
|
});
|
||||||
|
|
||||||
_.each(fixtures.permissions, function (permission) {
|
_.each(fixtures.permissions, function (permission) {
|
||||||
ops.push(function () {return Permission.add(permission); });
|
ops.push(function () {return Permission.add(permission); });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_.each(fixtures.permissions003, function (permission) {
|
||||||
|
ops.push(function () {return Permission.add(permission); });
|
||||||
|
});
|
||||||
|
|
||||||
// add the tag to the post
|
// add the tag to the post
|
||||||
ops.push(function () {
|
relations.push(function () {
|
||||||
Post.forge({id: 1}).fetch({withRelated: ['tags']}).then(function (post) {
|
Post.forge({id: 1}).fetch({withRelated: ['tags']}).then(function (post) {
|
||||||
post.tags().attach([1]);
|
post.tags().attach([1]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// finally, grant admins all permissions
|
//grant permissions to roles
|
||||||
ops.push(function () {
|
relations.push(function () {
|
||||||
Role.forge({id: 1}).fetch({withRelated: ['permissions']}).then(function (role) {
|
// admins gets all permissions
|
||||||
role.permissions().attach([1, 2, 3]);
|
Role.forge({name: 'Administrator'}).fetch({withRelated: ['permissions']}).then(function (role) {
|
||||||
|
Permissions.forge().fetch().then(function (perms) {
|
||||||
|
var admin_perm = _.map(perms.toJSON(), function (perm) {
|
||||||
|
return perm.id;
|
||||||
|
});
|
||||||
|
return role.permissions().attach(_.compact(admin_perm));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return sequence(ops);
|
// editor gets access to posts, users and settings.browse, settings.read
|
||||||
|
Role.forge({name: 'Editor'}).fetch({withRelated: ['permissions']}).then(function (role) {
|
||||||
|
Permissions.forge().fetch().then(function (perms) {
|
||||||
|
var editor_perm = _.map(perms.toJSON(), function (perm) {
|
||||||
|
if (perm.object_type === 'post' || perm.object_type === 'user') {
|
||||||
|
return perm.id;
|
||||||
}
|
}
|
||||||
|
if (perm.object_type === 'setting' &&
|
||||||
|
(perm.action_type === 'browse' || perm.action_type === 'read')) {
|
||||||
|
return perm.id;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
return role.permissions().attach(_.compact(editor_perm));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// author gets access to post.add, post.slug, settings.browse, settings.read, users.browse and users.read
|
||||||
|
Role.forge({name: 'Author'}).fetch({withRelated: ['permissions']}).then(function (role) {
|
||||||
|
Permissions.forge().fetch().then(function (perms) {
|
||||||
|
var author_perm = _.map(perms.toJSON(), function (perm) {
|
||||||
|
if (perm.object_type === 'post' &&
|
||||||
|
(perm.action_type === 'add' || perm.action_type === 'slug')) {
|
||||||
|
return perm.id;
|
||||||
|
}
|
||||||
|
if (perm.object_type === 'setting' &&
|
||||||
|
(perm.action_type === 'browse' || perm.action_type === 'read')) {
|
||||||
|
return perm.id;
|
||||||
|
}
|
||||||
|
if (perm.object_type === 'user' &&
|
||||||
|
(perm.action_type === 'browse' || perm.action_type === 'read')) {
|
||||||
|
return perm.id;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
return role.permissions().attach(_.compact(author_perm));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return sequence(ops).then(function () {
|
||||||
|
sequence(relations);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
updateFixtures = function () {
|
||||||
|
var ops = [],
|
||||||
|
relations = [];
|
||||||
|
|
||||||
|
_.each(fixtures.permissions003, function (permission) {
|
||||||
|
ops.push(function () {return Permission.add(permission); });
|
||||||
|
});
|
||||||
|
|
||||||
|
relations.push(function () {
|
||||||
|
// admin gets all new permissions
|
||||||
|
Role.forge({name: 'Administrator'}).fetch({withRelated: ['permissions']}).then(function (role) {
|
||||||
|
Permissions.forge().fetch().then(function (perms) {
|
||||||
|
var admin_perm = _.map(perms.toJSON(), function (perm) {
|
||||||
|
var result = fixtures.permissions003.filter(function (object) {
|
||||||
|
return object.object_type === perm.object_type && object.action_type === perm.action_type;
|
||||||
|
});
|
||||||
|
if (!_.isEmpty(result)) {
|
||||||
|
return perm.id;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
return role.permissions().attach(_.compact(admin_perm));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// editor gets access to posts, users and settings.browse, settings.read
|
||||||
|
Role.forge({name: 'Editor'}).fetch({withRelated: ['permissions']}).then(function (role) {
|
||||||
|
Permissions.forge().fetch().then(function (perms) {
|
||||||
|
var editor_perm = _.map(perms.toJSON(), function (perm) {
|
||||||
|
if (perm.object_type === 'post' || perm.object_type === 'user') {
|
||||||
|
return perm.id;
|
||||||
|
}
|
||||||
|
if (perm.object_type === 'setting' &&
|
||||||
|
(perm.action_type === 'browse' || perm.action_type === 'read')) {
|
||||||
|
return perm.id;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
return role.permissions().attach(_.compact(editor_perm));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// author gets access to post.add, post.slug, settings.browse, settings.read, users.browse and users.read
|
||||||
|
Role.forge({name: 'Author'}).fetch({withRelated: ['permissions']}).then(function (role) {
|
||||||
|
Permissions.forge().fetch().then(function (perms) {
|
||||||
|
var author_perm = _.map(perms.toJSON(), function (perm) {
|
||||||
|
if (perm.object_type === 'post' &&
|
||||||
|
(perm.action_type === 'add' || perm.action_type === 'slug')) {
|
||||||
|
return perm.id;
|
||||||
|
}
|
||||||
|
if (perm.object_type === 'setting' &&
|
||||||
|
(perm.action_type === 'browse' || perm.action_type === 'read')) {
|
||||||
|
return perm.id;
|
||||||
|
}
|
||||||
|
if (perm.object_type === 'user' &&
|
||||||
|
(perm.action_type === 'browse' || perm.action_type === 'read')) {
|
||||||
|
return perm.id;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
return role.permissions().attach(_.compact(author_perm));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return sequence(ops).then(function () {
|
||||||
|
sequence(relations);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
populateFixtures: populateFixtures,
|
||||||
|
updateFixtures: updateFixtures
|
||||||
};
|
};
|
|
@ -141,33 +141,33 @@ function importApps(ops, tableData, transaction) {
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
function importAppSettings(ops, tableData, transaction) {
|
|
||||||
var appsData = tableData.apps,
|
|
||||||
appSettingsData = tableData.app_settings,
|
|
||||||
appName;
|
|
||||||
|
|
||||||
appSettingsData = stripProperties(['id'], appSettingsData);
|
// function importAppSettings(ops, tableData, transaction) {
|
||||||
|
// var appsData = tableData.apps,
|
||||||
|
// appSettingsData = tableData.app_settings,
|
||||||
|
// appName;
|
||||||
|
//
|
||||||
|
// appSettingsData = stripProperties(['id'], appSettingsData);
|
||||||
|
//
|
||||||
|
// _.each(appSettingsData, function (appSetting) {
|
||||||
|
// // Find app to attach settings to
|
||||||
|
// appName = _.find(appsData, function (app) {
|
||||||
|
// return app.id === appSetting.app_id;
|
||||||
|
// }).name;
|
||||||
|
// ops.push(models.App.findOne({name: appName}, {transacting: transaction}).then(function (_app) {
|
||||||
|
// if (_app) {
|
||||||
|
// // Fix app_id
|
||||||
|
// appSetting.app_id = _app.id;
|
||||||
|
// return models.AppSetting.add(appSetting, {transacting: transaction})
|
||||||
|
// // add pass-through error handling so that bluebird doesn't think we've dropped it
|
||||||
|
// .otherwise(function (error) { return when.reject(error); });
|
||||||
|
// }
|
||||||
|
// // Gracefully ignore missing apps
|
||||||
|
// return when.resolve(_app);
|
||||||
|
// }));
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
|
||||||
_.each(appSettingsData, function (appSetting) {
|
|
||||||
// Find app to attach settings to
|
|
||||||
appName = _.find(appsData, function (app) {
|
|
||||||
return app.id === appSetting.app_id;
|
|
||||||
}).name;
|
|
||||||
ops.push(models.App.findOne({name: appName}, {transacting: transaction}).then(function (_app) {
|
|
||||||
if (_app) {
|
|
||||||
// Fix app_id
|
|
||||||
appSetting.app_id = _app.id;
|
|
||||||
return models.AppSetting.add(appSetting, {transacting: transaction})
|
|
||||||
// add pass-through error handling so that bluebird doesn't think we've dropped it
|
|
||||||
.otherwise(function (error) { return when.reject(error); });
|
|
||||||
}
|
|
||||||
// Gracefully ignore missing apps
|
|
||||||
return when.resolve(_app);
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
// No data needs modifying, we just import whatever tables are available
|
// No data needs modifying, we just import whatever tables are available
|
||||||
Importer000.prototype.basicImport = function (data) {
|
Importer000.prototype.basicImport = function (data) {
|
||||||
var ops = [],
|
var ops = [],
|
||||||
|
|
|
@ -252,7 +252,7 @@ function checkMySQLPostTable() {
|
||||||
// Migrate from a specific version to the latest
|
// Migrate from a specific version to the latest
|
||||||
migrateUp = function () {
|
migrateUp = function () {
|
||||||
return getTables().then(function (oldTables) {
|
return getTables().then(function (oldTables) {
|
||||||
// if tables exist and lient is mysqls check if posts table is okay
|
// if tables exist and client is mysqls check if posts table is okay
|
||||||
if (!_.isEmpty(oldTables) && client === 'mysql') {
|
if (!_.isEmpty(oldTables) && client === 'mysql') {
|
||||||
return checkMySQLPostTable().then(function () {
|
return checkMySQLPostTable().then(function () {
|
||||||
return oldTables;
|
return oldTables;
|
||||||
|
@ -274,6 +274,8 @@ migrateUp = function () {
|
||||||
return sequence(commands);
|
return sequence(commands);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
}).then(function () {
|
||||||
|
return fixtures.updateFixtures();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -6,10 +6,6 @@ var ghostBookshelf = require('./base'),
|
||||||
App = ghostBookshelf.Model.extend({
|
App = ghostBookshelf.Model.extend({
|
||||||
tableName: 'apps',
|
tableName: 'apps',
|
||||||
|
|
||||||
validate: function () {
|
|
||||||
ghostBookshelf.validator.check(this.get('name'), "App name cannot be blank").notEmpty();
|
|
||||||
},
|
|
||||||
|
|
||||||
permissions: function () {
|
permissions: function () {
|
||||||
// Have to use the require here because of circular dependencies
|
// Have to use the require here because of circular dependencies
|
||||||
return this.belongsToMany(require('./permission').Permission, 'permissions_apps');
|
return this.belongsToMany(require('./permission').Permission, 'permissions_apps');
|
||||||
|
|
|
@ -6,17 +6,6 @@ var ghostBookshelf = require('./base'),
|
||||||
AppField = ghostBookshelf.Model.extend({
|
AppField = ghostBookshelf.Model.extend({
|
||||||
tableName: 'app_fields',
|
tableName: 'app_fields',
|
||||||
|
|
||||||
validate: function () {
|
|
||||||
ghostBookshelf.validator.check(this.get('key'), 'Key cannot be blank').notEmpty();
|
|
||||||
ghostBookshelf.validator.check(this.get('key'), 'Key maximum length is 150 characters.').len(0, 150);
|
|
||||||
ghostBookshelf.validator.check(this.get('app_id'), 'App cannot be blank').notEmpty();
|
|
||||||
ghostBookshelf.validator.check(this.get('type'), 'Type maximum length is 150 characters.').len(0, 150);
|
|
||||||
ghostBookshelf.validator.check(this.get('relatable_id'), 'Relatable id cannot be blank').notEmpty();
|
|
||||||
ghostBookshelf.validator.check(this.get('relatable_type'), 'Relatable type cannot be blank').notEmpty();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
|
|
||||||
post: function () {
|
post: function () {
|
||||||
return this.morphOne(Post, 'relatable');
|
return this.morphOne(Post, 'relatable');
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,15 +6,6 @@ var ghostBookshelf = require('./base'),
|
||||||
AppSetting = ghostBookshelf.Model.extend({
|
AppSetting = ghostBookshelf.Model.extend({
|
||||||
tableName: 'app_settings',
|
tableName: 'app_settings',
|
||||||
|
|
||||||
validate: function () {
|
|
||||||
ghostBookshelf.validator.check(this.get('key'), 'Key cannot be blank').notEmpty();
|
|
||||||
ghostBookshelf.validator.check(this.get('key'), 'Key maximum length is 150 characters.').len(0, 150);
|
|
||||||
ghostBookshelf.validator.check(this.get('app_id'), 'App cannot be blank').notEmpty();
|
|
||||||
ghostBookshelf.validator.check(this.get('type'), 'Type maximum length is 150 characters.').len(0, 150);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
|
|
||||||
app: function () {
|
app: function () {
|
||||||
return this.belongsTo(App);
|
return this.belongsTo(App);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ var ghostBookshelf = require('./base'),
|
||||||
User = require('./user').User,
|
User = require('./user').User,
|
||||||
Role = require('./role').Role,
|
Role = require('./role').Role,
|
||||||
App = require('./app').App,
|
App = require('./app').App,
|
||||||
|
|
||||||
Permission,
|
Permission,
|
||||||
Permissions;
|
Permissions;
|
||||||
|
|
||||||
|
|
|
@ -103,13 +103,7 @@ describe("Import", function () {
|
||||||
|
|
||||||
beforeEach(function (done) {
|
beforeEach(function (done) {
|
||||||
// migrate to current version
|
// migrate to current version
|
||||||
migration.migrateUp().then(function () {
|
migration.migrateUpFreshDb().then(function () {
|
||||||
// Load the fixtures
|
|
||||||
return fixtures.populateFixtures();
|
|
||||||
}).then(function () {
|
|
||||||
// Initialise the default settings
|
|
||||||
return Settings.populateDefaults();
|
|
||||||
}).then(function () {
|
|
||||||
return testUtils.insertDefaultUser();
|
return testUtils.insertDefaultUser();
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
done();
|
done();
|
||||||
|
@ -169,12 +163,8 @@ describe("Import", function () {
|
||||||
|
|
||||||
beforeEach(function (done) {
|
beforeEach(function (done) {
|
||||||
// migrate to current version
|
// migrate to current version
|
||||||
migration.migrateUp().then(function () {
|
migration.migrateUpFreshDb().then(function () {
|
||||||
// Load the fixtures
|
// Load the fixtures
|
||||||
return fixtures.populateFixtures();
|
|
||||||
}).then(function () {
|
|
||||||
// Initialise the default settings
|
|
||||||
return Settings.populateDefaults();
|
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
return testUtils.insertDefaultUser();
|
return testUtils.insertDefaultUser();
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
|
@ -356,13 +346,7 @@ describe("Import", function () {
|
||||||
|
|
||||||
beforeEach(function (done) {
|
beforeEach(function (done) {
|
||||||
// migrate to current version
|
// migrate to current version
|
||||||
migration.migrateUp().then(function () {
|
migration.migrateUpFreshDb().then(function () {
|
||||||
// Load the fixtures
|
|
||||||
return fixtures.populateFixtures();
|
|
||||||
}).then(function () {
|
|
||||||
// Initialise the default settings
|
|
||||||
return Settings.populateDefaults();
|
|
||||||
}).then(function () {
|
|
||||||
return testUtils.insertDefaultUser();
|
return testUtils.insertDefaultUser();
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
done();
|
done();
|
||||||
|
@ -543,13 +527,7 @@ describe("Import", function () {
|
||||||
|
|
||||||
beforeEach(function (done) {
|
beforeEach(function (done) {
|
||||||
// migrate to current version
|
// migrate to current version
|
||||||
migration.migrateUp().then(function () {
|
migration.migrateUpFreshDb().then(function () {
|
||||||
// Load the fixtures
|
|
||||||
return fixtures.populateFixtures();
|
|
||||||
}).then(function () {
|
|
||||||
// Initialise the default settings
|
|
||||||
return Settings.populateDefaults();
|
|
||||||
}).then(function () {
|
|
||||||
return testUtils.insertDefaultUser();
|
return testUtils.insertDefaultUser();
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
done();
|
done();
|
||||||
|
|
|
@ -104,7 +104,7 @@ describe('Permissions', function () {
|
||||||
.then(function (actionsMap) {
|
.then(function (actionsMap) {
|
||||||
should.exist(actionsMap);
|
should.exist(actionsMap);
|
||||||
|
|
||||||
actionsMap.edit.sort().should.eql(['post', 'tag', 'user', 'page'].sort());
|
actionsMap.edit.sort().should.eql(['post', 'tag', 'user', 'page', 'setting'].sort());
|
||||||
|
|
||||||
actionsMap.should.equal(permissions.actionsMap);
|
actionsMap.should.equal(permissions.actionsMap);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue