2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2015-05-26 05:10:50 +03:00
|
|
|
|
|
|
|
export default Ember.Controller.extend({
|
|
|
|
notifications: Ember.inject.service(),
|
|
|
|
|
2015-07-09 20:10:00 +03:00
|
|
|
role: null,
|
|
|
|
authorRole: null,
|
|
|
|
|
|
|
|
roles: Ember.computed(function () {
|
|
|
|
return this.store.find('role', {permissions: 'assign'});
|
|
|
|
}),
|
|
|
|
|
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'),
|
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
|
|
|
|
self.set('email', '');
|
|
|
|
self.set('role', self.get('authorRole'));
|
|
|
|
self.send('closeModal');
|
|
|
|
|
2014-08-09 03:32:03 +04:00
|
|
|
this.store.find('user').then(function (result) {
|
|
|
|
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-06-19 00:56:18 +03:00
|
|
|
self.get('notifications').showAlert('A user with that email address was already invited.', {type: 'warn'});
|
2014-08-09 03:32:03 +04:00
|
|
|
} else {
|
2015-06-19 00:56:18 +03:00
|
|
|
self.get('notifications').showAlert('A user with that email address already exists.', {type: 'warn'});
|
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-06-19 00:56:18 +03:00
|
|
|
self.get('notifications').showAlert('Invitation email was not sent. Please try resending.', {type: 'error'});
|
2014-08-09 03:32:03 +04:00
|
|
|
} else {
|
2015-06-19 00:56:18 +03:00
|
|
|
self.get('notifications').showAlert(notificationText, {type: 'success'});
|
2014-08-09 03:32:03 +04:00
|
|
|
}
|
|
|
|
}).catch(function (errors) {
|
|
|
|
newUser.deleteRecord();
|
2015-05-26 05:10:50 +03:00
|
|
|
self.get('notifications').showErrors(errors);
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|