2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2015-09-03 00:10:51 +03:00
|
|
|
import ValidationEngine from 'ghost/mixins/validation-engine';
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
const {Controller, computed, inject, observer} = Ember;
|
|
|
|
|
|
|
|
export default Controller.extend(ValidationEngine, {
|
|
|
|
notifications: inject.service(),
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-09-03 00:10:51 +03:00
|
|
|
validationType: 'signup',
|
|
|
|
|
2015-07-09 20:10:00 +03:00
|
|
|
role: null,
|
|
|
|
authorRole: null,
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
roles: computed(function () {
|
2015-09-03 14:06:50 +03:00
|
|
|
return this.store.query('role', {permissions: 'assign'});
|
2015-07-09 20:10:00 +03:00
|
|
|
}),
|
|
|
|
|
2014-10-25 01:09:50 +04:00
|
|
|
// Used to set the initial value for the dropdown
|
2015-10-28 14:36:45 +03:00
|
|
|
authorRoleObserver: observer('roles.@each.role', function () {
|
|
|
|
this.get('roles').then((roles) => {
|
|
|
|
let authorRole = roles.findBy('name', 'Author');
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
this.set('authorRole', authorRole);
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
if (!this.get('role')) {
|
|
|
|
this.set('role', authorRole);
|
2015-07-09 20:10:00 +03:00
|
|
|
}
|
2014-07-30 04:11:02 +04:00
|
|
|
});
|
|
|
|
}),
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-07-05 12:06:10 +04:00
|
|
|
confirm: {
|
|
|
|
accept: {
|
|
|
|
text: 'send invitation now'
|
|
|
|
},
|
|
|
|
reject: {
|
|
|
|
buttonClass: 'hidden'
|
|
|
|
}
|
|
|
|
},
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
confirmReject() {
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2014-07-05 12:06:10 +04:00
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
setRole(role) {
|
2014-07-30 04:11:02 +04:00
|
|
|
this.set('role', role);
|
|
|
|
},
|
2014-08-09 00:44:03 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
confirmAccept() {
|
|
|
|
let email = this.get('email');
|
|
|
|
let role = this.get('role');
|
|
|
|
let validationErrors = this.get('errors.messages');
|
|
|
|
let newUser;
|
2014-07-05 12:06:10 +04:00
|
|
|
|
2014-08-09 00:44:03 +04:00
|
|
|
// reset the form and close the modal
|
2015-09-03 00:10:51 +03:00
|
|
|
this.set('email', '');
|
2015-10-28 14:36:45 +03:00
|
|
|
this.set('role', this.get('authorRole'));
|
2014-08-09 00:44:03 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
this.store.findAll('user', {reload: true}).then((result) => {
|
|
|
|
let invitedUser = result.findBy('email', email);
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-08-09 03:32:03 +04:00
|
|
|
if (invitedUser) {
|
|
|
|
if (invitedUser.get('status') === 'invited' || invitedUser.get('status') === 'invited-pending') {
|
2015-10-28 14:36:45 +03:00
|
|
|
this.get('notifications').showAlert('A user with that email address was already invited.', {type: 'warn', key: 'invite.send.already-invited'});
|
2014-08-09 03:32:03 +04:00
|
|
|
} else {
|
2015-10-28 14:36:45 +03:00
|
|
|
this.get('notifications').showAlert('A user with that email address already exists.', {type: 'warn', key: 'invite.send.user-exists'});
|
2014-08-09 03:32:03 +04:00
|
|
|
}
|
|
|
|
} else {
|
2015-10-28 14:36:45 +03:00
|
|
|
newUser = this.store.createRecord('user', {
|
|
|
|
email,
|
|
|
|
role,
|
|
|
|
status: 'invited'
|
2014-08-09 03:32:03 +04:00
|
|
|
});
|
2014-07-05 12:06:10 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
newUser.save().then(() => {
|
|
|
|
let notificationText = `Invitation sent! (${email})`;
|
2014-07-05 12:06:10 +04:00
|
|
|
|
2014-08-09 03:32:03 +04:00
|
|
|
// 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 (newUser.get('status') === 'invited-pending') {
|
2015-10-28 14:36:45 +03:00
|
|
|
this.get('notifications').showAlert('Invitation email was not sent. Please try resending.', {type: 'error', key: 'invite.send.failed'});
|
2014-08-09 03:32:03 +04:00
|
|
|
} else {
|
2015-10-28 14:36:45 +03:00
|
|
|
this.get('notifications').closeAlerts('invite.send');
|
|
|
|
this.get('notifications').showNotification(notificationText);
|
2014-08-09 03:32:03 +04:00
|
|
|
}
|
2015-10-28 14:36:45 +03:00
|
|
|
}).catch((errors) => {
|
2014-08-09 03:32:03 +04:00
|
|
|
newUser.deleteRecord();
|
2015-07-21 20:56:17 +03:00
|
|
|
// TODO: user model includes ValidationEngine mixin so
|
|
|
|
// save is overridden in order to validate, we probably
|
|
|
|
// want to use inline-validations here and only show an
|
|
|
|
// alert if we have an actual error
|
2015-09-03 00:10:51 +03:00
|
|
|
if (errors) {
|
2015-10-28 14:36:45 +03:00
|
|
|
this.get('notifications').showErrors(errors, {key: 'invite.send'});
|
2015-09-03 00:10:51 +03:00
|
|
|
} else if (validationErrors) {
|
2015-10-28 14:36:45 +03:00
|
|
|
this.get('notifications').showAlert(validationErrors.toString(), {type: 'error', key: 'invite.send.validation-error'});
|
2015-09-03 00:10:51 +03:00
|
|
|
}
|
2015-10-28 14:36:45 +03:00
|
|
|
}).finally(() => {
|
|
|
|
this.get('errors').clear();
|
2014-08-09 03:32:03 +04:00
|
|
|
});
|
2014-07-23 10:13:20 +04:00
|
|
|
}
|
2014-07-05 12:06:10 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|