2014-07-31 08:25:42 +04:00
|
|
|
import SlugGenerator from 'ghost/models/slug-generator';
|
|
|
|
import boundOneWay from 'ghost/utils/bound-one-way';
|
|
|
|
|
2014-07-01 19:58:26 +04:00
|
|
|
var SettingsUserController = Ember.ObjectController.extend({
|
|
|
|
|
|
|
|
user: Ember.computed.alias('model'),
|
|
|
|
|
2014-07-02 07:44:39 +04:00
|
|
|
email: Ember.computed.readOnly('user.email'),
|
|
|
|
|
2014-07-13 08:01:26 +04:00
|
|
|
coverDefault: function () {
|
|
|
|
return this.get('ghostPaths.url').asset('/shared/img/user-cover.png');
|
|
|
|
}.property('ghostPaths'),
|
|
|
|
|
|
|
|
userDefault: function () {
|
|
|
|
return this.get('ghostPaths.url').asset('/shared/img/user-image.png');
|
|
|
|
}.property('ghostPaths'),
|
2014-07-02 07:44:39 +04:00
|
|
|
|
2014-03-23 06:31:45 +04:00
|
|
|
cover: function () {
|
2014-07-01 19:58:26 +04:00
|
|
|
var cover = this.get('user.cover');
|
2014-07-21 22:03:11 +04:00
|
|
|
if (Ember.isBlank(cover)) {
|
2014-05-24 18:43:53 +04:00
|
|
|
cover = this.get('coverDefault');
|
|
|
|
}
|
|
|
|
return cover;
|
|
|
|
}.property('user.cover', 'coverDefault'),
|
2014-03-23 06:31:45 +04:00
|
|
|
|
|
|
|
coverTitle: function () {
|
|
|
|
return this.get('user.name') + '\'s Cover Image';
|
|
|
|
}.property('user.name'),
|
|
|
|
|
|
|
|
image: function () {
|
2014-07-13 08:01:26 +04:00
|
|
|
return 'background-image: url(' + this.get('imageUrl') + ')';
|
|
|
|
}.property('imageUrl'),
|
2014-07-02 07:44:39 +04:00
|
|
|
|
|
|
|
imageUrl: function () {
|
2014-07-13 08:01:26 +04:00
|
|
|
return this.get('user.image') || this.get('userDefault');
|
2014-03-23 06:31:45 +04:00
|
|
|
}.property('user.image'),
|
|
|
|
|
2014-07-02 07:44:39 +04:00
|
|
|
last_login: function () {
|
2014-07-16 21:12:45 +04:00
|
|
|
var lastLogin = this.get('user.last_login');
|
|
|
|
|
|
|
|
return lastLogin ? lastLogin.fromNow() : '';
|
2014-07-02 07:44:39 +04:00
|
|
|
}.property('user.last_login'),
|
|
|
|
|
|
|
|
created_at: function () {
|
2014-07-16 21:12:45 +04:00
|
|
|
var createdAt = this.get('user.created_at');
|
|
|
|
|
|
|
|
return createdAt ? createdAt.fromNow() : '';
|
2014-07-02 07:44:39 +04:00
|
|
|
}.property('user.created_at'),
|
2014-07-31 08:25:42 +04:00
|
|
|
|
|
|
|
//Lazy load the slug generator for slugPlaceholder
|
|
|
|
slugGenerator: Ember.computed(function () {
|
|
|
|
return SlugGenerator.create({
|
|
|
|
ghostPaths: this.get('ghostPaths'),
|
|
|
|
slugType: 'user'
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
|
|
|
|
slugValue: boundOneWay('user.slug'),
|
|
|
|
|
2014-03-23 06:31:45 +04:00
|
|
|
actions: {
|
2014-07-30 04:11:02 +04:00
|
|
|
changeRole: function (newRole) {
|
|
|
|
this.set('model.role', newRole);
|
|
|
|
},
|
2014-07-02 07:44:39 +04:00
|
|
|
revoke: function () {
|
2014-07-08 09:24:07 +04:00
|
|
|
var self = this,
|
|
|
|
email = this.get('email');
|
|
|
|
|
|
|
|
this.get('model').destroyRecord().then(function () {
|
|
|
|
var notificationText = 'Invitation revoked. (' + email + ')';
|
|
|
|
self.notifications.showSuccess(notificationText, false);
|
|
|
|
}).catch(function (error) {
|
|
|
|
self.notifications.showAPIError(error);
|
|
|
|
});
|
2014-07-02 07:44:39 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
resend: function () {
|
2014-07-08 09:24:07 +04:00
|
|
|
var self = this;
|
|
|
|
|
2014-07-29 17:35:48 +04:00
|
|
|
this.get('model').resendInvite().then(function (result) {
|
2014-07-08 09:24:07 +04:00
|
|
|
var notificationText = 'Invitation resent! (' + self.get('email') + ')';
|
2014-07-29 17:35:48 +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 (result.users[0].status === 'invited-pending') {
|
|
|
|
self.notifications.showWarn('Invitation email was not sent. Please try resending.');
|
|
|
|
} else {
|
|
|
|
self.get('model').set('status', result.users[0].status);
|
|
|
|
self.notifications.showSuccess(notificationText, false);
|
|
|
|
}
|
2014-07-08 09:24:07 +04:00
|
|
|
}).catch(function (error) {
|
|
|
|
self.notifications.showAPIError(error);
|
|
|
|
});
|
2014-07-02 07:44:39 +04:00
|
|
|
},
|
|
|
|
|
2014-03-23 06:31:45 +04:00
|
|
|
save: function () {
|
2014-07-01 19:58:26 +04:00
|
|
|
var user = this.get('user'),
|
|
|
|
self = this;
|
2014-06-30 01:45:03 +04:00
|
|
|
|
2014-07-14 20:32:55 +04:00
|
|
|
user.save({ format: false }).then(function (model) {
|
|
|
|
self.notifications.showSuccess('Settings successfully saved.');
|
2014-03-23 06:31:45 +04:00
|
|
|
|
2014-07-14 20:32:55 +04:00
|
|
|
return model;
|
|
|
|
}).catch(function (errors) {
|
2014-07-13 08:01:26 +04:00
|
|
|
self.notifications.showErrors(errors);
|
|
|
|
});
|
2014-03-23 06:31:45 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
password: function () {
|
2014-07-01 19:58:26 +04:00
|
|
|
var user = this.get('user'),
|
2014-07-13 08:01:26 +04:00
|
|
|
self = this;
|
|
|
|
|
|
|
|
if (user.get('isPasswordValid')) {
|
|
|
|
user.saveNewPassword().then(function (model) {
|
2014-03-23 06:31:45 +04:00
|
|
|
|
|
|
|
// Clear properties from view
|
2014-07-13 08:01:26 +04:00
|
|
|
user.setProperties({
|
2014-03-23 06:31:45 +04:00
|
|
|
'password': '',
|
2014-07-13 08:01:26 +04:00
|
|
|
'newPassword': '',
|
|
|
|
'ne2Password': ''
|
2014-03-23 06:31:45 +04:00
|
|
|
});
|
2014-07-13 08:01:26 +04:00
|
|
|
|
|
|
|
self.notifications.showSuccess('Password updated.');
|
|
|
|
|
|
|
|
return model;
|
|
|
|
}).catch(function (errors) {
|
|
|
|
self.notifications.showAPIError(errors);
|
2014-03-23 06:31:45 +04:00
|
|
|
});
|
|
|
|
} else {
|
2014-07-13 08:01:26 +04:00
|
|
|
self.notifications.showErrors(user.get('passwordValidationErrors'));
|
2014-03-23 06:31:45 +04:00
|
|
|
}
|
2014-07-31 08:25:42 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
updateSlug: function (newSlug) {
|
|
|
|
var slug = this.get('user.slug'),
|
|
|
|
self = this;
|
|
|
|
|
|
|
|
newSlug = newSlug || slug;
|
|
|
|
|
|
|
|
newSlug = newSlug.trim();
|
|
|
|
|
|
|
|
// Ignore unchanged slugs or candidate slugs that are empty
|
|
|
|
if (!newSlug || slug === newSlug) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.get('slugGenerator').generateSlug(newSlug).then(function (serverSlug) {
|
|
|
|
|
|
|
|
// If after getting the sanitized and unique slug back from the API
|
|
|
|
// we end up with a slug that matches the existing slug, abort the change
|
|
|
|
if (serverSlug === slug) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Because the server transforms the candidate slug by stripping
|
|
|
|
// certain characters and appending a number onto the end of slugs
|
|
|
|
// to enforce uniqueness, there are cases where we can get back a
|
|
|
|
// candidate slug that is a duplicate of the original except for
|
|
|
|
// the trailing incrementor (e.g., this-is-a-slug and this-is-a-slug-2)
|
|
|
|
|
|
|
|
// get the last token out of the slug candidate and see if it's a number
|
|
|
|
var slugTokens = serverSlug.split('-'),
|
|
|
|
check = Number(slugTokens.pop());
|
|
|
|
|
|
|
|
// if the candidate slug is the same as the existing slug except
|
|
|
|
// for the incrementor then the existing slug should be used
|
|
|
|
if (_.isNumber(check) && check > 0) {
|
|
|
|
if (slug === slugTokens.join('-') && serverSlug !== newSlug) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.set('user.slug', serverSlug);
|
|
|
|
});
|
2014-03-23 06:31:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-06-30 01:45:03 +04:00
|
|
|
export default SettingsUserController;
|