Ghost/ghost/admin/app/models/user.js

143 lines
4.8 KiB
JavaScript
Raw Normal View History

/* eslint-disable camelcase */
import BaseModel from './base';
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
import {attr, hasMany} from '@ember-data/model';
import {computed} from '@ember/object';
import {equal, or} from '@ember/object/computed';
Fixed hosting management screen not loading after sign-in process (#15763) refs https://github.com/TryGhost/Team/issues/2110 - dynamically defined properties on the config service did not have autotracking set up properly if they were accessed in any way before the property was defined, this caused problems in a number of areas because we have both "unauthed" and "authed" sets of config and when not logged in we had parts of the app checking for authed config properties that don't exist until after sign-in and subsequent config re-fetch - renamed `config` service to `configManager` and updated to only contain methods for fetching config data - added a `config` instance initializer that sets up a `TrackedObject` instance with some custom properties/methods and registers it on `config:main` - uses application instance initializer rather than a standard initializer because standard initializers are only called once when setting up the test suite so we'd end up with config leaking across tests - added an `@inject` decorator that when used takes the property name and injects whatever is registered at `${propertyName}:main`, this allows us to use dependency injection for any object rather than just services or controllers - using `application.inject()` in the initializer was initially used but that only works for objects that extend from `EmberObject`, the injections weren't available in native-class glimmer components so this decorator keeps the injection syntax consistent - swapped all `@service config` uses to `@inject config`
2022-11-03 14:14:36 +03:00
import {inject} from 'ghost-admin/decorators/inject';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
export default BaseModel.extend(ValidationEngine, {
validationType: 'user',
name: attr('string'),
slug: attr('string'),
email: attr('string'),
profileImage: attr('string'),
coverImage: attr('string'),
bio: attr('string'),
website: attr('string'),
location: attr('string'),
accessibility: attr('string'),
status: attr('string'),
metaTitle: attr('string'),
metaDescription: attr('string'),
lastLoginUTC: attr('moment-utc'),
createdAtUTC: attr('moment-utc'),
createdBy: attr('number'),
updatedAtUTC: attr('moment-utc'),
updatedBy: attr('number'),
roles: hasMany('role', {
embedded: 'always',
async: false
}),
count: attr('raw'),
facebook: attr('facebook-url-user'),
twitter: attr('twitter-url-user'),
tour: attr('json-string'),
commentNotifications: attr(),
freeMemberSignupNotification: attr(),
paidSubscriptionStartedNotification: attr(),
paidSubscriptionCanceledNotification: attr(),
ghostPaths: service(),
ajax: service(),
session: service(),
notifications: service(),
Fixed hosting management screen not loading after sign-in process (#15763) refs https://github.com/TryGhost/Team/issues/2110 - dynamically defined properties on the config service did not have autotracking set up properly if they were accessed in any way before the property was defined, this caused problems in a number of areas because we have both "unauthed" and "authed" sets of config and when not logged in we had parts of the app checking for authed config properties that don't exist until after sign-in and subsequent config re-fetch - renamed `config` service to `configManager` and updated to only contain methods for fetching config data - added a `config` instance initializer that sets up a `TrackedObject` instance with some custom properties/methods and registers it on `config:main` - uses application instance initializer rather than a standard initializer because standard initializers are only called once when setting up the test suite so we'd end up with config leaking across tests - added an `@inject` decorator that when used takes the property name and injects whatever is registered at `${propertyName}:main`, this allows us to use dependency injection for any object rather than just services or controllers - using `application.inject()` in the initializer was initially used but that only works for objects that extend from `EmberObject`, the injections weren't available in native-class glimmer components so this decorator keeps the injection syntax consistent - swapped all `@service config` uses to `@inject config`
2022-11-03 14:14:36 +03:00
config: inject(),
// TODO: Once client-side permissions are in place,
// remove the hard role check.
isContributor: equal('role.name', 'Contributor'),
isAuthor: equal('role.name', 'Author'),
isEditor: equal('role.name', 'Editor'),
isAdminOnly: equal('role.name', 'Administrator'),
isOwnerOnly: equal('role.name', 'Owner'),
2019-01-22 19:22:12 +03:00
// These are used in enough places that it's useful to throw them here
isAdmin: or('isOwnerOnly', 'isAdminOnly'),
isAuthorOrContributor: or('isAuthor', 'isContributor'),
isLoggedIn: computed('id', 'session.user.id', function () {
return this.id === this.get('session.user.id');
}),
isActive: computed('status', function () {
// TODO: review "locked" as an "active" status
return ['active', 'warn-1', 'warn-2', 'warn-3', 'warn-4', 'locked'].indexOf(this.status) > -1;
}),
isSuspended: equal('status', 'inactive'),
isLocked: equal('status', 'locked'),
role: computed('roles', {
get() {
return this.get('roles.firstObject');
},
set(key, value) {
// Only one role per user, so remove any old data.
this.roles.clear();
this.roles.pushObject(value);
return value;
}
}),
profileImageUrl: computed('ghostPaths.assetRoot', 'profileImage', function () {
2018-05-14 15:56:09 +03:00
// keep path separate so asset rewriting correctly picks it up
let defaultImage = '/img/user-image.png';
let defaultPath = this.ghostPaths.assetRoot.replace(/\/$/, '') + defaultImage;
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
let defaultImage = '/img/user-cover.png';
let defaultPath = this.ghostPaths.assetRoot.replace(/\/$/, '') + defaultImage;
return this.coverImage || defaultPath;
}),
saveNewPasswordTask: task(function* () {
const validation = this.isLoggedIn ? 'ownPasswordChange' : 'passwordChange';
try {
yield this.validate({property: validation});
} catch (e) {
// validation error, don't do anything
return;
}
try {
let url = this.ghostPaths.url.api('users', 'password');
yield this.ajax.put(url, {
data: {
password: [{
user_id: this.id,
oldPassword: this.password,
newPassword: this.newPassword,
ne2Password: this.ne2Password
}]
}
});
this.password = '';
this.newPassword = '';
this.ne2Password = '';
this.notifications.showNotification('Password updated', {type: 'success', key: 'user.change-password.success'});
// clear errors manually for ne2password because validation
// engine only clears the "validated property"
// TODO: clean up once we have a better validations library
this.errors.remove('ne2Password');
return true;
} catch (error) {
this.notifications.showAPIError(error, {key: 'user.change-password'});
}
}).drop()
});