2015-02-12 21:22:32 -07:00
|
|
|
import Ember from 'ember';
|
2014-07-31 16:29:35 -04:00
|
|
|
import isNumber from 'ghost/utils/isNumber';
|
2014-11-10 13:12:27 -08:00
|
|
|
import boundOneWay from 'ghost/utils/bound-one-way';
|
2015-07-07 18:14:23 +01:00
|
|
|
import ValidationEngine from 'ghost/mixins/validation-engine';
|
|
|
|
|
2016-01-19 07:03:27 -06:00
|
|
|
const {
|
|
|
|
Controller,
|
|
|
|
RSVP,
|
|
|
|
computed,
|
|
|
|
inject: {service},
|
|
|
|
isArray
|
|
|
|
} = Ember;
|
2015-10-28 11:36:45 +00:00
|
|
|
const {alias, and, not, or, readOnly} = computed;
|
|
|
|
|
|
|
|
export default Controller.extend(ValidationEngine, {
|
2015-07-07 18:14:23 +01:00
|
|
|
// ValidationEngine settings
|
|
|
|
validationType: 'user',
|
2015-08-10 09:43:49 -06:00
|
|
|
submitting: false,
|
2015-11-18 10:50:48 +00:00
|
|
|
lastPromise: null,
|
|
|
|
showDeleteUserModal: false,
|
|
|
|
showTransferOwnerModal: false,
|
|
|
|
showUploadCoverModal: false,
|
|
|
|
showUplaodImageModal: false,
|
2014-07-31 00:25:42 -04:00
|
|
|
|
2016-01-19 07:03:27 -06:00
|
|
|
ajax: service(),
|
|
|
|
dropdown: service(),
|
|
|
|
ghostPaths: service(),
|
|
|
|
notifications: service(),
|
|
|
|
session: service(),
|
|
|
|
slugGenerator: service(),
|
2015-10-28 11:36:45 +00:00
|
|
|
|
|
|
|
user: alias('model'),
|
2015-11-18 10:50:48 +00:00
|
|
|
currentUser: alias('session.user'),
|
2014-07-01 17:58:26 +02:00
|
|
|
|
2015-11-18 10:50:48 +00:00
|
|
|
email: readOnly('model.email'),
|
|
|
|
slugValue: boundOneWay('model.slug'),
|
2014-07-01 23:44:39 -04:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
isNotOwnersProfile: not('user.isOwner'),
|
|
|
|
isAdminUserOnOwnerProfile: and('currentUser.isAdmin', 'user.isOwner'),
|
|
|
|
canAssignRoles: or('currentUser.isAdmin', 'currentUser.isOwner'),
|
|
|
|
canMakeOwner: and('currentUser.isOwner', 'isNotOwnProfile', 'user.isAdmin'),
|
|
|
|
rolesDropdownIsVisible: and('isNotOwnProfile', 'canAssignRoles', 'isNotOwnersProfile'),
|
2015-11-18 10:50:48 +00:00
|
|
|
userActionsAreVisible: or('deleteUserActionIsVisible', 'canMakeOwner'),
|
|
|
|
|
|
|
|
isNotOwnProfile: computed('user.id', 'currentUser.id', function () {
|
|
|
|
return this.get('user.id') !== this.get('currentUser.id');
|
|
|
|
}),
|
2015-05-25 21:10:50 -05:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
deleteUserActionIsVisible: computed('currentUser', 'canAssignRoles', 'user', function () {
|
2015-06-13 09:34:09 -05:00
|
|
|
if ((this.get('canAssignRoles') && this.get('isNotOwnProfile') && !this.get('user.isOwner')) ||
|
|
|
|
(this.get('currentUser.isEditor') && (this.get('isNotOwnProfile') ||
|
|
|
|
this.get('user.isAuthor')))) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-07-29 19:57:19 -06:00
|
|
|
}),
|
2014-03-22 22:31:45 -04:00
|
|
|
|
2015-06-13 09:34:09 -05:00
|
|
|
// duplicated in gh-user-active -- find a better home and consolidate?
|
2015-10-28 11:36:45 +00:00
|
|
|
userDefault: computed('ghostPaths', function () {
|
2016-01-14 16:25:29 +00:00
|
|
|
return `${this.get('ghostPaths.subdir')}/ghost/img/user-image.png`;
|
2014-07-29 19:57:19 -06:00
|
|
|
}),
|
2015-05-25 21:10:50 -05:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
userImageBackground: computed('user.image', 'userDefault', function () {
|
|
|
|
let url = this.get('user.image') || this.get('userDefault');
|
2015-05-25 21:10:50 -05:00
|
|
|
|
2015-11-20 17:45:43 -05:00
|
|
|
return Ember.String.htmlSafe(`background-image: url(${url})`);
|
2014-07-29 19:57:19 -06:00
|
|
|
}),
|
2015-06-13 09:34:09 -05:00
|
|
|
// end duplicated
|
2014-07-16 17:12:45 +00:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
coverDefault: computed('ghostPaths', function () {
|
2016-01-14 16:25:29 +00:00
|
|
|
return `${this.get('ghostPaths.subdir')}/ghost/img/user-cover.png`;
|
2014-07-29 19:57:19 -06:00
|
|
|
}),
|
2014-07-01 23:44:39 -04:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
coverImageBackground: computed('user.cover', 'coverDefault', function () {
|
|
|
|
let url = this.get('user.cover') || this.get('coverDefault');
|
2015-06-13 09:34:09 -05:00
|
|
|
|
2015-11-20 17:45:43 -05:00
|
|
|
return Ember.String.htmlSafe(`background-image: url(${url})`);
|
2015-06-13 09:34:09 -05:00
|
|
|
}),
|
2014-07-16 17:12:45 +00:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
coverTitle: computed('user.name', function () {
|
|
|
|
return `${this.get('user.name')}'s Cover Image`;
|
2014-07-29 19:57:19 -06:00
|
|
|
}),
|
2014-07-31 00:25:42 -04:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
roles: computed(function () {
|
2015-09-03 12:06:50 +01:00
|
|
|
return this.store.query('role', {permissions: 'assign'});
|
2015-07-09 12:10:00 -05:00
|
|
|
}),
|
|
|
|
|
2015-11-18 10:50:48 +00:00
|
|
|
_deleteUser() {
|
|
|
|
if (this.get('deleteUserActionIsVisible')) {
|
|
|
|
let user = this.get('user');
|
|
|
|
return user.destroyRecord();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_deleteUserSuccess() {
|
|
|
|
this.get('notifications').closeAlerts('user.delete');
|
|
|
|
this.store.unloadAll('post');
|
|
|
|
this.transitionToRoute('team');
|
|
|
|
},
|
|
|
|
|
|
|
|
_deleteUserFailure() {
|
|
|
|
this.get('notifications').showAlert('The user could not be deleted. Please try again.', {type: 'error', key: 'user.delete.failed'});
|
|
|
|
},
|
|
|
|
|
2014-03-22 22:31:45 -04:00
|
|
|
actions: {
|
2015-10-28 11:36:45 +00:00
|
|
|
changeRole(newRole) {
|
2014-07-29 18:11:02 -06:00
|
|
|
this.set('model.role', newRole);
|
|
|
|
},
|
2014-10-24 21:09:50 +00:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
save() {
|
|
|
|
let user = this.get('user');
|
|
|
|
let slugValue = this.get('slugValue');
|
|
|
|
let afterUpdateSlug = this.get('lastPromise');
|
|
|
|
let promise,
|
|
|
|
slugChanged;
|
2014-06-29 23:45:03 +02:00
|
|
|
|
2014-08-12 18:56:31 +00:00
|
|
|
if (user.get('slug') !== slugValue) {
|
|
|
|
slugChanged = true;
|
|
|
|
user.set('slug', slugValue);
|
|
|
|
}
|
|
|
|
|
2015-08-10 09:43:49 -06:00
|
|
|
this.toggleProperty('submitting');
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
promise = RSVP.resolve(afterUpdateSlug).then(() => {
|
2014-10-24 21:09:50 +00:00
|
|
|
return user.save({format: false});
|
2015-10-28 11:36:45 +00:00
|
|
|
}).then((model) => {
|
|
|
|
let currentPath,
|
2014-08-12 18:56:31 +00:00
|
|
|
newPath;
|
|
|
|
|
|
|
|
// If the user's slug has changed, change the URL and replace
|
|
|
|
// the history so refresh and back button still work
|
|
|
|
if (slugChanged) {
|
|
|
|
currentPath = window.history.state.path;
|
|
|
|
|
|
|
|
newPath = currentPath.split('/');
|
|
|
|
newPath[newPath.length - 2] = model.get('slug');
|
|
|
|
newPath = newPath.join('/');
|
|
|
|
|
2014-10-24 21:09:50 +00:00
|
|
|
window.history.replaceState({path: newPath}, '', newPath);
|
2014-08-12 18:56:31 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
this.toggleProperty('submitting');
|
|
|
|
this.get('notifications').closeAlerts('user.update');
|
2015-08-10 09:43:49 -06:00
|
|
|
|
2014-07-14 16:32:55 +00:00
|
|
|
return model;
|
2015-10-28 11:36:45 +00:00
|
|
|
}).catch((errors) => {
|
2015-06-06 22:19:19 -05:00
|
|
|
if (errors) {
|
2015-10-28 11:36:45 +00:00
|
|
|
this.get('notifications').showErrors(errors, {key: 'user.update'});
|
2015-06-06 22:19:19 -05:00
|
|
|
}
|
2015-08-10 09:43:49 -06:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
this.toggleProperty('submitting');
|
2014-07-13 00:01:26 -04:00
|
|
|
});
|
2014-08-12 18:56:31 +00:00
|
|
|
|
|
|
|
this.set('lastPromise', promise);
|
2014-03-22 22:31:45 -04:00
|
|
|
},
|
|
|
|
|
2015-11-18 10:50:48 +00:00
|
|
|
deleteUser() {
|
|
|
|
return this._deleteUser().then(() => {
|
|
|
|
this._deleteUserSuccess();
|
|
|
|
}, () => {
|
|
|
|
this._deleteUserFailure();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleDeleteUserModal() {
|
|
|
|
if (this.get('deleteUserActionIsVisible')) {
|
|
|
|
this.toggleProperty('showDeleteUserModal');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
password() {
|
|
|
|
let user = this.get('user');
|
2014-07-13 00:01:26 -04:00
|
|
|
|
|
|
|
if (user.get('isPasswordValid')) {
|
2015-10-28 11:36:45 +00:00
|
|
|
user.saveNewPassword().then((model) => {
|
2014-03-22 22:31:45 -04:00
|
|
|
// Clear properties from view
|
2014-07-13 00:01:26 -04:00
|
|
|
user.setProperties({
|
2014-10-24 21:09:50 +00:00
|
|
|
password: '',
|
|
|
|
newPassword: '',
|
|
|
|
ne2Password: ''
|
2014-03-22 22:31:45 -04:00
|
|
|
});
|
2014-07-13 00:01:26 -04:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
this.get('notifications').showAlert('Password updated.', {type: 'success', key: 'user.change-password.success'});
|
2014-07-13 00:01:26 -04:00
|
|
|
|
|
|
|
return model;
|
2015-10-28 11:36:45 +00:00
|
|
|
}).catch((errors) => {
|
|
|
|
this.get('notifications').showAPIError(errors, {key: 'user.change-password'});
|
2014-03-22 22:31:45 -04:00
|
|
|
});
|
|
|
|
} else {
|
2015-06-18 22:56:18 +01:00
|
|
|
// TODO: switch to in-line validation
|
2015-10-28 11:36:45 +00:00
|
|
|
this.get('notifications').showErrors(user.get('passwordValidationErrors'), {key: 'user.change-password'});
|
2014-03-22 22:31:45 -04:00
|
|
|
}
|
2014-07-31 00:25:42 -04:00
|
|
|
},
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
updateSlug(newSlug) {
|
|
|
|
let afterSave = this.get('lastPromise');
|
|
|
|
let promise;
|
2014-07-31 00:25:42 -04:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
promise = RSVP.resolve(afterSave).then(() => {
|
|
|
|
let slug = this.get('model.slug');
|
2014-07-31 00:25:42 -04:00
|
|
|
|
2014-08-12 18:56:31 +00:00
|
|
|
newSlug = newSlug || slug;
|
|
|
|
newSlug = newSlug.trim();
|
2014-07-31 00:25:42 -04:00
|
|
|
|
2014-08-12 18:56:31 +00:00
|
|
|
// Ignore unchanged slugs or candidate slugs that are empty
|
|
|
|
if (!newSlug || slug === newSlug) {
|
2015-10-28 11:36:45 +00:00
|
|
|
this.set('slugValue', slug);
|
2014-07-31 00:25:42 -04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-15 09:43:16 -06:00
|
|
|
return this.get('slugGenerator').generateSlug('user', newSlug).then((serverSlug) => {
|
2014-08-12 18:56:31 +00:00
|
|
|
// 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) {
|
2014-07-31 00:25:42 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-12 18:56:31 +00:00
|
|
|
// 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
|
2015-10-28 11:36:45 +00:00
|
|
|
let slugTokens = serverSlug.split('-');
|
|
|
|
let check = Number(slugTokens.pop());
|
2014-08-12 18:56:31 +00:00
|
|
|
|
|
|
|
// if the candidate slug is the same as the existing slug except
|
|
|
|
// for the incrementor then the existing slug should be used
|
2014-07-31 16:29:35 -04:00
|
|
|
if (isNumber(check) && check > 0) {
|
2014-08-12 18:56:31 +00:00
|
|
|
if (slug === slugTokens.join('-') && serverSlug !== newSlug) {
|
2015-10-28 11:36:45 +00:00
|
|
|
this.set('slugValue', slug);
|
2014-08-12 18:56:31 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
this.set('slugValue', serverSlug);
|
2014-08-12 18:56:31 +00:00
|
|
|
});
|
2014-07-31 00:25:42 -04:00
|
|
|
});
|
2014-08-12 18:56:31 +00:00
|
|
|
|
|
|
|
this.set('lastPromise', promise);
|
2015-11-18 10:50:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
transferOwnership() {
|
|
|
|
let user = this.get('user');
|
|
|
|
let url = this.get('ghostPaths.url').api('users', 'owner');
|
|
|
|
|
|
|
|
this.get('dropdown').closeDropdowns();
|
|
|
|
|
2016-01-18 09:37:14 -06:00
|
|
|
return this.get('ajax').put(url, {
|
2015-11-18 10:50:48 +00:00
|
|
|
data: {
|
|
|
|
owner: [{
|
|
|
|
id: user.get('id')
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}).then((response) => {
|
|
|
|
// manually update the roles for the users that just changed roles
|
|
|
|
// because store.pushPayload is not working with embedded relations
|
|
|
|
if (response && isArray(response.users)) {
|
|
|
|
response.users.forEach((userJSON) => {
|
|
|
|
let user = this.store.peekRecord('user', userJSON.id);
|
|
|
|
let role = this.store.peekRecord('role', userJSON.roles[0].id);
|
|
|
|
|
|
|
|
user.set('role', role);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.get('notifications').showAlert(`Ownership successfully transferred to ${user.get('name')}`, {type: 'success', key: 'owner.transfer.success'});
|
|
|
|
}).catch((error) => {
|
|
|
|
this.get('notifications').showAPIError(error, {key: 'owner.transfer'});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleTransferOwnerModal() {
|
|
|
|
if (this.get('canMakeOwner')) {
|
|
|
|
this.toggleProperty('showTransferOwnerModal');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleUploadCoverModal() {
|
|
|
|
this.toggleProperty('showUploadCoverModal');
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleUploadImageModal() {
|
|
|
|
this.toggleProperty('showUploadImageModal');
|
2014-03-22 22:31:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|