2015-10-28 14:36:45 +03:00
|
|
|
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
|
2016-01-12 22:23:01 +03:00
|
|
|
import Model from 'ember-data/model';
|
|
|
|
import attr from 'ember-data/attr';
|
2016-06-14 14:46:24 +03:00
|
|
|
import {hasMany} from 'ember-data/relationships';
|
|
|
|
import computed, {equal} from 'ember-computed';
|
|
|
|
import injectService from 'ember-service/inject';
|
2016-05-24 15:06:59 +03:00
|
|
|
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Model.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
|
|
|
uuid: attr('string'),
|
|
|
|
name: attr('string'),
|
|
|
|
slug: attr('string'),
|
|
|
|
email: attr('string'),
|
|
|
|
image: attr('string'),
|
|
|
|
cover: attr('string'),
|
|
|
|
bio: attr('string'),
|
|
|
|
website: attr('string'),
|
|
|
|
location: attr('string'),
|
|
|
|
accessibility: attr('string'),
|
|
|
|
status: attr('string'),
|
|
|
|
language: attr('string', {defaultValue: 'en_US'}),
|
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'),
|
2014-05-15 03:36:13 +04:00
|
|
|
|
2016-06-30 13:21:47 +03:00
|
|
|
ghostPaths: injectService(),
|
|
|
|
ajax: injectService(),
|
2016-06-14 14:46:24 +03:00
|
|
|
session: injectService(),
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
// TODO: Once client-side permissions are in place,
|
|
|
|
// remove the hard role check.
|
|
|
|
isAuthor: equal('role.name', 'Author'),
|
|
|
|
isEditor: equal('role.name', 'Editor'),
|
|
|
|
isAdmin: equal('role.name', 'Administrator'),
|
|
|
|
isOwner: equal('role.name', 'Owner'),
|
|
|
|
|
2016-06-14 14:46:24 +03:00
|
|
|
isLoggedIn: computed('id', 'session.user.id', function () {
|
|
|
|
return this.get('id') === this.get('session.user.id');
|
|
|
|
}),
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
active: computed('status', function () {
|
|
|
|
return ['active', 'warn-1', 'warn-2', 'warn-3', 'warn-4', 'locked'].indexOf(this.get('status')) > -1;
|
|
|
|
}),
|
|
|
|
|
|
|
|
invited: computed('status', function () {
|
|
|
|
return ['invited', 'invited-pending'].indexOf(this.get('status')) > -1;
|
|
|
|
}),
|
|
|
|
|
2016-04-08 18:00:16 +03:00
|
|
|
pending: equal('status', 'invited-pending'),
|
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.
|
2014-07-30 04:11:02 +04:00
|
|
|
this.get('roles').clear();
|
|
|
|
this.get('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
|
|
|
}),
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
saveNewPassword() {
|
|
|
|
let url = this.get('ghostPaths.url').api('users', 'password');
|
2016-06-14 14:46:24 +03:00
|
|
|
let validation = this.get('isLoggedIn') ? 'ownPasswordChange' : 'passwordChange';
|
|
|
|
|
|
|
|
return this.validate({property: validation}).then(() => {
|
|
|
|
return this.get('ajax').put(url, {
|
|
|
|
data: {
|
|
|
|
password: [{
|
|
|
|
user_id: this.get('id'),
|
|
|
|
oldPassword: this.get('password'),
|
|
|
|
newPassword: this.get('newPassword'),
|
|
|
|
ne2Password: this.get('ne2Password')
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
});
|
2014-03-23 06:31:45 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
resendInvite() {
|
|
|
|
let fullUserData = this.toJSON();
|
|
|
|
let userData = {
|
|
|
|
email: fullUserData.email,
|
|
|
|
roles: fullUserData.roles
|
|
|
|
};
|
2016-01-18 18:37:14 +03:00
|
|
|
let inviteUrl = this.get('ghostPaths.url').api('users');
|
2014-07-08 09:24:07 +04:00
|
|
|
|
2016-01-18 18:37:14 +03:00
|
|
|
return this.get('ajax').post(inviteUrl, {
|
2014-07-08 09:24:07 +04:00
|
|
|
data: JSON.stringify({users: [userData]}),
|
|
|
|
contentType: 'application/json'
|
|
|
|
});
|
2015-10-28 14:36:45 +03:00
|
|
|
}
|
2014-03-23 06:31:45 +04:00
|
|
|
});
|