2018-01-11 01:57:43 +03:00
|
|
|
/* eslint-disable camelcase, ghost/ember/alias-model-in-controller */
|
2017-10-30 12:38:01 +03:00
|
|
|
import Controller, {inject as controller} from '@ember/controller';
|
2016-07-06 22:47:30 +03:00
|
|
|
import RSVP from 'rsvp';
|
2017-04-19 13:27:32 +03:00
|
|
|
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
|
2019-02-22 08:29:40 +03:00
|
|
|
import {get} from '@ember/object';
|
2016-09-26 16:07:18 +03:00
|
|
|
import {isInvalidError} from 'ember-ajax/errors';
|
2017-04-19 13:27:32 +03:00
|
|
|
import {isVersionMismatchError} from 'ghost-admin/services/ajax';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2017-04-19 13:27:32 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2015-03-29 21:10:53 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Controller.extend(ValidationEngine, {
|
2017-10-30 12:38:01 +03:00
|
|
|
application: controller(),
|
2018-01-11 20:43:23 +03:00
|
|
|
ajax: service(),
|
2017-10-30 12:38:01 +03:00
|
|
|
config: service(),
|
|
|
|
ghostPaths: service(),
|
|
|
|
notifications: service(),
|
|
|
|
session: service(),
|
|
|
|
settings: service(),
|
2015-05-27 23:10:47 +03:00
|
|
|
|
2015-03-29 21:10:53 +03:00
|
|
|
// ValidationEngine settings
|
|
|
|
validationType: 'setup',
|
|
|
|
|
2017-04-19 13:27:32 +03:00
|
|
|
blogCreated: false,
|
|
|
|
blogTitle: null,
|
|
|
|
email: '',
|
|
|
|
flowErrors: '',
|
2017-04-24 20:22:39 +03:00
|
|
|
profileImage: null,
|
2017-04-19 13:27:32 +03:00
|
|
|
name: null,
|
|
|
|
password: null,
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
actions: {
|
|
|
|
setup() {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.setup.perform();
|
2018-01-11 20:43:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
preValidate(model) {
|
|
|
|
// Only triggers validation if a value has been entered, preventing empty errors on focusOut
|
|
|
|
if (this.get(model)) {
|
|
|
|
return this.validate({property: model});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setImage(image) {
|
|
|
|
this.set('profileImage', image);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-04-19 13:27:32 +03:00
|
|
|
setup: task(function* () {
|
2017-09-04 22:17:04 +03:00
|
|
|
return yield this._passwordSetup();
|
2017-04-19 13:27:32 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
authenticate: task(function* (authStrategy, authentication) {
|
|
|
|
// we don't want to redirect after sign-in during setup
|
|
|
|
this.set('session.skipAuthSuccessHandler', true);
|
|
|
|
|
|
|
|
try {
|
2019-03-06 16:53:54 +03:00
|
|
|
let authResult = yield this.session
|
2017-04-19 13:27:32 +03:00
|
|
|
.authenticate(authStrategy, ...authentication);
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
this.errors.remove('session');
|
2017-04-19 13:27:32 +03:00
|
|
|
|
|
|
|
return authResult;
|
|
|
|
} catch (error) {
|
2017-11-04 01:59:39 +03:00
|
|
|
if (error && error.payload && error.payload.errors) {
|
2017-04-19 13:27:32 +03:00
|
|
|
if (isVersionMismatchError(error)) {
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.notifications.showAPIError(error);
|
2017-04-19 13:27:32 +03:00
|
|
|
}
|
|
|
|
|
2017-11-04 01:59:39 +03:00
|
|
|
error.payload.errors.forEach((err) => {
|
2017-04-19 13:27:32 +03:00
|
|
|
err.message = err.message.htmlSafe();
|
|
|
|
});
|
|
|
|
|
2017-11-04 01:59:39 +03:00
|
|
|
this.set('flowErrors', error.payload.errors[0].message.string);
|
2017-04-19 13:27:32 +03:00
|
|
|
} else {
|
|
|
|
// Connection errors don't return proper status message, only req.body
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAlert('There was a problem on the server.', {type: 'error', key: 'session.authenticate.failed'});
|
2017-04-19 13:27:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2015-05-28 08:52:41 +03:00
|
|
|
/**
|
|
|
|
* Uploads the given data image, then sends the changed user image property to the server
|
|
|
|
* @param {Object} user User object, returned from the 'setup' api call
|
2018-05-03 19:52:39 +03:00
|
|
|
* @return {RSVP.Promise} A promise that takes care of both calls
|
2015-05-28 08:52:41 +03:00
|
|
|
*/
|
2017-04-19 13:27:32 +03:00
|
|
|
_sendImage(user) {
|
2017-08-18 06:27:42 +03:00
|
|
|
let formData = new FormData();
|
2019-03-06 16:53:54 +03:00
|
|
|
let imageFile = this.profileImage;
|
2019-02-26 07:21:24 +03:00
|
|
|
let uploadUrl = this.get('ghostPaths.url').api('images', 'upload');
|
2017-08-18 06:27:42 +03:00
|
|
|
|
2019-02-26 07:21:24 +03:00
|
|
|
formData.append('file', imageFile, imageFile.name);
|
|
|
|
formData.append('purpose', 'profile_image');
|
2017-08-18 06:27:42 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.ajax.post(uploadUrl, {
|
2017-08-18 06:27:42 +03:00
|
|
|
data: formData,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
dataType: 'text'
|
|
|
|
}).then((response) => {
|
2019-02-26 07:21:24 +03:00
|
|
|
let [image] = get(JSON.parse(response), 'images');
|
|
|
|
let imageUrl = image.url;
|
2017-08-18 06:27:42 +03:00
|
|
|
let usersUrl = this.get('ghostPaths.url').api('users', user.id.toString());
|
|
|
|
user.profile_image = imageUrl;
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.ajax.put(usersUrl, {
|
2017-08-18 06:27:42 +03:00
|
|
|
data: {
|
|
|
|
users: [user]
|
|
|
|
}
|
|
|
|
});
|
2015-05-28 08:52:41 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-09-30 14:43:40 +03:00
|
|
|
_passwordSetup() {
|
|
|
|
let setupProperties = ['blogTitle', 'name', 'email', 'password'];
|
|
|
|
let data = this.getProperties(setupProperties);
|
2019-03-06 16:53:54 +03:00
|
|
|
let config = this.config;
|
|
|
|
let method = this.blogCreated ? 'put' : 'post';
|
2016-09-30 14:43:40 +03:00
|
|
|
|
|
|
|
this.set('flowErrors', '');
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
this.hasValidated.addObjects(setupProperties);
|
2016-09-24 18:48:06 +03:00
|
|
|
|
|
|
|
return this.validate().then(() => {
|
2016-09-30 14:43:40 +03:00
|
|
|
let authUrl = this.get('ghostPaths.url').api('authentication', 'setup');
|
2016-09-24 18:48:06 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.ajax[method](authUrl, {
|
2016-09-30 14:43:40 +03:00
|
|
|
data: {
|
|
|
|
setup: [{
|
|
|
|
name: data.name,
|
|
|
|
email: data.email,
|
|
|
|
password: data.password,
|
|
|
|
blogTitle: data.blogTitle
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}).then((result) => {
|
|
|
|
config.set('blogTitle', data.blogTitle);
|
|
|
|
|
|
|
|
// don't try to login again if we are already logged in
|
|
|
|
if (this.get('session.isAuthenticated')) {
|
2017-04-19 13:27:32 +03:00
|
|
|
return this._afterAuthentication(result);
|
2016-09-30 14:43:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't call the success handler, otherwise we will be redirected to admin
|
|
|
|
this.set('session.skipAuthSuccessHandler', true);
|
2016-09-24 18:48:06 +03:00
|
|
|
|
2019-03-21 13:53:35 +03:00
|
|
|
return this.session.authenticate('authenticator:cookie', data.email, data.password).then(() => {
|
2016-09-30 14:43:40 +03:00
|
|
|
this.set('blogCreated', true);
|
2017-04-19 13:27:32 +03:00
|
|
|
return this._afterAuthentication(result);
|
2016-09-30 14:43:40 +03:00
|
|
|
}).catch((error) => {
|
|
|
|
this._handleAuthenticationError(error);
|
|
|
|
}).finally(() => {
|
|
|
|
this.set('session.skipAuthSuccessHandler', undefined);
|
|
|
|
});
|
|
|
|
}).catch((error) => {
|
|
|
|
this._handleSaveError(error);
|
|
|
|
});
|
|
|
|
}).catch(() => {
|
|
|
|
this.set('flowErrors', 'Please fill out the form to setup your blog.');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-04-19 13:27:32 +03:00
|
|
|
_handleSaveError(resp) {
|
|
|
|
if (isInvalidError(resp)) {
|
2019-03-21 13:53:35 +03:00
|
|
|
let [error] = resp.payload.errors;
|
2019-03-21 14:01:29 +03:00
|
|
|
this.set('flowErrors', [error.message, error.context].join(' '));
|
2017-04-19 13:27:32 +03:00
|
|
|
} else {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAPIError(resp, {key: 'setup.blog-details'});
|
2017-04-19 13:27:32 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleAuthenticationError(error) {
|
2017-11-04 01:59:39 +03:00
|
|
|
if (error && error.payload && error.payload.errors) {
|
2019-03-21 13:53:35 +03:00
|
|
|
let [apiError] = error.payload.errors;
|
2019-03-21 14:01:29 +03:00
|
|
|
this.set('flowErrors', [apiError.message, apiError.context].join(' '));
|
2017-04-19 13:27:32 +03:00
|
|
|
} else {
|
|
|
|
// Connection errors don't return proper status message, only req.body
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAlert('There was a problem on the server.', {type: 'error', key: 'setup.authenticate.failed'});
|
2017-04-19 13:27:32 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_afterAuthentication(result) {
|
2018-01-05 18:38:23 +03:00
|
|
|
// fetch settings and private config for synchronous access before transitioning
|
|
|
|
let fetchSettingsAndConfig = RSVP.all([
|
2019-03-06 16:53:54 +03:00
|
|
|
this.settings.fetch()
|
2018-01-05 18:38:23 +03:00
|
|
|
]);
|
2017-08-02 10:05:59 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.profileImage) {
|
2017-04-19 13:27:32 +03:00
|
|
|
return this._sendImage(result.users[0])
|
2018-01-05 18:38:23 +03:00
|
|
|
.then(() => (fetchSettingsAndConfig))
|
|
|
|
.then(() => (this.transitionToRoute('setup.three')))
|
|
|
|
.catch((resp) => {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAPIError(resp, {key: 'setup.blog-details'});
|
2017-08-18 06:27:42 +03:00
|
|
|
});
|
2017-04-19 13:27:32 +03:00
|
|
|
} else {
|
2018-01-05 18:38:23 +03:00
|
|
|
return fetchSettingsAndConfig.then(() => this.transitionToRoute('setup.three'));
|
2017-04-19 13:27:32 +03:00
|
|
|
}
|
2015-03-29 21:10:53 +03:00
|
|
|
}
|
|
|
|
});
|