2016-11-14 16:16:51 +03:00
|
|
|
/* eslint-disable camelcase */
|
2019-12-17 23:42:42 +03:00
|
|
|
import BaseModel from './base';
|
2017-05-29 21:50:03 +03:00
|
|
|
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
|
2019-12-17 23:42:42 +03:00
|
|
|
import {attr, hasMany} from '@ember-data/model';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {computed} from '@ember/object';
|
2018-02-07 12:42:46 +03:00
|
|
|
import {equal, or} from '@ember/object/computed';
|
2022-11-03 14:14:36 +03:00
|
|
|
import {inject} from 'ghost-admin/decorators/inject';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2016-08-11 09:58:38 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2019-12-17 23:42:42 +03:00
|
|
|
export default BaseModel.extend(ValidationEngine, {
|
2014-07-11 23:01:42 +04:00
|
|
|
validationType: 'user',
|
2014-07-14 20:32:55 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
name: attr('string'),
|
|
|
|
slug: attr('string'),
|
|
|
|
email: attr('string'),
|
2017-04-24 20:22:39 +03:00
|
|
|
profileImage: attr('string'),
|
|
|
|
coverImage: attr('string'),
|
2015-10-28 14:36:45 +03:00
|
|
|
bio: attr('string'),
|
|
|
|
website: attr('string'),
|
|
|
|
location: attr('string'),
|
|
|
|
accessibility: attr('string'),
|
|
|
|
status: attr('string'),
|
2016-01-23 21:12:22 +03:00
|
|
|
metaTitle: attr('string'),
|
|
|
|
metaDescription: attr('string'),
|
2016-06-14 11:11:59 +03:00
|
|
|
lastLoginUTC: attr('moment-utc'),
|
|
|
|
createdAtUTC: attr('moment-utc'),
|
2016-01-23 21:12:22 +03:00
|
|
|
createdBy: attr('number'),
|
2016-06-14 11:11:59 +03:00
|
|
|
updatedAtUTC: attr('moment-utc'),
|
2016-01-23 21:12:22 +03:00
|
|
|
updatedBy: attr('number'),
|
2015-10-28 14:36:45 +03:00
|
|
|
roles: hasMany('role', {
|
2015-09-03 14:06:50 +03:00
|
|
|
embedded: 'always',
|
|
|
|
async: false
|
|
|
|
}),
|
2016-01-12 22:23:01 +03:00
|
|
|
count: attr('raw'),
|
2016-05-16 21:16:40 +03:00
|
|
|
facebook: attr('facebook-url-user'),
|
|
|
|
twitter: attr('twitter-url-user'),
|
2017-06-08 18:00:10 +03:00
|
|
|
tour: attr('json-string'),
|
2022-07-06 19:32:13 +03:00
|
|
|
commentNotifications: attr(),
|
2022-08-25 13:17:14 +03:00
|
|
|
freeMemberSignupNotification: attr(),
|
|
|
|
paidSubscriptionStartedNotification: attr(),
|
|
|
|
paidSubscriptionCanceledNotification: attr(),
|
2014-05-15 03:36:13 +04:00
|
|
|
|
2017-10-30 12:38:01 +03:00
|
|
|
ghostPaths: service(),
|
|
|
|
ajax: service(),
|
|
|
|
session: service(),
|
|
|
|
notifications: service(),
|
2022-11-03 14:14:36 +03:00
|
|
|
|
|
|
|
config: inject(),
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
// TODO: Once client-side permissions are in place,
|
|
|
|
// remove the hard role check.
|
2018-02-07 12:42:46 +03:00
|
|
|
isContributor: equal('role.name', 'Contributor'),
|
2015-10-28 14:36:45 +03:00
|
|
|
isAuthor: equal('role.name', 'Author'),
|
|
|
|
isEditor: equal('role.name', 'Editor'),
|
2021-07-12 15:55:56 +03:00
|
|
|
isAdminOnly: equal('role.name', 'Administrator'),
|
|
|
|
isOwnerOnly: equal('role.name', 'Owner'),
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2019-01-22 19:22:12 +03:00
|
|
|
// These are used in enough places that it's useful to throw them here
|
2021-07-12 15:55:56 +03:00
|
|
|
isAdmin: or('isOwnerOnly', 'isAdminOnly'),
|
2018-02-07 12:42:46 +03:00
|
|
|
isAuthorOrContributor: or('isAuthor', 'isContributor'),
|
|
|
|
|
2016-06-14 14:46:24 +03:00
|
|
|
isLoggedIn: computed('id', 'session.user.id', function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.id === this.get('session.user.id');
|
2016-06-14 14:46:24 +03:00
|
|
|
}),
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2017-03-08 21:21:35 +03:00
|
|
|
isActive: computed('status', function () {
|
|
|
|
// TODO: review "locked" as an "active" status
|
2019-03-06 16:53:54 +03:00
|
|
|
return ['active', 'warn-1', 'warn-2', 'warn-3', 'warn-4', 'locked'].indexOf(this.status) > -1;
|
2015-10-28 14:36:45 +03:00
|
|
|
}),
|
|
|
|
|
2017-03-08 21:21:35 +03:00
|
|
|
isSuspended: equal('status', 'inactive'),
|
2017-07-22 00:21:27 +03:00
|
|
|
isLocked: equal('status', 'locked'),
|
2017-03-08 21:21:35 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
role: computed('roles', {
|
|
|
|
get() {
|
2015-06-03 05:56:42 +03:00
|
|
|
return this.get('roles.firstObject');
|
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
set(key, value) {
|
2014-10-25 01:09:50 +04:00
|
|
|
// Only one role per user, so remove any old data.
|
2019-03-06 16:53:54 +03:00
|
|
|
this.roles.clear();
|
|
|
|
this.roles.pushObject(value);
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-07-30 04:11:02 +04:00
|
|
|
return value;
|
|
|
|
}
|
2014-07-22 01:19:46 +04:00
|
|
|
}),
|
|
|
|
|
2018-05-14 14:15:08 +03:00
|
|
|
profileImageUrl: computed('ghostPaths.assetRoot', 'profileImage', function () {
|
2018-05-14 15:56:09 +03:00
|
|
|
// keep path separate so asset rewriting correctly picks it up
|
2018-05-14 14:15:08 +03:00
|
|
|
let defaultImage = '/img/user-image.png';
|
2018-08-16 01:58:37 +03:00
|
|
|
let defaultPath = this.ghostPaths.assetRoot.replace(/\/$/, '') + defaultImage;
|
2018-05-14 14:15:08 +03:00
|
|
|
return this.profileImage || defaultPath;
|
|
|
|
}),
|
|
|
|
|
|
|
|
coverImageUrl: computed('ghostPaths.assetRoot', 'coverImage', function () {
|
2018-05-14 15:56:09 +03:00
|
|
|
// keep path separate so asset rewriting correctly picks it up
|
2018-05-14 14:15:08 +03:00
|
|
|
let defaultImage = '/img/user-cover.png';
|
2018-08-16 01:58:37 +03:00
|
|
|
let defaultPath = this.ghostPaths.assetRoot.replace(/\/$/, '') + defaultImage;
|
2018-05-14 14:15:08 +03:00
|
|
|
return this.coverImage || defaultPath;
|
|
|
|
}),
|
|
|
|
|
2022-10-05 14:05:31 +03:00
|
|
|
saveNewPasswordTask: task(function* () {
|
|
|
|
const validation = this.isLoggedIn ? 'ownPasswordChange' : 'passwordChange';
|
2016-06-14 14:46:24 +03:00
|
|
|
|
2016-08-11 09:58:38 +03:00
|
|
|
try {
|
|
|
|
yield this.validate({property: validation});
|
|
|
|
} catch (e) {
|
|
|
|
// validation error, don't do anything
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-10-05 14:05:31 +03:00
|
|
|
let url = this.ghostPaths.url.api('users', 'password');
|
2016-08-11 09:58:38 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
yield this.ajax.put(url, {
|
2016-06-14 14:46:24 +03:00
|
|
|
data: {
|
|
|
|
password: [{
|
2019-03-06 16:53:54 +03:00
|
|
|
user_id: this.id,
|
|
|
|
oldPassword: this.password,
|
|
|
|
newPassword: this.newPassword,
|
|
|
|
ne2Password: this.ne2Password
|
2016-06-14 14:46:24 +03:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
});
|
2016-08-11 09:58:38 +03:00
|
|
|
|
2022-10-05 14:05:31 +03:00
|
|
|
this.password = '';
|
|
|
|
this.newPassword = '';
|
|
|
|
this.ne2Password = '';
|
2016-08-11 09:58:38 +03:00
|
|
|
|
2020-02-27 12:19:29 +03:00
|
|
|
this.notifications.showNotification('Password updated', {type: 'success', key: 'user.change-password.success'});
|
2016-08-11 09:58:38 +03:00
|
|
|
|
|
|
|
// clear errors manually for ne2password because validation
|
2022-10-05 14:05:31 +03:00
|
|
|
// engine only clears the "validated property"
|
2016-08-11 09:58:38 +03:00
|
|
|
// TODO: clean up once we have a better validations library
|
2019-03-06 16:53:54 +03:00
|
|
|
this.errors.remove('ne2Password');
|
2017-04-19 19:21:47 +03:00
|
|
|
|
|
|
|
return true;
|
2016-08-11 09:58:38 +03:00
|
|
|
} catch (error) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAPIError(error, {key: 'user.change-password'});
|
2016-08-11 09:58:38 +03:00
|
|
|
}
|
2016-09-26 19:03:53 +03:00
|
|
|
}).drop()
|
2014-03-23 06:31:45 +04:00
|
|
|
});
|