2019-07-17 13:28:16 +03:00
|
|
|
const _ = require('lodash');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../shared/config');
|
2020-04-30 22:26:12 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-05-28 21:30:23 +03:00
|
|
|
const {i18n} = require('../../lib/common');
|
|
|
|
const logging = require('../../../shared/logging');
|
2019-07-17 13:28:16 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
const mail = require('../mail');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns setup status
|
|
|
|
*
|
|
|
|
* @return {Promise<Boolean>}
|
|
|
|
*/
|
|
|
|
async function checkIsSetup() {
|
|
|
|
return models.User.isSetup();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows an assertion to be made about setup status.
|
|
|
|
*
|
|
|
|
* @param {Boolean} status True: setup must be complete. False: setup must not be complete.
|
|
|
|
* @return {Function} returns a "task ready" function
|
|
|
|
*/
|
|
|
|
function assertSetupCompleted(status) {
|
|
|
|
return async function checkPermission(__) {
|
|
|
|
const isSetup = await checkIsSetup();
|
|
|
|
|
|
|
|
if (isSetup === status) {
|
|
|
|
return __;
|
|
|
|
}
|
|
|
|
|
2020-04-30 22:26:12 +03:00
|
|
|
const completed = i18n.t('errors.api.authentication.setupAlreadyCompleted');
|
|
|
|
const notCompleted = i18n.t('errors.api.authentication.setupMustBeCompleted');
|
2019-07-17 13:28:16 +03:00
|
|
|
|
|
|
|
function throwReason(reason) {
|
2020-04-30 22:26:12 +03:00
|
|
|
throw new errors.NoPermissionError({message: reason});
|
2019-07-17 13:28:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isSetup) {
|
|
|
|
throwReason(completed);
|
|
|
|
} else {
|
|
|
|
throwReason(notCompleted);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setupUser(userData) {
|
|
|
|
const context = {context: {internal: true}};
|
|
|
|
|
|
|
|
const owner = await models.User.findOne({role: 'Owner', status: 'all'});
|
|
|
|
|
|
|
|
if (!owner) {
|
2020-04-30 22:26:12 +03:00
|
|
|
throw new errors.GhostError({
|
|
|
|
message: i18n.t('errors.api.authentication.setupUnableToRun')
|
2019-07-17 13:28:16 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await models.User.setup(userData, _.extend({id: owner.id}, context));
|
|
|
|
|
|
|
|
return {
|
|
|
|
user: user,
|
|
|
|
userData: userData
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function doSettings(data, settingsAPI) {
|
|
|
|
const context = {context: {user: data.user.id}};
|
|
|
|
const user = data.user;
|
|
|
|
const blogTitle = data.userData.blogTitle;
|
|
|
|
|
|
|
|
let userSettings;
|
|
|
|
|
|
|
|
if (!blogTitle || typeof blogTitle !== 'string') {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
userSettings = [
|
|
|
|
{key: 'title', value: blogTitle.trim()},
|
2020-04-30 22:26:12 +03:00
|
|
|
{key: 'description', value: i18n.t('common.api.authentication.sampleBlogDescription')}
|
2019-07-17 13:28:16 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
await settingsAPI.edit({settings: userSettings}, context);
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2019-07-30 17:15:53 +03:00
|
|
|
function sendWelcomeEmail(email, mailAPI) {
|
2019-07-17 13:28:16 +03:00
|
|
|
if (config.get('sendWelcomeEmail')) {
|
2019-07-30 17:13:53 +03:00
|
|
|
const data = {
|
|
|
|
ownerEmail: email
|
|
|
|
};
|
|
|
|
|
2019-07-17 13:28:16 +03:00
|
|
|
return mail.utils.generateContent({data: data, template: 'welcome'})
|
|
|
|
.then((content) => {
|
|
|
|
const message = {
|
2020-04-29 18:44:27 +03:00
|
|
|
to: email,
|
2020-04-30 22:26:12 +03:00
|
|
|
subject: i18n.t('common.api.authentication.mail.yourNewGhostBlog'),
|
2020-04-29 18:44:27 +03:00
|
|
|
html: content.html,
|
|
|
|
text: content.text
|
|
|
|
};
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
mail: [{
|
|
|
|
message: message,
|
|
|
|
options: {}
|
|
|
|
}]
|
|
|
|
};
|
2019-07-17 13:28:16 +03:00
|
|
|
|
|
|
|
mailAPI.send(payload, {context: {internal: true}})
|
|
|
|
.catch((err) => {
|
2020-04-30 22:26:12 +03:00
|
|
|
err.context = i18n.t('errors.api.authentication.unableToSendWelcomeEmail');
|
|
|
|
logging.error(err);
|
2019-07-17 13:28:16 +03:00
|
|
|
});
|
2019-07-30 17:13:53 +03:00
|
|
|
});
|
2019-07-17 13:28:16 +03:00
|
|
|
}
|
2019-08-20 13:13:43 +03:00
|
|
|
return Promise.resolve();
|
2019-07-17 13:28:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
checkIsSetup: checkIsSetup,
|
|
|
|
assertSetupCompleted: assertSetupCompleted,
|
|
|
|
setupUser: setupUser,
|
|
|
|
doSettings: doSettings,
|
2019-07-30 17:15:53 +03:00
|
|
|
sendWelcomeEmail: sendWelcomeEmail
|
2019-07-17 13:28:16 +03:00
|
|
|
};
|