2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const testUtils = require('../../utils');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const Models = require('../../../core/server/models');
|
2015-08-18 16:08:52 +03:00
|
|
|
|
|
|
|
describe('Database Migration (special functions)', function () {
|
2020-02-24 23:51:09 +03:00
|
|
|
before(testUtils.teardownDb);
|
|
|
|
afterEach(testUtils.teardownDb);
|
2016-02-25 10:10:36 +03:00
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2016-02-25 10:10:36 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Fixtures', function () {
|
|
|
|
// Custom assertion for detection that a permissions is assigned to the correct roles
|
|
|
|
should.Assertion.add('AssignedToRoles', function (roles) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let roleNames;
|
2016-02-25 10:10:36 +03:00
|
|
|
this.params = {operator: 'to have role'};
|
|
|
|
|
|
|
|
should.exist(this.obj);
|
|
|
|
|
|
|
|
this.obj.should.be.an.Object().with.property(['roles']);
|
|
|
|
this.obj.roles.should.be.an.Array();
|
2016-03-15 21:27:03 +03:00
|
|
|
|
|
|
|
// Ensure the roles are in id order
|
2016-06-11 21:23:27 +03:00
|
|
|
roleNames = _(this.obj.roles).sortBy('id').map('name').value();
|
2016-02-25 10:10:36 +03:00
|
|
|
roleNames.should.eql(roles);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Custom assertion to wrap all permissions
|
|
|
|
should.Assertion.add('CompletePermissions', function () {
|
|
|
|
this.params = {operator: 'to have a complete set of permissions'};
|
2020-04-29 18:44:27 +03:00
|
|
|
const permissions = this.obj;
|
2016-02-25 10:10:36 +03:00
|
|
|
|
|
|
|
// DB
|
|
|
|
permissions[0].name.should.eql('Export database');
|
2019-08-05 05:57:47 +03:00
|
|
|
permissions[0].should.be.AssignedToRoles(['Administrator', 'DB Backup Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
permissions[1].name.should.eql('Import database');
|
2019-08-05 05:57:47 +03:00
|
|
|
permissions[1].should.be.AssignedToRoles(['Administrator', 'DB Backup Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
permissions[2].name.should.eql('Delete all content');
|
2019-08-05 05:57:47 +03:00
|
|
|
permissions[2].should.be.AssignedToRoles(['Administrator', 'DB Backup Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
|
|
|
|
// Mail
|
|
|
|
permissions[3].name.should.eql('Send mail');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[3].should.be.AssignedToRoles(['Administrator', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
|
|
|
|
// Notifications
|
|
|
|
permissions[4].name.should.eql('Browse notifications');
|
2019-04-17 20:18:44 +03:00
|
|
|
permissions[4].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
permissions[5].name.should.eql('Add notifications');
|
2019-04-17 20:18:44 +03:00
|
|
|
permissions[5].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
permissions[6].name.should.eql('Delete notifications');
|
2019-04-17 20:18:44 +03:00
|
|
|
permissions[6].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
|
|
|
|
// Posts
|
|
|
|
permissions[7].name.should.eql('Browse posts');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[7].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
permissions[8].name.should.eql('Read posts');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[8].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
permissions[9].name.should.eql('Edit posts');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[9].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
permissions[10].name.should.eql('Add posts');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[10].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
permissions[11].name.should.eql('Delete posts');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[11].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
|
|
|
|
// Settings
|
|
|
|
permissions[12].name.should.eql('Browse settings');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[12].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
permissions[13].name.should.eql('Read settings');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[13].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
permissions[14].name.should.eql('Edit settings');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[14].should.be.AssignedToRoles(['Administrator', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
|
|
|
|
// Slugs
|
|
|
|
permissions[15].name.should.eql('Generate slugs');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[15].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
|
|
|
|
// Tags
|
|
|
|
permissions[16].name.should.eql('Browse tags');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[16].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
permissions[17].name.should.eql('Read tags');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[17].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
permissions[18].name.should.eql('Edit tags');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[18].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
permissions[19].name.should.eql('Add tags');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[19].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
permissions[20].name.should.eql('Delete tags');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[20].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
|
|
|
|
// Themes
|
|
|
|
permissions[21].name.should.eql('Browse themes');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[21].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
permissions[22].name.should.eql('Edit themes');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[22].should.be.AssignedToRoles(['Administrator', 'Admin Integration']);
|
2017-03-13 14:44:44 +03:00
|
|
|
permissions[23].name.should.eql('Activate themes');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[23].should.be.AssignedToRoles(['Administrator', 'Admin Integration']);
|
2017-03-13 14:44:44 +03:00
|
|
|
permissions[24].name.should.eql('Upload themes');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[24].should.be.AssignedToRoles(['Administrator', 'Admin Integration']);
|
2017-03-13 14:44:44 +03:00
|
|
|
permissions[25].name.should.eql('Download themes');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[25].should.be.AssignedToRoles(['Administrator', 'Admin Integration']);
|
2017-03-13 14:44:44 +03:00
|
|
|
permissions[26].name.should.eql('Delete themes');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[26].should.be.AssignedToRoles(['Administrator', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
|
|
|
|
// Users
|
2017-03-13 14:44:44 +03:00
|
|
|
permissions[27].name.should.eql('Browse users');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[27].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
2017-03-13 14:44:44 +03:00
|
|
|
permissions[28].name.should.eql('Read users');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[28].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
2017-03-13 14:44:44 +03:00
|
|
|
permissions[29].name.should.eql('Edit users');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[29].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
2017-03-13 14:44:44 +03:00
|
|
|
permissions[30].name.should.eql('Add users');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[30].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
2017-03-13 14:44:44 +03:00
|
|
|
permissions[31].name.should.eql('Delete users');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[31].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
2015-08-18 16:08:52 +03:00
|
|
|
|
2016-02-25 10:10:36 +03:00
|
|
|
// Roles
|
2017-03-13 14:44:44 +03:00
|
|
|
permissions[32].name.should.eql('Assign a role');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[32].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
2017-03-13 14:44:44 +03:00
|
|
|
permissions[33].name.should.eql('Browse roles');
|
2018-10-02 19:46:38 +03:00
|
|
|
permissions[33].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
2016-04-08 12:09:26 +03:00
|
|
|
|
2017-03-13 14:44:44 +03:00
|
|
|
// Invites
|
2019-10-09 18:26:14 +03:00
|
|
|
permissions[34].name.should.eql('Browse invites');
|
|
|
|
permissions[34].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
|
|
|
permissions[35].name.should.eql('Read invites');
|
|
|
|
permissions[35].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
|
|
|
permissions[36].name.should.eql('Edit invites');
|
|
|
|
permissions[36].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
|
|
|
permissions[37].name.should.eql('Add invites');
|
|
|
|
permissions[37].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
|
|
|
permissions[38].name.should.eql('Delete invites');
|
|
|
|
permissions[38].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
2017-09-21 18:01:03 +03:00
|
|
|
|
|
|
|
// Redirects
|
2019-10-09 18:26:14 +03:00
|
|
|
permissions[39].name.should.eql('Download redirects');
|
|
|
|
permissions[39].should.be.AssignedToRoles(['Administrator', 'Admin Integration']);
|
|
|
|
permissions[40].name.should.eql('Upload redirects');
|
|
|
|
permissions[40].should.be.AssignedToRoles(['Administrator', 'Admin Integration']);
|
2018-10-02 19:46:38 +03:00
|
|
|
|
|
|
|
// Webhooks
|
2019-10-09 18:26:14 +03:00
|
|
|
permissions[41].name.should.eql('Add webhooks');
|
|
|
|
permissions[41].should.be.AssignedToRoles(['Administrator', 'Admin Integration']);
|
|
|
|
permissions[42].name.should.eql('Edit webhooks');
|
|
|
|
permissions[42].should.be.AssignedToRoles(['Administrator', 'Admin Integration']);
|
|
|
|
permissions[43].name.should.eql('Delete webhooks');
|
|
|
|
permissions[43].should.be.AssignedToRoles(['Administrator', 'Admin Integration']);
|
2018-10-02 19:46:38 +03:00
|
|
|
|
|
|
|
// Integrations
|
2019-10-09 18:26:14 +03:00
|
|
|
permissions[44].name.should.eql('Browse integrations');
|
|
|
|
permissions[44].should.be.AssignedToRoles(['Administrator']);
|
|
|
|
permissions[45].name.should.eql('Read integrations');
|
|
|
|
permissions[45].should.be.AssignedToRoles(['Administrator']);
|
|
|
|
permissions[46].name.should.eql('Edit integrations');
|
|
|
|
permissions[46].should.be.AssignedToRoles(['Administrator']);
|
|
|
|
permissions[47].name.should.eql('Add integrations');
|
|
|
|
permissions[47].should.be.AssignedToRoles(['Administrator']);
|
|
|
|
permissions[48].name.should.eql('Delete integrations');
|
|
|
|
permissions[48].should.be.AssignedToRoles(['Administrator']);
|
|
|
|
|
|
|
|
// API Keys
|
|
|
|
permissions[49].name.should.eql('Browse API keys');
|
2019-09-12 16:54:39 +03:00
|
|
|
permissions[49].should.be.AssignedToRoles(['Administrator']);
|
2019-10-09 18:26:14 +03:00
|
|
|
permissions[50].name.should.eql('Read API keys');
|
2019-09-12 16:54:39 +03:00
|
|
|
permissions[50].should.be.AssignedToRoles(['Administrator']);
|
2019-10-09 18:26:14 +03:00
|
|
|
permissions[51].name.should.eql('Edit API keys');
|
2019-09-12 16:54:39 +03:00
|
|
|
permissions[51].should.be.AssignedToRoles(['Administrator']);
|
2019-10-09 18:26:14 +03:00
|
|
|
permissions[52].name.should.eql('Add API keys');
|
2019-09-12 16:54:39 +03:00
|
|
|
permissions[52].should.be.AssignedToRoles(['Administrator']);
|
2019-10-09 18:26:14 +03:00
|
|
|
permissions[53].name.should.eql('Delete API keys');
|
2019-09-12 16:54:39 +03:00
|
|
|
permissions[53].should.be.AssignedToRoles(['Administrator']);
|
|
|
|
|
2019-01-29 21:00:44 +03:00
|
|
|
// Actions
|
2019-10-09 18:26:14 +03:00
|
|
|
permissions[54].name.should.eql('Browse Actions');
|
|
|
|
permissions[54].should.be.AssignedToRoles(['Administrator', 'Admin Integration']);
|
2019-02-26 06:24:09 +03:00
|
|
|
|
|
|
|
// Members
|
2019-10-09 18:26:14 +03:00
|
|
|
permissions[55].name.should.eql('Browse Members');
|
|
|
|
permissions[56].name.should.eql('Read Members');
|
|
|
|
permissions[57].name.should.eql('Edit Members');
|
|
|
|
permissions[58].name.should.eql('Add Members');
|
|
|
|
permissions[59].name.should.eql('Delete Members');
|
2019-08-06 15:56:47 +03:00
|
|
|
|
|
|
|
// Posts
|
2019-10-09 18:26:14 +03:00
|
|
|
permissions[60].name.should.eql('Publish posts');
|
|
|
|
permissions[60].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration', 'Scheduler Integration']);
|
2019-08-09 01:53:59 +03:00
|
|
|
|
|
|
|
// DB
|
2019-10-09 18:26:14 +03:00
|
|
|
permissions[61].name.should.eql('Backup database');
|
|
|
|
permissions[61].should.be.AssignedToRoles(['Administrator', 'DB Backup Integration']);
|
2019-11-27 16:01:55 +03:00
|
|
|
|
|
|
|
// Bulk Email
|
|
|
|
permissions[62].name.should.eql('Email preview');
|
|
|
|
permissions[62].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
|
|
|
permissions[63].name.should.eql('Send test email');
|
|
|
|
permissions[63].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
|
|
|
permissions[64].name.should.eql('Browse emails');
|
|
|
|
permissions[64].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
|
|
|
permissions[65].name.should.eql('Read emails');
|
|
|
|
permissions[65].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
|
|
|
permissions[66].name.should.eql('Retry emails');
|
|
|
|
permissions[66].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
2020-10-16 14:53:09 +03:00
|
|
|
|
|
|
|
// Labels
|
2020-02-14 13:14:47 +03:00
|
|
|
permissions[67].name.should.eql('Browse labels');
|
|
|
|
permissions[68].name.should.eql('Read labels');
|
|
|
|
permissions[69].name.should.eql('Edit labels');
|
|
|
|
permissions[70].name.should.eql('Add labels');
|
|
|
|
permissions[71].name.should.eql('Delete labels');
|
2020-10-16 14:53:09 +03:00
|
|
|
|
|
|
|
// Member auth
|
2020-02-27 07:57:53 +03:00
|
|
|
permissions[72].name.should.eql('Read member signin urls');
|
2020-10-16 14:53:09 +03:00
|
|
|
permissions[73].name.should.eql('Read identities');
|
|
|
|
permissions[74].name.should.eql('Auth Stripe Connect for Members');
|
|
|
|
|
|
|
|
// Snippets
|
|
|
|
permissions[75].name.should.eql('Browse snippets');
|
|
|
|
permissions[75].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
|
|
|
permissions[76].name.should.eql('Read snippets');
|
|
|
|
permissions[76].should.be.AssignedToRoles(['Administrator', 'Editor', 'Author', 'Contributor', 'Admin Integration']);
|
|
|
|
permissions[77].name.should.eql('Edit snippets');
|
|
|
|
permissions[77].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
|
|
|
permissions[78].name.should.eql('Add snippets');
|
|
|
|
permissions[78].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
|
|
|
permissions[79].name.should.eql('Delete snippets');
|
|
|
|
permissions[79].should.be.AssignedToRoles(['Administrator', 'Editor', 'Admin Integration']);
|
2016-02-25 10:10:36 +03:00
|
|
|
});
|
|
|
|
|
2016-04-08 12:09:26 +03:00
|
|
|
describe('Populate', function () {
|
2017-01-25 16:47:49 +03:00
|
|
|
beforeEach(testUtils.setup('default'));
|
2016-04-14 18:54:49 +03:00
|
|
|
|
2017-11-28 21:32:03 +03:00
|
|
|
it('should populate all fixtures correctly', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const props = {
|
Sorted out the mixed usages of `include` and `withRelated` (#9425)
no issue
- this commit cleans up the usages of `include` and `withRelated`.
### API layer (`include`)
- as request parameter e.g. `?include=roles,tags`
- as theme API parameter e.g. `{{get .... include="author"}}`
- as internal API access e.g. `api.posts.browse({include: 'author,tags'})`
- the `include` notation is more readable than `withRelated`
- and it allows us to use a different easier format (comma separated list)
- the API utility transforms these more readable properties into model style (or into Ghost style)
### Model access (`withRelated`)
- e.g. `models.Post.findPage({withRelated: ['tags']})`
- driven by bookshelf
---
Commits explained.
* Reorder the usage of `convertOptions`
- 1. validation
- 2. options convertion
- 3. permissions
- the reason is simple, the permission layer access the model layer
- we have to prepare the options before talking to the model layer
- added `convertOptions` where it was missed (not required, but for consistency reasons)
* Use `withRelated` when accessing the model layer and use `include` when accessing the API layer
* Change `convertOptions` API utiliy
- API Usage
- ghost.api(..., {include: 'tags,authors'})
- `include` should only be used when calling the API (either via request or via manual usage)
- `include` is only for readability and easier format
- Ghost (Model Layer Usage)
- models.Post.findOne(..., {withRelated: ['tags', 'authors']})
- should only use `withRelated`
- model layer cannot read 'tags,authors`
- model layer has no idea what `include` means, speaks a different language
- `withRelated` is bookshelf
- internal usage
* include-count plugin: use `withRelated` instead of `include`
- imagine you outsource this plugin to git and publish it to npm
- `include` is an unknown option in bookshelf
* Updated `permittedOptions` in base model
- `include` is no longer a known option
* Remove all occurances of `include` in the model layer
* Extend `filterOptions` base function
- this function should be called as first action
- we clone the unfiltered options
- check if you are using `include` (this is a protection which could help us in the beginning)
- check for permitted and (later on default `withRelated`) options
- the usage is coming in next commit
* Ensure we call `filterOptions` as first action
- use `ghostBookshelf.Model.filterOptions` as first action
- consistent naming pattern for incoming options: `unfilteredOptions`
- re-added allowed options for `toJSON`
- one unsolved architecture problem:
- if you override a function e.g. `edit`
- then you should call `filterOptions` as first action
- the base implementation of e.g. `edit` will call it again
- future improvement
* Removed `findOne` from Invite model
- no longer needed, the base implementation is the same
2018-02-15 12:53:53 +03:00
|
|
|
posts: Models.Post.findAll({withRelated: ['tags']}),
|
2017-01-25 16:47:49 +03:00
|
|
|
tags: Models.Tag.findAll(),
|
|
|
|
users: Models.User.findAll({
|
|
|
|
filter: 'status:inactive',
|
|
|
|
context: {internal: true},
|
Sorted out the mixed usages of `include` and `withRelated` (#9425)
no issue
- this commit cleans up the usages of `include` and `withRelated`.
### API layer (`include`)
- as request parameter e.g. `?include=roles,tags`
- as theme API parameter e.g. `{{get .... include="author"}}`
- as internal API access e.g. `api.posts.browse({include: 'author,tags'})`
- the `include` notation is more readable than `withRelated`
- and it allows us to use a different easier format (comma separated list)
- the API utility transforms these more readable properties into model style (or into Ghost style)
### Model access (`withRelated`)
- e.g. `models.Post.findPage({withRelated: ['tags']})`
- driven by bookshelf
---
Commits explained.
* Reorder the usage of `convertOptions`
- 1. validation
- 2. options convertion
- 3. permissions
- the reason is simple, the permission layer access the model layer
- we have to prepare the options before talking to the model layer
- added `convertOptions` where it was missed (not required, but for consistency reasons)
* Use `withRelated` when accessing the model layer and use `include` when accessing the API layer
* Change `convertOptions` API utiliy
- API Usage
- ghost.api(..., {include: 'tags,authors'})
- `include` should only be used when calling the API (either via request or via manual usage)
- `include` is only for readability and easier format
- Ghost (Model Layer Usage)
- models.Post.findOne(..., {withRelated: ['tags', 'authors']})
- should only use `withRelated`
- model layer cannot read 'tags,authors`
- model layer has no idea what `include` means, speaks a different language
- `withRelated` is bookshelf
- internal usage
* include-count plugin: use `withRelated` instead of `include`
- imagine you outsource this plugin to git and publish it to npm
- `include` is an unknown option in bookshelf
* Updated `permittedOptions` in base model
- `include` is no longer a known option
* Remove all occurances of `include` in the model layer
* Extend `filterOptions` base function
- this function should be called as first action
- we clone the unfiltered options
- check if you are using `include` (this is a protection which could help us in the beginning)
- check for permitted and (later on default `withRelated`) options
- the usage is coming in next commit
* Ensure we call `filterOptions` as first action
- use `ghostBookshelf.Model.filterOptions` as first action
- consistent naming pattern for incoming options: `unfilteredOptions`
- re-added allowed options for `toJSON`
- one unsolved architecture problem:
- if you override a function e.g. `edit`
- then you should call `filterOptions` as first action
- the base implementation of e.g. `edit` will call it again
- future improvement
* Removed `findOne` from Invite model
- no longer needed, the base implementation is the same
2018-02-15 12:53:53 +03:00
|
|
|
withRelated: ['roles']
|
2017-01-25 16:47:49 +03:00
|
|
|
}),
|
|
|
|
roles: Models.Role.findAll(),
|
Sorted out the mixed usages of `include` and `withRelated` (#9425)
no issue
- this commit cleans up the usages of `include` and `withRelated`.
### API layer (`include`)
- as request parameter e.g. `?include=roles,tags`
- as theme API parameter e.g. `{{get .... include="author"}}`
- as internal API access e.g. `api.posts.browse({include: 'author,tags'})`
- the `include` notation is more readable than `withRelated`
- and it allows us to use a different easier format (comma separated list)
- the API utility transforms these more readable properties into model style (or into Ghost style)
### Model access (`withRelated`)
- e.g. `models.Post.findPage({withRelated: ['tags']})`
- driven by bookshelf
---
Commits explained.
* Reorder the usage of `convertOptions`
- 1. validation
- 2. options convertion
- 3. permissions
- the reason is simple, the permission layer access the model layer
- we have to prepare the options before talking to the model layer
- added `convertOptions` where it was missed (not required, but for consistency reasons)
* Use `withRelated` when accessing the model layer and use `include` when accessing the API layer
* Change `convertOptions` API utiliy
- API Usage
- ghost.api(..., {include: 'tags,authors'})
- `include` should only be used when calling the API (either via request or via manual usage)
- `include` is only for readability and easier format
- Ghost (Model Layer Usage)
- models.Post.findOne(..., {withRelated: ['tags', 'authors']})
- should only use `withRelated`
- model layer cannot read 'tags,authors`
- model layer has no idea what `include` means, speaks a different language
- `withRelated` is bookshelf
- internal usage
* include-count plugin: use `withRelated` instead of `include`
- imagine you outsource this plugin to git and publish it to npm
- `include` is an unknown option in bookshelf
* Updated `permittedOptions` in base model
- `include` is no longer a known option
* Remove all occurances of `include` in the model layer
* Extend `filterOptions` base function
- this function should be called as first action
- we clone the unfiltered options
- check if you are using `include` (this is a protection which could help us in the beginning)
- check for permitted and (later on default `withRelated`) options
- the usage is coming in next commit
* Ensure we call `filterOptions` as first action
- use `ghostBookshelf.Model.filterOptions` as first action
- consistent naming pattern for incoming options: `unfilteredOptions`
- re-added allowed options for `toJSON`
- one unsolved architecture problem:
- if you override a function e.g. `edit`
- then you should call `filterOptions` as first action
- the base implementation of e.g. `edit` will call it again
- future improvement
* Removed `findOne` from Invite model
- no longer needed, the base implementation is the same
2018-02-15 12:53:53 +03:00
|
|
|
permissions: Models.Permission.findAll({withRelated: ['roles']})
|
2017-01-25 16:47:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return Promise.props(props).then(function (result) {
|
|
|
|
should.exist(result);
|
|
|
|
|
|
|
|
// Post
|
|
|
|
should.exist(result.posts);
|
2017-06-08 18:36:14 +03:00
|
|
|
result.posts.length.should.eql(7);
|
2018-10-09 16:31:09 +03:00
|
|
|
result.posts.at(0).get('title').should.eql('Welcome to Ghost');
|
|
|
|
result.posts.at(6).get('title').should.eql('Creating a custom theme');
|
2017-01-25 16:47:49 +03:00
|
|
|
|
|
|
|
// Tag
|
|
|
|
should.exist(result.tags);
|
|
|
|
result.tags.length.should.eql(1);
|
|
|
|
result.tags.at(0).get('name').should.eql('Getting Started');
|
|
|
|
|
|
|
|
// Post Tag relation
|
|
|
|
result.posts.at(0).related('tags').length.should.eql(1);
|
|
|
|
result.posts.at(0).related('tags').at(0).get('name').should.eql('Getting Started');
|
|
|
|
|
|
|
|
// User (Owner)
|
|
|
|
should.exist(result.users);
|
|
|
|
result.users.length.should.eql(1);
|
2017-05-29 20:25:19 +03:00
|
|
|
result.users.at(0).get('name').should.eql('Ghost');
|
2017-01-25 16:47:49 +03:00
|
|
|
result.users.at(0).get('status').should.eql('inactive');
|
|
|
|
result.users.at(0).related('roles').length.should.eql(1);
|
|
|
|
result.users.at(0).related('roles').at(0).get('name').should.eql('Owner');
|
|
|
|
|
|
|
|
// Roles
|
|
|
|
should.exist(result.roles);
|
2019-08-06 15:56:47 +03:00
|
|
|
result.roles.length.should.eql(8);
|
2017-01-25 16:47:49 +03:00
|
|
|
result.roles.at(0).get('name').should.eql('Administrator');
|
|
|
|
result.roles.at(1).get('name').should.eql('Editor');
|
|
|
|
result.roles.at(2).get('name').should.eql('Author');
|
2018-02-07 12:46:22 +03:00
|
|
|
result.roles.at(3).get('name').should.eql('Contributor');
|
|
|
|
result.roles.at(4).get('name').should.eql('Owner');
|
2018-10-02 19:46:38 +03:00
|
|
|
result.roles.at(5).get('name').should.eql('Admin Integration');
|
2019-08-05 05:57:47 +03:00
|
|
|
result.roles.at(6).get('name').should.eql('DB Backup Integration');
|
2019-08-06 15:56:47 +03:00
|
|
|
result.roles.at(7).get('name').should.eql('Scheduler Integration');
|
2017-01-25 16:47:49 +03:00
|
|
|
|
|
|
|
// Permissions
|
2020-10-16 14:53:09 +03:00
|
|
|
result.permissions.length.should.eql(80);
|
2017-01-25 16:47:49 +03:00
|
|
|
result.permissions.toJSON().should.be.CompletePermissions();
|
2017-11-28 21:32:03 +03:00
|
|
|
});
|
2016-04-08 12:09:26 +03:00
|
|
|
});
|
2015-08-18 16:08:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|