2017-04-24 20:22:39 +03:00
|
|
|
/* eslint-disable camelcase */
|
2017-08-22 10:53:26 +03:00
|
|
|
import 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';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {inject as injectController} from '@ember/controller';
|
|
|
|
import {inject as injectService} from '@ember/service';
|
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';
|
|
|
|
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-04-19 13:27:32 +03:00
|
|
|
ajax: injectService(),
|
2016-07-06 22:47:30 +03:00
|
|
|
application: injectController(),
|
|
|
|
config: injectService(),
|
2017-04-19 13:27:32 +03:00
|
|
|
ghostPaths: injectService(),
|
|
|
|
notifications: injectService(),
|
2016-07-06 22:47:30 +03:00
|
|
|
session: injectService(),
|
2017-03-17 20:16:21 +03:00
|
|
|
settings: injectService(),
|
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,
|
|
|
|
|
|
|
|
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 {
|
|
|
|
let authResult = yield this.get('session')
|
|
|
|
.authenticate(authStrategy, ...authentication);
|
|
|
|
|
|
|
|
this.get('errors').remove('session');
|
|
|
|
|
|
|
|
return authResult;
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
if (error && error.errors) {
|
|
|
|
if (isVersionMismatchError(error)) {
|
|
|
|
return this.get('notifications').showAPIError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
error.errors.forEach((err) => {
|
|
|
|
err.message = err.message.htmlSafe();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.set('flowErrors', error.errors[0].message.string);
|
|
|
|
} else {
|
|
|
|
// Connection errors don't return proper status message, only req.body
|
|
|
|
this.get('notifications').showAlert('There was a problem on the server.', {type: 'error', key: 'session.authenticate.failed'});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
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
|
|
|
|
* @return {Ember.RSVP.Promise} A promise that takes care of both calls
|
|
|
|
*/
|
2017-04-19 13:27:32 +03:00
|
|
|
_sendImage(user) {
|
2017-08-18 06:27:42 +03:00
|
|
|
let formData = new FormData();
|
|
|
|
let imageFile = this.get('profileImage');
|
|
|
|
let uploadUrl = this.get('ghostPaths.url').api('uploads');
|
|
|
|
|
|
|
|
formData.append('uploadimage', imageFile, imageFile.name);
|
|
|
|
|
|
|
|
return this.get('ajax').post(uploadUrl, {
|
|
|
|
data: formData,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
dataType: 'text'
|
|
|
|
}).then((response) => {
|
|
|
|
let imageUrl = JSON.parse(response);
|
|
|
|
let usersUrl = this.get('ghostPaths.url').api('users', user.id.toString());
|
|
|
|
user.profile_image = imageUrl;
|
|
|
|
|
|
|
|
return this.get('ajax').put(usersUrl, {
|
|
|
|
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);
|
|
|
|
let config = this.get('config');
|
|
|
|
let method = this.get('blogCreated') ? 'put' : 'post';
|
|
|
|
|
|
|
|
this.set('flowErrors', '');
|
|
|
|
|
|
|
|
this.get('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
|
|
|
|
|
|
|
return this.get('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
|
|
|
|
|
|
|
return this.get('session').authenticate('authenticator:oauth2', this.get('email'), this.get('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)) {
|
|
|
|
this.set('flowErrors', resp.errors[0].message);
|
|
|
|
} else {
|
|
|
|
this.get('notifications').showAPIError(resp, {key: 'setup.blog-details'});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleAuthenticationError(error) {
|
|
|
|
if (error && error.errors) {
|
|
|
|
this.set('flowErrors', error.errors[0].message);
|
|
|
|
} else {
|
|
|
|
// Connection errors don't return proper status message, only req.body
|
|
|
|
this.get('notifications').showAlert('There was a problem on the server.', {type: 'error', key: 'setup.authenticate.failed'});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_afterAuthentication(result) {
|
2017-08-02 10:05:59 +03:00
|
|
|
let promises = [];
|
|
|
|
|
|
|
|
promises.pushObject(this.get('settings').fetch());
|
|
|
|
promises.pushObject(this.get('config').fetchPrivate());
|
|
|
|
|
2017-04-24 20:22:39 +03:00
|
|
|
if (this.get('profileImage')) {
|
2017-04-19 13:27:32 +03:00
|
|
|
return this._sendImage(result.users[0])
|
2017-09-11 10:56:11 +03:00
|
|
|
.then(() => {
|
|
|
|
// fetch settings and private config for synchronous access before transitioning
|
|
|
|
return RSVP.all(promises).then(() => {
|
|
|
|
return this.transitionToRoute('setup.three');
|
|
|
|
});
|
|
|
|
}).catch((resp) => {
|
|
|
|
this.get('notifications').showAPIError(resp, {key: 'setup.blog-details'});
|
2017-08-18 06:27:42 +03:00
|
|
|
});
|
2017-04-19 13:27:32 +03:00
|
|
|
} else {
|
2017-08-02 10:05:59 +03:00
|
|
|
// fetch settings and private config for synchronous access before transitioning
|
2017-08-18 06:27:42 +03:00
|
|
|
return RSVP.all(promises).then(() => {
|
|
|
|
return this.transitionToRoute('setup.three');
|
|
|
|
});
|
2017-04-19 13:27:32 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-29 21:10:53 +03:00
|
|
|
actions: {
|
2017-04-19 13:27:32 +03:00
|
|
|
setup() {
|
|
|
|
this.get('setup').perform();
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
preValidate(model) {
|
2015-08-27 23:28:41 +03:00
|
|
|
// Only triggers validation if a value has been entered, preventing empty errors on focusOut
|
2015-08-27 22:49:36 +03:00
|
|
|
if (this.get(model)) {
|
2016-09-24 18:48:06 +03:00
|
|
|
return this.validate({property: model});
|
2015-08-27 22:49:36 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
setImage(image) {
|
2017-04-24 20:22:39 +03:00
|
|
|
this.set('profileImage', image);
|
2015-03-29 21:10:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|