2014-06-03 17:05:25 +04:00
|
|
|
// # Users API
|
|
|
|
// RESTful API for the User resource
|
2017-09-12 18:31:14 +03:00
|
|
|
var Promise = require('bluebird'),
|
|
|
|
_ = require('lodash'),
|
2017-12-14 00:20:02 +03:00
|
|
|
pipeline = require('../lib/promise/pipeline'),
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils = require('./utils'),
|
2017-12-14 05:01:23 +03:00
|
|
|
canThis = require('../services/permissions').canThis,
|
2017-09-12 18:31:14 +03:00
|
|
|
models = require('../models'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../lib/common'),
|
2017-09-12 18:31:14 +03:00
|
|
|
docName = 'users',
|
2014-07-08 20:00:59 +04:00
|
|
|
// TODO: implement created_by, updated_by
|
2015-11-20 15:04:49 +03:00
|
|
|
allowedIncludes = ['count.posts', 'permissions', 'roles', 'roles.permissions'],
|
2016-09-21 17:48:14 +03:00
|
|
|
users;
|
2014-07-31 04:15:34 +04:00
|
|
|
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
2015-06-22 23:11:35 +03:00
|
|
|
* ### Users API Methods
|
2014-06-03 17:05:25 +04:00
|
|
|
*
|
2017-12-14 16:13:40 +03:00
|
|
|
* **See:** [API Methods](constants.js.html#api%20methods)
|
2014-06-03 17:05:25 +04:00
|
|
|
*/
|
2013-12-06 12:51:35 +04:00
|
|
|
users = {
|
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
|
|
|
/**
|
|
|
|
* ## Browse
|
|
|
|
* Fetch all users
|
2014-06-03 17:05:25 +04:00
|
|
|
* @param {{context}} options (optional)
|
2015-06-22 23:11:35 +03:00
|
|
|
* @returns {Promise<Users>} Users Collection
|
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-12-06 12:51:35 +04:00
|
|
|
browse: function browse(options) {
|
2015-10-24 23:39:47 +03:00
|
|
|
var extraOptions = ['status'],
|
2017-12-14 00:14:19 +03:00
|
|
|
permittedOptions = localUtils.browseDefaultOptions.concat(extraOptions),
|
2015-07-01 21:17:56 +03:00
|
|
|
tasks;
|
2015-06-22 23:11:35 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Model Query
|
|
|
|
* Make the call to the Model layer
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Object} options
|
|
|
|
*/
|
|
|
|
function doQuery(options) {
|
2017-09-12 18:31:14 +03:00
|
|
|
return models.User.findPage(options);
|
2015-06-22 23:11:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push all of our tasks into a `tasks` array in the correct order
|
2015-07-01 21:17:56 +03:00
|
|
|
tasks = [
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils.validate(docName, {opts: permittedOptions}),
|
|
|
|
localUtils.convertOptions(allowedIncludes),
|
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
|
|
|
localUtils.handlePublicPermissions(docName, 'browse'),
|
2015-07-01 21:17:56 +03:00
|
|
|
doQuery
|
|
|
|
];
|
2015-06-22 23:11:35 +03:00
|
|
|
|
|
|
|
// Pipeline calls each task passing the result of one to be the arguments for the next
|
|
|
|
return pipeline(tasks, options);
|
2013-12-06 12:51:35 +04:00
|
|
|
},
|
|
|
|
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
2015-06-22 23:11:35 +03:00
|
|
|
* ## Read
|
2014-06-03 17:05:25 +04:00
|
|
|
* @param {{id, context}} options
|
2015-06-22 23:11:35 +03:00
|
|
|
* @returns {Promise<Users>} User
|
2014-06-03 17:05:25 +04:00
|
|
|
*/
|
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
|
|
|
read: function read(options) {
|
2015-07-01 21:17:56 +03:00
|
|
|
var attrs = ['id', 'slug', 'status', 'email', 'role'],
|
2017-09-27 06:07:39 +03:00
|
|
|
tasks;
|
2015-06-22 23:11:35 +03:00
|
|
|
|
2017-02-23 21:04:24 +03:00
|
|
|
// Special handling for /users/me request
|
2017-09-27 06:07:39 +03:00
|
|
|
if (options.id === 'me' && options.context && options.context.user) {
|
2015-06-27 21:09:25 +03:00
|
|
|
options.id = options.context.user;
|
2014-07-08 20:00:59 +04:00
|
|
|
}
|
|
|
|
|
2015-06-22 23:11:35 +03:00
|
|
|
/**
|
|
|
|
* ### Model Query
|
|
|
|
* Make the call to the Model layer
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Object} options
|
|
|
|
*/
|
|
|
|
function doQuery(options) {
|
2017-09-27 11:31:41 +03:00
|
|
|
return models.User.findOne(options.data, _.omit(options, ['data']))
|
|
|
|
.then(function onModelResponse(model) {
|
|
|
|
if (!model) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.NotFoundError({
|
|
|
|
message: common.i18n.t('errors.api.users.userNotFound')
|
2017-09-27 11:31:41 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
users: [model.toJSON(options)]
|
|
|
|
};
|
|
|
|
});
|
2013-12-06 12:51:35 +04:00
|
|
|
}
|
|
|
|
|
2015-06-22 23:11:35 +03:00
|
|
|
// Push all of our tasks into a `tasks` array in the correct order
|
2015-07-01 21:17:56 +03:00
|
|
|
tasks = [
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils.validate(docName, {attrs: attrs}),
|
|
|
|
localUtils.convertOptions(allowedIncludes),
|
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
|
|
|
localUtils.handlePublicPermissions(docName, 'read'),
|
2015-07-01 21:17:56 +03:00
|
|
|
doQuery
|
|
|
|
];
|
2015-06-22 23:11:35 +03:00
|
|
|
|
|
|
|
// Pipeline calls each task passing the result of one to be the arguments for the next
|
2017-09-27 11:31:41 +03:00
|
|
|
return pipeline(tasks, options);
|
2013-12-06 12:51:35 +04:00
|
|
|
},
|
|
|
|
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
2015-06-22 23:11:35 +03:00
|
|
|
* ## Edit
|
2014-06-03 17:05:25 +04:00
|
|
|
* @param {User} object the user details to edit
|
|
|
|
* @param {{id, context}} options
|
2015-06-22 23:11:35 +03:00
|
|
|
* @returns {Promise<User>}
|
2014-06-03 17:05:25 +04:00
|
|
|
*/
|
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
|
|
|
edit: function edit(object, options) {
|
2015-07-01 21:17:56 +03:00
|
|
|
var extraOptions = ['editRoles'],
|
2017-12-14 00:14:19 +03:00
|
|
|
permittedOptions = extraOptions.concat(localUtils.idDefaultOptions),
|
2015-07-01 21:17:56 +03:00
|
|
|
tasks;
|
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
|
|
|
|
2015-07-01 21:17:56 +03:00
|
|
|
if (object.users && object.users[0] && object.users[0].roles && object.users[0].roles[0]) {
|
|
|
|
options.editRoles = true;
|
2014-07-24 13:46:05 +04:00
|
|
|
}
|
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
|
|
|
|
2015-09-23 21:44:38 +03:00
|
|
|
// The password should never be set via this endpoint, if it is passed, ignore it
|
|
|
|
if (object.users && object.users[0] && object.users[0].password) {
|
|
|
|
delete object.users[0].password;
|
|
|
|
}
|
|
|
|
|
2015-06-22 23:11:35 +03:00
|
|
|
/**
|
|
|
|
* ### Handle Permissions
|
|
|
|
* We need to be an authorised user to perform this action
|
|
|
|
* Edit user allows the related role object to be updated as well, with some rules:
|
|
|
|
* - No change permitted to the role of the owner
|
|
|
|
* - no change permitted to the role of the context user (user making the request)
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Object} options
|
|
|
|
*/
|
|
|
|
function handlePermissions(options) {
|
|
|
|
if (options.id === 'me' && options.context && options.context.user) {
|
|
|
|
options.id = options.context.user;
|
|
|
|
}
|
2014-07-08 20:00:59 +04:00
|
|
|
|
2014-07-24 13:46:05 +04:00
|
|
|
return canThis(options.context).edit.user(options.id).then(function () {
|
2017-03-13 15:03:26 +03:00
|
|
|
// CASE: can't edit my own status to inactive or locked
|
|
|
|
if (options.id === options.context.user) {
|
2017-09-12 18:31:14 +03:00
|
|
|
if (models.User.inactiveStates.indexOf(options.data.users[0].status) !== -1) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.NoPermissionError({
|
|
|
|
message: common.i18n.t('errors.api.users.cannotChangeStatus')
|
2017-03-13 15:03:26 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: if roles aren't in the payload, proceed with the edit
|
2015-06-22 23:11:35 +03:00
|
|
|
if (!(options.data.users[0].roles && options.data.users[0].roles[0])) {
|
|
|
|
return options;
|
2015-01-20 22:21:50 +03:00
|
|
|
}
|
|
|
|
|
2015-06-22 23:11:35 +03:00
|
|
|
// @TODO move role permissions out of here
|
|
|
|
var role = options.data.users[0].roles[0],
|
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
|
|
|
roleId = role.id || role,
|
|
|
|
editedUserId = options.id;
|
2015-01-20 22:21:50 +03:00
|
|
|
|
2017-09-12 18:31:14 +03:00
|
|
|
return models.User.findOne(
|
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
|
|
|
{id: options.context.user, status: 'all'}, {withRelated: ['roles']}
|
2015-01-20 22:21:50 +03:00
|
|
|
).then(function (contextUser) {
|
2015-04-18 00:27:04 +03:00
|
|
|
var contextRoleId = contextUser.related('roles').toJSON(options)[0].id;
|
2015-01-20 22:21:50 +03:00
|
|
|
|
|
|
|
if (roleId !== contextRoleId && editedUserId === contextUser.id) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.NoPermissionError({
|
|
|
|
message: common.i18n.t('errors.api.users.cannotChangeOwnRole')
|
2017-03-13 15:03:26 +03:00
|
|
|
}));
|
2015-01-20 22:21:50 +03:00
|
|
|
}
|
|
|
|
|
2017-09-12 18:31:14 +03:00
|
|
|
return models.User.findOne({role: 'Owner'}).then(function (owner) {
|
2015-01-20 22:21:50 +03:00
|
|
|
if (contextUser.id !== owner.id) {
|
|
|
|
if (editedUserId === owner.id) {
|
|
|
|
if (owner.related('roles').at(0).id !== roleId) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.NoPermissionError({
|
|
|
|
message: common.i18n.t('errors.api.users.cannotChangeOwnersRole')
|
2017-03-13 15:03:26 +03:00
|
|
|
}));
|
2014-08-13 23:12:50 +04:00
|
|
|
}
|
2015-01-20 22:21:50 +03:00
|
|
|
} else if (roleId !== contextRoleId) {
|
|
|
|
return canThis(options.context).assign.role(role).then(function () {
|
2015-06-22 23:11:35 +03:00
|
|
|
return options;
|
2015-01-20 22:21:50 +03:00
|
|
|
});
|
|
|
|
}
|
2014-07-31 04:15:34 +04:00
|
|
|
}
|
|
|
|
|
2015-06-22 23:11:35 +03:00
|
|
|
return options;
|
2014-07-24 13:46:05 +04:00
|
|
|
});
|
2015-01-20 22:21:50 +03:00
|
|
|
});
|
2016-10-04 18:33:43 +03:00
|
|
|
}).catch(function handleError(err) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.NoPermissionError({
|
2016-10-06 15:27:35 +03:00
|
|
|
err: err,
|
2017-12-12 00:47:46 +03:00
|
|
|
context: common.i18n.t('errors.api.users.noPermissionToEditUser')
|
2016-10-06 15:27:35 +03:00
|
|
|
}));
|
2014-06-20 13:15:01 +04:00
|
|
|
});
|
2015-06-22 23:11:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Model Query
|
|
|
|
* Make the call to the Model layer
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Object} options
|
|
|
|
*/
|
|
|
|
function doQuery(options) {
|
2017-09-27 11:31:41 +03:00
|
|
|
return models.User.edit(options.data.users[0], _.omit(options, ['data']))
|
|
|
|
.then(function onModelResponse(model) {
|
|
|
|
if (!model) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.NotFoundError({
|
|
|
|
message: common.i18n.t('errors.api.users.userNotFound')
|
2017-09-27 11:31:41 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
users: [model.toJSON(options)]
|
|
|
|
};
|
|
|
|
});
|
2015-06-22 23:11:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push all of our tasks into a `tasks` array in the correct order
|
2015-07-01 21:17:56 +03:00
|
|
|
tasks = [
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils.validate(docName, {opts: permittedOptions}),
|
|
|
|
localUtils.convertOptions(allowedIncludes),
|
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
|
|
|
handlePermissions,
|
2015-07-01 21:17:56 +03:00
|
|
|
doQuery
|
|
|
|
];
|
2015-06-22 23:11:35 +03:00
|
|
|
|
2017-09-27 11:31:41 +03:00
|
|
|
return pipeline(tasks, object, options);
|
2014-06-20 13:15:01 +04:00
|
|
|
},
|
2014-04-03 17:03:09 +04:00
|
|
|
|
2014-07-24 13:46:05 +04:00
|
|
|
/**
|
2015-06-22 23:11:35 +03:00
|
|
|
* ## Destroy
|
2014-07-24 13:46:05 +04:00
|
|
|
* @param {{id, context}} options
|
2016-03-20 20:26:42 +03:00
|
|
|
* @returns {Promise}
|
2014-07-24 13:46:05 +04:00
|
|
|
*/
|
|
|
|
destroy: function destroy(options) {
|
2015-06-22 23:11:35 +03:00
|
|
|
var tasks;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Handle Permissions
|
|
|
|
* We need to be an authorised user to perform this action
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Object} options
|
|
|
|
*/
|
|
|
|
function handlePermissions(options) {
|
|
|
|
return canThis(options.context).destroy.user(options.id).then(function permissionGranted() {
|
|
|
|
options.status = 'all';
|
|
|
|
return options;
|
2016-10-04 18:33:43 +03:00
|
|
|
}).catch(function handleError(err) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.NoPermissionError({
|
2016-10-06 15:27:35 +03:00
|
|
|
err: err,
|
2017-12-12 00:47:46 +03:00
|
|
|
context: common.i18n.t('errors.api.users.noPermissionToDestroyUser')
|
2016-10-06 15:27:35 +03:00
|
|
|
}));
|
2015-06-22 23:11:35 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-20 20:26:42 +03:00
|
|
|
* ### Delete User
|
2015-06-22 23:11:35 +03:00
|
|
|
* Make the call to the Model layer
|
|
|
|
* @param {Object} options
|
|
|
|
*/
|
2016-03-20 20:26:42 +03:00
|
|
|
function deleteUser(options) {
|
2017-09-12 18:31:14 +03:00
|
|
|
return models.Base.transaction(function (t) {
|
2016-03-20 20:26:42 +03:00
|
|
|
options.transacting = t;
|
|
|
|
|
|
|
|
return Promise.all([
|
2017-09-12 18:31:14 +03:00
|
|
|
models.Accesstoken.destroyByUser(options),
|
|
|
|
models.Refreshtoken.destroyByUser(options),
|
|
|
|
models.Post.destroyByAuthor(options)
|
2016-03-20 20:26:42 +03:00
|
|
|
]).then(function () {
|
2017-09-12 18:31:14 +03:00
|
|
|
return models.User.destroy(options);
|
2016-03-20 20:26:42 +03:00
|
|
|
}).return(null);
|
2016-10-04 18:33:43 +03:00
|
|
|
}).catch(function (err) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.NoPermissionError({
|
2016-10-06 15:27:35 +03:00
|
|
|
err: err
|
|
|
|
}));
|
2014-07-24 13:46:05 +04:00
|
|
|
});
|
2015-06-22 23:11:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push all of our tasks into a `tasks` array in the correct order
|
2015-07-01 21:17:56 +03:00
|
|
|
tasks = [
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils.validate(docName, {opts: localUtils.idDefaultOptions}),
|
|
|
|
localUtils.convertOptions(allowedIncludes),
|
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
|
|
|
handlePermissions,
|
2016-03-20 20:26:42 +03:00
|
|
|
deleteUser
|
2015-07-01 21:17:56 +03:00
|
|
|
];
|
2015-06-22 23:11:35 +03:00
|
|
|
|
|
|
|
// Pipeline calls each task passing the result of one to be the arguments for the next
|
|
|
|
return pipeline(tasks, options);
|
2014-07-24 13:46:05 +04:00
|
|
|
},
|
|
|
|
|
2014-06-20 13:15:01 +04:00
|
|
|
/**
|
2015-06-22 23:11:35 +03:00
|
|
|
* ## Change Password
|
2014-07-09 02:28:31 +04:00
|
|
|
* @param {password} object
|
2014-06-20 13:15:01 +04:00
|
|
|
* @param {{context}} options
|
2015-06-22 23:11:35 +03:00
|
|
|
* @returns {Promise<password>} success message
|
2014-06-20 13:15:01 +04:00
|
|
|
*/
|
|
|
|
changePassword: function changePassword(object, options) {
|
2015-06-22 23:11:35 +03:00
|
|
|
var tasks;
|
|
|
|
|
2016-11-07 14:18:50 +03:00
|
|
|
function validateRequest() {
|
2017-12-14 00:14:19 +03:00
|
|
|
return localUtils.validate('password')(object, options)
|
2016-11-07 14:18:50 +03:00
|
|
|
.then(function (options) {
|
|
|
|
var data = options.data.password[0];
|
|
|
|
|
|
|
|
if (data.newPassword !== data.ne2Password) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.ValidationError({
|
|
|
|
message: common.i18n.t('errors.models.user.newPasswordsDoNotMatch')
|
2016-11-07 14:18:50 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve(options);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-22 23:11:35 +03:00
|
|
|
/**
|
|
|
|
* ### Handle Permissions
|
|
|
|
* We need to be an authorised user to perform this action
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Object} options
|
|
|
|
*/
|
|
|
|
function handlePermissions(options) {
|
2015-07-07 16:32:50 +03:00
|
|
|
return canThis(options.context).edit.user(options.data.password[0].user_id).then(function permissionGranted() {
|
2015-06-22 23:11:35 +03:00
|
|
|
return options;
|
2016-10-04 18:33:43 +03:00
|
|
|
}).catch(function (err) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.NoPermissionError({
|
2016-10-06 15:27:35 +03:00
|
|
|
err: err,
|
2017-12-12 00:47:46 +03:00
|
|
|
context: common.i18n.t('errors.api.users.noPermissionToChangeUsersPwd')
|
2016-10-06 15:27:35 +03:00
|
|
|
}));
|
2015-06-22 23:11:35 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Model Query
|
|
|
|
* Make the call to the Model layer
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Object} options
|
|
|
|
*/
|
|
|
|
function doQuery(options) {
|
2017-09-12 18:31:14 +03:00
|
|
|
return models.User.changePassword(
|
2015-07-07 16:32:50 +03:00
|
|
|
options.data.password[0],
|
2015-06-22 23:11:35 +03:00
|
|
|
_.omit(options, ['data'])
|
2017-09-27 11:31:41 +03:00
|
|
|
).then(function onModelResponse() {
|
|
|
|
return Promise.resolve({
|
2017-12-12 00:47:46 +03:00
|
|
|
password: [{message: common.i18n.t('notices.api.users.pwdChangedSuccessfully')}]
|
2017-09-27 11:31:41 +03:00
|
|
|
});
|
|
|
|
});
|
2015-06-22 23:11:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push all of our tasks into a `tasks` array in the correct order
|
2015-07-01 21:17:56 +03:00
|
|
|
tasks = [
|
2016-11-07 14:18:50 +03:00
|
|
|
validateRequest,
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils.convertOptions(allowedIncludes),
|
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
|
|
|
handlePermissions,
|
2015-07-01 21:17:56 +03:00
|
|
|
doQuery
|
|
|
|
];
|
2015-06-22 23:11:35 +03:00
|
|
|
|
|
|
|
// Pipeline calls each task passing the result of one to be the arguments for the next
|
2017-09-27 11:31:41 +03:00
|
|
|
return pipeline(tasks, object, options);
|
2014-07-30 19:40:30 +04:00
|
|
|
},
|
2013-12-06 12:51:35 +04:00
|
|
|
|
2014-07-30 19:40:30 +04:00
|
|
|
/**
|
2015-06-22 23:11:35 +03:00
|
|
|
* ## Transfer Ownership
|
|
|
|
* @param {owner} object
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Promise<User>}
|
2014-07-30 19:40:30 +04:00
|
|
|
*/
|
|
|
|
transferOwnership: function transferOwnership(object, options) {
|
2015-06-22 23:11:35 +03:00
|
|
|
var tasks;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Handle Permissions
|
|
|
|
* We need to be an authorised user to perform this action
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Object} options
|
|
|
|
*/
|
|
|
|
function handlePermissions(options) {
|
2017-09-12 18:31:14 +03:00
|
|
|
return models.Role.findOne({name: 'Owner'}).then(function (ownerRole) {
|
2015-06-22 23:11:35 +03:00
|
|
|
return canThis(options.context).assign.role(ownerRole);
|
|
|
|
}).then(function () {
|
|
|
|
return options;
|
2014-07-30 19:40:30 +04:00
|
|
|
});
|
2015-06-22 23:11:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Model Query
|
|
|
|
* Make the call to the Model layer
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Object} options
|
|
|
|
*/
|
|
|
|
function doQuery(options) {
|
2017-09-27 11:31:41 +03:00
|
|
|
return models.User.transferOwnership(options.data.owner[0], _.omit(options, ['data']))
|
|
|
|
.then(function onModelResponse(model) {
|
|
|
|
// NOTE: model returns json object already
|
|
|
|
// @TODO: why?
|
|
|
|
return {
|
|
|
|
users: model
|
|
|
|
};
|
|
|
|
});
|
2015-06-22 23:11:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push all of our tasks into a `tasks` array in the correct order
|
2015-07-01 21:17:56 +03:00
|
|
|
tasks = [
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils.validate('owner'),
|
|
|
|
localUtils.convertOptions(allowedIncludes),
|
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
|
|
|
handlePermissions,
|
2015-07-01 21:17:56 +03:00
|
|
|
doQuery
|
|
|
|
];
|
2015-06-22 23:11:35 +03:00
|
|
|
|
|
|
|
// Pipeline calls each task passing the result of one to be the arguments for the next
|
2017-09-27 11:31:41 +03:00
|
|
|
return pipeline(tasks, object, options);
|
2014-07-30 19:40:30 +04:00
|
|
|
}
|
2013-12-06 12:51:35 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = users;
|