From 83e1ffca1d49647c079fbe39bf36fe8375eab210 Mon Sep 17 00:00:00 2001 From: Sebastian Gierlinger Date: Tue, 29 Jul 2014 15:35:48 +0200 Subject: [PATCH] Fix Invitations no issue - added `invited-pending` when resending invitation - promise chain was missing a return statement - email error was masked and front end showed success notification --- .../controllers/modals/invite-new-user.js | 3 +- .../client/controllers/settings/users/user.js | 12 +++- core/server/api/mail.js | 25 ++++---- core/server/api/users.js | 59 ++++++++++--------- 4 files changed, 54 insertions(+), 45 deletions(-) diff --git a/core/client/controllers/modals/invite-new-user.js b/core/client/controllers/modals/invite-new-user.js index 092ca43952..b4d4f7d45c 100644 --- a/core/client/controllers/modals/invite-new-user.js +++ b/core/client/controllers/modals/invite-new-user.js @@ -56,8 +56,7 @@ var InviteNewUserController = Ember.Controller.extend({ // but the user's status in the response object will be 'invited-pending'. if (newUser.get('status') === 'invited-pending') { self.notifications.showWarn('Invitation email was not sent. Please try resending.'); - } - else { + } else { self.notifications.showSuccess(notificationText, false); } }).catch(function (errors) { diff --git a/core/client/controllers/settings/users/user.js b/core/client/controllers/settings/users/user.js index c1506b5a55..7dd4e421cd 100644 --- a/core/client/controllers/settings/users/user.js +++ b/core/client/controllers/settings/users/user.js @@ -66,10 +66,16 @@ var SettingsUserController = Ember.ObjectController.extend({ resend: function () { var self = this; - this.get('model').resendInvite().then(function () { - self.get('model').set('status', 'invited'); + this.get('model').resendInvite().then(function (result) { var notificationText = 'Invitation resent! (' + self.get('email') + ')'; - self.notifications.showSuccess(notificationText, false); + // If sending the invitation email fails, the API will still return a status of 201 + // but the user's status in the response object will be 'invited-pending'. + if (result.users[0].status === 'invited-pending') { + self.notifications.showWarn('Invitation email was not sent. Please try resending.'); + } else { + self.get('model').set('status', result.users[0].status); + self.notifications.showSuccess(notificationText, false); + } }).catch(function (error) { self.notifications.closePassive(); self.notifications.showAPIError(error); diff --git a/core/server/api/mail.js b/core/server/api/mail.js index a4f0aead1d..c5614ecd85 100644 --- a/core/server/api/mail.js +++ b/core/server/api/mail.js @@ -1,14 +1,14 @@ // # Mail API // API for sending Mail -var when = require('when'), - _ = require('lodash'), - config = require('../config'), - canThis = require('../permissions').canThis, - errors = require('../errors'), - path = require('path'), - fs = require('fs'), - templatesDir = path.resolve(__dirname, '..', 'email-templates'), - htmlToText = require('html-to-text'), +var _ = require('lodash'), + when = require('when'), + config = require('../config'), + canThis = require('../permissions').canThis, + errors = require('../errors'), + path = require('path'), + fs = require('fs'), + templatesDir = path.resolve(__dirname, '..', 'email-templates'), + htmlToText = require('html-to-text'), mail; /** @@ -105,9 +105,10 @@ mail = { //generate a plain-text version of the same email textContent = htmlToText.fromString(htmlContent); - resolve({ html: htmlContent, - text: textContent - }); + resolve({ + html: htmlContent, + text: textContent + }); }); }); diff --git a/core/server/api/users.js b/core/server/api/users.js index a377e1f893..75e745c319 100644 --- a/core/server/api/users.js +++ b/core/server/api/users.js @@ -132,7 +132,8 @@ users = { add: function add(object, options) { var newUser, user, - roleId; + roleId, + emailData; return canThis(options.context).add.user(object).then(function () { return utils.checkObject(object, docName).then(function (checkedUserData) { @@ -182,39 +183,41 @@ users = { dbHash = response.settings[0].value; return dataProvider.User.generateResetToken(user.email, expires, dbHash); }).then(function (resetToken) { - when.join(users.read({'id': user.created_by}), settings.read({'key': 'title'})).then(function (values) { + return when.join(users.read({'id': user.created_by}), settings.read({'key': 'title'})).then(function (values) { var invitedBy = values[0].users[0], blogTitle = values[1].settings[0].value, baseUrl = config.forceAdminSSL ? (config.urlSSL || config.url) : config.url, - resetUrl = baseUrl.replace(/\/$/, '') + '/ghost/signup/' + resetToken + '/', - emailData = { - blogName: blogTitle, - invitedByName: invitedBy.name, - invitedByEmail: invitedBy.email, - resetLink: resetUrl - }; + resetUrl = baseUrl.replace(/\/$/, '') + '/ghost/signup/' + resetToken + '/'; - mail.generateContent({data: emailData, template: 'invite-user'}).then(function (emailContent) { + emailData = { + blogName: blogTitle, + invitedByName: invitedBy.name, + invitedByEmail: invitedBy.email, + resetLink: resetUrl + }; - var payload = { - mail: [ - { - message: { - to: user.email, - subject: emailData.invitedByName + ' has invited you to join ' + emailData.blogName, - html: emailContent.html, - text: emailContent.text - }, - options: {} - } - ] - }; - return mail.send(payload, {context: {internal: true}}).then(function () { - // If status was invited-pending and sending the invitation succeeded, set status to invited. - if (user.status === 'invited-pending') { - return dataProvider.User.edit({status: 'invited'}, {id: user.id}); + return mail.generateContent({data: emailData, template: 'invite-user'}); + }).then(function (emailContent) { + var payload = { + mail: [ + { + message: { + to: user.email, + subject: emailData.invitedByName + ' has invited you to join ' + emailData.blogName, + html: emailContent.html, + text: emailContent.text + }, + options: {} } - }); + ] + }; + return mail.send(payload, {context: {internal: true}}).then(function () { + // If status was invited-pending and sending the invitation succeeded, set status to invited. + if (user.status === 'invited-pending') { + return dataProvider.User.edit({status: 'invited'}, {id: user.id}).then(function (editedUser) { + user = editedUser.toJSON(); + }); + } }); }); }).then(function () {