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-09-03 00:10:51 +03:00
|
|
|
export default Ember.Controller.extend(ValidationEngine, {
|
2015-05-26 05:10:50 +03:00
|
|
|
notifications: Ember.inject.service(),
|
|
|
|
|
2015-09-03 00:10:51 +03:00
|
|
|
validationType: 'signup',
|
|
|
|
|
2015-07-09 20:10:00 +03:00
|
|
|
role: null,
|
|
|
|
authorRole: null,
|
|
|
|
|
|
|
|
roles: Ember.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-07-09 20:10:00 +03:00
|
|
|
authorRoleObserver: Ember.observer('roles.@each.role', function () {
|
2014-07-30 04:11:02 +04:00
|
|
|
var self = this;
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2015-07-09 20:10:00 +03:00
|
|
|
this.get('roles').then(function (roles) {
|
2014-07-30 04:11:02 +04:00
|
|
|
var authorRole = roles.findBy('name', 'Author');
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-08-02 03:12:12 +04:00
|
|
|
self.set('authorRole', authorRole);
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2015-07-09 20:10:00 +03:00
|
|
|
if (!self.get('role')) {
|
|
|
|
self.set('role', authorRole);
|
|
|
|
}
|
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
|
|
|
|
2014-07-05 12:06:10 +04:00
|
|
|
actions: {
|
2014-07-30 04:11:02 +04:00
|
|
|
setRole: function (role) {
|
|
|
|
this.set('role', role);
|
|
|
|
},
|
2014-08-09 00:44:03 +04:00
|
|
|
|
2014-07-05 12:06:10 +04:00
|
|
|
confirmAccept: function () {
|
|
|
|
var email = this.get('email'),
|
2014-07-30 04:11:02 +04:00
|
|
|
role = this.get('role'),
|
2015-09-03 00:10:51 +03:00
|
|
|
validationErrors = this.get('errors.messages'),
|
2014-07-05 12:06:10 +04:00
|
|
|
self = this,
|
2014-07-30 04:11:02 +04:00
|
|
|
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', '');
|
|
|
|
this.set('role', self.get('authorRole'));
|
2014-08-09 00:44:03 +04:00
|
|
|
|
2015-09-03 14:06:50 +03:00
|
|
|
this.store.findAll('user', {reload: true}).then(function (result) {
|
2014-08-09 03:32:03 +04:00
|
|
|
var 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-07 17:44:23 +03:00
|
|
|
self.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-07 17:44:23 +03:00
|
|
|
self.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 {
|
|
|
|
newUser = self.store.createRecord('user', {
|
|
|
|
email: email,
|
|
|
|
status: 'invited',
|
|
|
|
role: role
|
|
|
|
});
|
2014-07-05 12:06:10 +04:00
|
|
|
|
2014-08-09 03:32:03 +04:00
|
|
|
newUser.save().then(function () {
|
|
|
|
var 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-07 17:44:23 +03:00
|
|
|
self.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-07 17:44:23 +03:00
|
|
|
self.get('notifications').closeAlerts('invite.send');
|
2015-08-20 12:25:30 +03:00
|
|
|
self.get('notifications').showNotification(notificationText);
|
2014-08-09 03:32:03 +04:00
|
|
|
}
|
|
|
|
}).catch(function (errors) {
|
|
|
|
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-07 17:44:23 +03:00
|
|
|
self.get('notifications').showErrors(errors, {key: 'invite.send'});
|
2015-09-03 00:10:51 +03:00
|
|
|
} else if (validationErrors) {
|
2015-10-07 17:44:23 +03:00
|
|
|
self.get('notifications').showAlert(validationErrors.toString(), {type: 'error', key: 'invite.send.validation-error'});
|
2015-09-03 00:10:51 +03:00
|
|
|
}
|
|
|
|
}).finally(function () {
|
|
|
|
self.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
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
confirmReject: function () {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|