2018-09-10 15:30:48 +03:00
|
|
|
const Promise = require('bluebird'),
|
|
|
|
{omit, merge} = require('lodash'),
|
2018-09-27 17:06:57 +03:00
|
|
|
pipeline = require('../../lib/promise/pipeline'),
|
|
|
|
mail = require('../../services/mail'),
|
2019-06-18 16:13:55 +03:00
|
|
|
urlUtils = require('../../lib/url-utils'),
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils = require('./utils'),
|
2018-09-27 17:06:57 +03:00
|
|
|
models = require('../../models'),
|
|
|
|
common = require('../../lib/common'),
|
|
|
|
security = require('../../lib/security'),
|
2017-09-12 18:31:14 +03:00
|
|
|
mailAPI = require('./mail'),
|
|
|
|
settingsAPI = require('./settings'),
|
2016-09-21 17:48:14 +03:00
|
|
|
docName = 'invites',
|
2018-10-05 16:38:14 +03:00
|
|
|
allowedIncludes = ['created_by', 'updated_by'],
|
|
|
|
unsafeAttrs = ['role_id'];
|
2016-09-21 17:48:14 +03:00
|
|
|
|
2018-09-10 15:30:48 +03:00
|
|
|
const invites = {
|
2018-09-18 16:59:56 +03:00
|
|
|
browse(options) {
|
2018-09-10 15:30:48 +03:00
|
|
|
let tasks;
|
2016-09-21 17:48:14 +03:00
|
|
|
|
|
|
|
function modelQuery(options) {
|
2018-09-26 15:11:22 +03:00
|
|
|
return models.Invite.findPage(options)
|
|
|
|
.then(({data, meta}) => {
|
|
|
|
return {
|
|
|
|
invites: data.map(model => model.toJSON(options)),
|
|
|
|
meta: meta
|
|
|
|
};
|
|
|
|
});
|
2016-09-21 17:48:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tasks = [
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils.validate(docName, {opts: localUtils.browseDefaultOptions}),
|
|
|
|
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'),
|
2016-09-21 17:48:14 +03:00
|
|
|
modelQuery
|
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks, options);
|
|
|
|
},
|
|
|
|
|
2018-09-18 16:59:56 +03:00
|
|
|
read(options) {
|
2018-09-10 15:30:48 +03:00
|
|
|
const attrs = ['id', 'email'];
|
|
|
|
let tasks;
|
2016-09-21 17:48:14 +03:00
|
|
|
|
|
|
|
function modelQuery(options) {
|
2018-09-10 15:30:48 +03:00
|
|
|
return models.Invite.findOne(options.data, omit(options, ['data']))
|
2018-09-18 16:59:56 +03:00
|
|
|
.then((model) => {
|
2017-09-27 11:31:41 +03:00
|
|
|
if (!model) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.NotFoundError({
|
|
|
|
message: common.i18n.t('errors.api.invites.inviteNotFound')
|
2017-09-27 11:31:41 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
invites: [model.toJSON(options)]
|
|
|
|
};
|
|
|
|
});
|
2016-09-21 17:48:14 +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'),
|
2016-09-21 17:48:14 +03:00
|
|
|
modelQuery
|
|
|
|
];
|
|
|
|
|
2017-09-27 11:31:41 +03:00
|
|
|
return pipeline(tasks, options);
|
2016-09-21 17:48:14 +03:00
|
|
|
},
|
|
|
|
|
2018-09-18 16:59:56 +03:00
|
|
|
destroy(options) {
|
2018-09-10 15:30:48 +03:00
|
|
|
let tasks;
|
2016-09-21 17:48:14 +03:00
|
|
|
|
|
|
|
function modelQuery(options) {
|
2018-09-10 15:30:48 +03:00
|
|
|
return models.Invite.findOne({id: options.id}, omit(options, ['data']))
|
|
|
|
.then((invite) => {
|
2016-09-21 17:48:14 +03:00
|
|
|
if (!invite) {
|
2017-12-12 00:47:46 +03:00
|
|
|
throw new common.errors.NotFoundError({message: common.i18n.t('errors.api.invites.inviteNotFound')});
|
2016-09-21 17:48:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return invite.destroy(options).return(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
localUtils.handlePermissions(docName, 'destroy'),
|
2016-09-21 17:48:14 +03:00
|
|
|
modelQuery
|
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks, options);
|
|
|
|
},
|
|
|
|
|
2018-09-18 16:59:56 +03:00
|
|
|
add(object, options) {
|
2018-09-10 15:30:48 +03:00
|
|
|
let loggedInUser = options.context.user,
|
|
|
|
tasks,
|
2016-09-21 17:48:14 +03:00
|
|
|
emailData,
|
|
|
|
invite;
|
|
|
|
|
|
|
|
function addInvite(options) {
|
2018-09-10 15:30:48 +03:00
|
|
|
const data = options.data;
|
2016-09-21 17:48:14 +03:00
|
|
|
|
2018-09-10 15:30:48 +03:00
|
|
|
return models.Invite.add(data.invites[0], omit(options, 'data'))
|
|
|
|
.then((_invite) => {
|
2016-09-21 17:48:14 +03:00
|
|
|
invite = _invite;
|
|
|
|
|
2017-09-12 18:31:14 +03:00
|
|
|
return settingsAPI.read({key: 'title'});
|
2016-09-21 17:48:14 +03:00
|
|
|
})
|
2018-09-10 15:30:48 +03:00
|
|
|
.then((response) => {
|
2019-06-18 16:13:55 +03:00
|
|
|
const adminUrl = urlUtils.urlFor('admin', true);
|
2016-09-21 17:48:14 +03:00
|
|
|
|
|
|
|
emailData = {
|
|
|
|
blogName: response.settings[0].value,
|
|
|
|
invitedByName: loggedInUser.get('name'),
|
|
|
|
invitedByEmail: loggedInUser.get('email'),
|
|
|
|
// @TODO: resetLink sounds weird
|
2019-06-18 16:13:55 +03:00
|
|
|
resetLink: urlUtils.urlJoin(adminUrl, 'signup', security.url.encodeBase64(invite.get('token')), '/')
|
2016-09-21 17:48:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return mail.utils.generateContent({data: emailData, template: 'invite-user'});
|
2018-09-10 15:30:48 +03:00
|
|
|
}).then((emailContent) => {
|
|
|
|
const payload = {
|
2016-09-21 17:48:14 +03:00
|
|
|
mail: [{
|
|
|
|
message: {
|
|
|
|
to: invite.get('email'),
|
2017-12-12 00:47:46 +03:00
|
|
|
subject: common.i18n.t('common.api.users.mail.invitedByName', {
|
2016-09-21 17:48:14 +03:00
|
|
|
invitedByName: emailData.invitedByName,
|
|
|
|
blogName: emailData.blogName
|
|
|
|
}),
|
|
|
|
html: emailContent.html,
|
|
|
|
text: emailContent.text
|
|
|
|
},
|
|
|
|
options: {}
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
2017-09-12 18:31:14 +03:00
|
|
|
return mailAPI.send(payload, {context: {internal: true}});
|
2018-09-10 15:30:48 +03:00
|
|
|
}).then(() => {
|
2016-09-21 17:48:14 +03:00
|
|
|
options.id = invite.id;
|
2017-09-12 18:31:14 +03:00
|
|
|
return models.Invite.edit({status: 'sent'}, options);
|
2018-09-10 15:30:48 +03:00
|
|
|
}).then(() => {
|
2016-09-21 17:48:14 +03:00
|
|
|
invite.set('status', 'sent');
|
2018-09-10 15:30:48 +03:00
|
|
|
const inviteAsJSON = invite.toJSON();
|
2017-09-27 11:31:41 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
invites: [inviteAsJSON]
|
|
|
|
};
|
2018-09-10 15:30:48 +03:00
|
|
|
}).catch((error) => {
|
2016-09-21 17:48:14 +03:00
|
|
|
if (error && error.errorType === 'EmailError') {
|
2018-09-10 15:30:48 +03:00
|
|
|
const errorMessage = common.i18n.t('errors.api.invites.errorSendingEmail.error', {
|
|
|
|
message: error.message
|
|
|
|
});
|
|
|
|
const helpText = common.i18n.t('errors.api.invites.errorSendingEmail.help');
|
|
|
|
error.message = `${errorMessage} ${helpText}`;
|
2017-12-12 00:47:46 +03:00
|
|
|
common.logging.warn(error.message);
|
2016-09-21 17:48:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function destroyOldInvite(options) {
|
2018-09-10 15:30:48 +03:00
|
|
|
const data = options.data;
|
2016-09-21 17:48:14 +03:00
|
|
|
|
2018-09-10 15:30:48 +03:00
|
|
|
return models.Invite.findOne({email: data.invites[0].email}, omit(options, 'data'))
|
|
|
|
.then((invite) => {
|
2016-09-21 17:48:14 +03:00
|
|
|
if (!invite) {
|
|
|
|
return Promise.resolve(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return invite.destroy(options);
|
|
|
|
})
|
2018-09-17 21:49:30 +03:00
|
|
|
.then(() => {
|
2018-09-26 15:11:22 +03:00
|
|
|
return options;
|
2018-09-17 21:49:30 +03:00
|
|
|
});
|
2016-09-21 17:48:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function validation(options) {
|
|
|
|
if (!options.data.invites[0].email) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.ValidationError({message: common.i18n.t('errors.api.invites.emailIsRequired')}));
|
2016-09-21 17:48:14 +03:00
|
|
|
}
|
|
|
|
|
2016-11-16 12:33:44 +03:00
|
|
|
if (!options.data.invites[0].role_id) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.ValidationError({message: common.i18n.t('errors.api.invites.roleIsRequired')}));
|
2016-09-21 17:48:14 +03:00
|
|
|
}
|
|
|
|
|
2018-10-05 16:38:14 +03:00
|
|
|
return options;
|
2016-09-21 17:48:14 +03:00
|
|
|
}
|
|
|
|
|
2017-07-14 22:55:49 +03:00
|
|
|
function checkIfUserExists(options) {
|
2017-09-12 18:31:14 +03:00
|
|
|
return models.User.findOne({email: options.data.invites[0].email}, options)
|
2018-09-10 15:30:48 +03:00
|
|
|
.then((user) => {
|
2017-07-14 22:55:49 +03:00
|
|
|
if (user) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.ValidationError({
|
|
|
|
message: common.i18n.t('errors.api.users.userAlreadyRegistered')
|
2017-07-14 22:55:49 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-25 15:07:31 +03:00
|
|
|
function fetchLoggedInUser(options) {
|
2018-09-10 15:30:48 +03:00
|
|
|
return models.User.findOne({id: loggedInUser}, merge({}, omit(options, 'data'), {withRelated: ['roles']}))
|
|
|
|
.then((user) => {
|
2017-01-25 15:07:31 +03:00
|
|
|
if (!user) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.NotFoundError({message: common.i18n.t('errors.api.users.userNotFound')}));
|
2017-01-25 15:07:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
loggedInUser = user;
|
|
|
|
return options;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-21 17:48:14 +03:00
|
|
|
tasks = [
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils.validate(docName, {opts: ['email']}),
|
|
|
|
localUtils.convertOptions(allowedIncludes),
|
2016-09-21 17:48:14 +03:00
|
|
|
validation,
|
2018-10-05 16:38:14 +03:00
|
|
|
localUtils.handlePermissions(docName, 'add', unsafeAttrs),
|
|
|
|
fetchLoggedInUser,
|
2017-07-14 22:55:49 +03:00
|
|
|
checkIfUserExists,
|
2016-09-21 17:48:14 +03:00
|
|
|
destroyOldInvite,
|
|
|
|
addInvite
|
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks, object, options);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = invites;
|