2016-07-06 22:47:30 +03:00
|
|
|
import Controller from 'ember-controller';
|
|
|
|
import RSVP from 'rsvp';
|
|
|
|
import injectService from 'ember-service/inject';
|
|
|
|
import {isEmberArray} from 'ember-array/utils';
|
|
|
|
|
2016-05-24 15:06:59 +03:00
|
|
|
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
|
2016-06-30 17:45:02 +03:00
|
|
|
import {isVersionMismatchError} from 'ghost-admin/services/ajax';
|
2014-06-24 04:47:51 +04:00
|
|
|
|
2016-07-06 22:47:30 +03:00
|
|
|
const {Promise} = RSVP;
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
export default Controller.extend(ValidationEngine, {
|
2014-06-24 04:47:51 +04:00
|
|
|
// ValidationEngine settings
|
|
|
|
validationType: 'signup',
|
|
|
|
|
2015-05-26 05:10:50 +03:00
|
|
|
submitting: false,
|
2015-08-10 20:45:50 +03:00
|
|
|
flowErrors: '',
|
2015-08-12 03:10:27 +03:00
|
|
|
image: null,
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2016-07-06 22:47:30 +03:00
|
|
|
ghostPaths: injectService(),
|
|
|
|
config: injectService(),
|
|
|
|
notifications: injectService(),
|
|
|
|
session: injectService(),
|
|
|
|
ajax: injectService(),
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
sendImage() {
|
|
|
|
let image = this.get('image');
|
2015-08-12 03:10:27 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
this.get('session.user').then((user) => {
|
2016-01-19 16:03:27 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2015-08-12 03:10:27 +03:00
|
|
|
image.formData = {};
|
|
|
|
image.submit()
|
2015-10-28 14:36:45 +03:00
|
|
|
.success((response) => {
|
2016-01-18 18:37:14 +03:00
|
|
|
let usersUrl = this.get('ghostPaths.url').api('users', user.id.toString());
|
2015-08-12 03:10:27 +03:00
|
|
|
user.image = response;
|
2016-01-18 18:37:14 +03:00
|
|
|
this.get('ajax').put(usersUrl, {
|
2015-08-12 03:10:27 +03:00
|
|
|
data: {
|
|
|
|
users: [user]
|
|
|
|
}
|
|
|
|
}).then(resolve).catch(reject);
|
|
|
|
})
|
|
|
|
.error(reject);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-06-24 04:47:51 +04:00
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
signup() {
|
|
|
|
let model = this.get('model');
|
|
|
|
let setupProperties = ['name', 'email', 'password', 'token'];
|
|
|
|
let data = model.getProperties(setupProperties);
|
|
|
|
let image = this.get('image');
|
|
|
|
let notifications = this.get('notifications');
|
2014-06-24 04:47:51 +04:00
|
|
|
|
2015-08-10 20:45:50 +03:00
|
|
|
this.set('flowErrors', '');
|
2014-06-25 20:56:09 +04:00
|
|
|
|
2015-08-27 23:28:41 +03:00
|
|
|
this.get('hasValidated').addObjects(setupProperties);
|
2016-09-24 18:48:06 +03:00
|
|
|
return this.validate().then(() => {
|
2016-01-18 18:37:14 +03:00
|
|
|
let authUrl = this.get('ghostPaths.url').api('authentication', 'invitation');
|
2015-10-28 14:36:45 +03:00
|
|
|
this.toggleProperty('submitting');
|
2016-09-24 18:48:06 +03:00
|
|
|
return this.get('ajax').post(authUrl, {
|
2014-07-03 19:06:07 +04:00
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
invitation: [{
|
|
|
|
name: data.name,
|
|
|
|
email: data.email,
|
|
|
|
password: data.password,
|
|
|
|
token: data.token
|
|
|
|
}]
|
|
|
|
}
|
2015-10-28 14:36:45 +03:00
|
|
|
}).then(() => {
|
2016-09-24 18:48:06 +03:00
|
|
|
return this.get('session').authenticate('authenticator:oauth2', this.get('model.email'), this.get('model.password')).then(() => {
|
2015-08-12 03:10:27 +03:00
|
|
|
if (image) {
|
2015-10-28 14:36:45 +03:00
|
|
|
this.sendImage();
|
2015-08-12 03:10:27 +03:00
|
|
|
}
|
2015-10-28 14:36:45 +03:00
|
|
|
}).catch((resp) => {
|
2015-10-07 17:44:23 +03:00
|
|
|
notifications.showAPIError(resp, {key: 'signup.complete'});
|
2014-06-30 17:34:36 +04:00
|
|
|
});
|
2016-06-30 17:45:02 +03:00
|
|
|
}).catch((error) => {
|
2015-10-28 14:36:45 +03:00
|
|
|
this.toggleProperty('submitting');
|
2016-01-18 18:37:14 +03:00
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
if (error && error.errors && isEmberArray(error.errors)) {
|
|
|
|
if (isVersionMismatchError(error)) {
|
|
|
|
notifications.showAPIError(error);
|
|
|
|
}
|
|
|
|
this.set('flowErrors', error.errors[0].message);
|
2015-08-10 20:45:50 +03:00
|
|
|
} else {
|
2016-06-30 17:45:02 +03:00
|
|
|
notifications.showAPIError(error, {key: 'signup.complete'});
|
2015-08-10 20:45:50 +03:00
|
|
|
}
|
2014-06-24 04:47:51 +04:00
|
|
|
});
|
2015-10-28 14:36:45 +03:00
|
|
|
}).catch(() => {
|
|
|
|
this.set('flowErrors', 'Please fill out the form to complete your sign-up');
|
2014-06-24 04:47:51 +04:00
|
|
|
});
|
2015-08-12 03:10:27 +03:00
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
setImage(image) {
|
2015-08-12 03:10:27 +03:00
|
|
|
this.set('image', image);
|
2014-06-24 04:47:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|