2014-07-05 12:06:10 +04:00
|
|
|
var InviteNewUserController = Ember.Controller.extend({
|
|
|
|
|
|
|
|
confirm: {
|
|
|
|
accept: {
|
|
|
|
text: 'send invitation now'
|
|
|
|
},
|
|
|
|
reject: {
|
|
|
|
buttonClass: 'hidden'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-07-23 10:13:20 +04:00
|
|
|
roles: Ember.computed(function () {
|
|
|
|
var roles = {},
|
|
|
|
self = this;
|
|
|
|
|
|
|
|
roles.promise = this.store.find('role', { permissions: 'assign' }).then(function (roles) {
|
2014-07-24 22:28:30 +04:00
|
|
|
return roles.rejectBy('name', 'Owner').sortBy('id');
|
2014-07-23 10:13:20 +04:00
|
|
|
}).then(function (roles) {
|
|
|
|
// After the promise containing the roles has been resolved and the array
|
|
|
|
// has been sorted, explicitly set the selectedRole for the Ember.Select.
|
|
|
|
// The explicit set is needed because the data-select-text attribute is
|
|
|
|
// not being set until a change is made in the dropdown list.
|
|
|
|
// This is only required with Ember.Select when it is bound to async data.
|
|
|
|
self.set('selectedRole', roles.get('firstObject'));
|
|
|
|
|
|
|
|
return roles;
|
|
|
|
});
|
|
|
|
|
|
|
|
return Ember.ArrayProxy.extend(Ember.PromiseProxyMixin).create(roles);
|
|
|
|
}),
|
2014-07-05 12:06:10 +04:00
|
|
|
|
|
|
|
actions: {
|
|
|
|
confirmAccept: function () {
|
|
|
|
var email = this.get('email'),
|
|
|
|
role_id = this.get('role'),
|
|
|
|
self = this,
|
2014-07-23 10:13:20 +04:00
|
|
|
newUser,
|
|
|
|
role;
|
2014-07-05 12:06:10 +04:00
|
|
|
|
2014-07-23 10:13:20 +04:00
|
|
|
newUser = self.store.createRecord('user', {
|
2014-07-11 23:01:42 +04:00
|
|
|
email: email,
|
|
|
|
status: 'invited'
|
2014-07-05 12:06:10 +04:00
|
|
|
});
|
|
|
|
|
2014-07-23 10:13:20 +04:00
|
|
|
// no need to make an API request, the store will already have this role
|
|
|
|
role = self.store.getById('role', role_id);
|
|
|
|
|
|
|
|
newUser.get('roles').pushObject(role);
|
|
|
|
|
2014-07-05 12:06:10 +04:00
|
|
|
newUser.save().then(function () {
|
|
|
|
var notificationText = 'Invitation sent! (' + email + ')';
|
|
|
|
|
2014-07-23 10:13:20 +04:00
|
|
|
self.notifications.closePassive();
|
2014-07-19 04:58:27 +04:00
|
|
|
|
2014-07-23 10:13:20 +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') {
|
|
|
|
self.notifications.showWarn('Invitation email was not sent. Please try resending.');
|
2014-07-29 17:35:48 +04:00
|
|
|
} else {
|
2014-07-23 10:13:20 +04:00
|
|
|
self.notifications.showSuccess(notificationText, false);
|
|
|
|
}
|
|
|
|
}).catch(function (errors) {
|
|
|
|
newUser.deleteRecord();
|
2014-07-05 12:06:10 +04:00
|
|
|
self.notifications.closePassive();
|
2014-07-11 23:01:42 +04:00
|
|
|
self.notifications.showErrors(errors);
|
2014-07-05 12:06:10 +04:00
|
|
|
});
|
|
|
|
|
2014-07-23 10:13:20 +04:00
|
|
|
self.set('email', null);
|
|
|
|
self.set('role', null);
|
2014-07-05 12:06:10 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
confirmReject: function () {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default InviteNewUserController;
|