2015-10-28 14:36:45 +03:00
|
|
|
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
|
2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2016-01-12 22:23:01 +03:00
|
|
|
import Model from 'ember-data/model';
|
|
|
|
import attr from 'ember-data/attr';
|
|
|
|
import { hasMany } from 'ember-data/relationships';
|
2014-07-11 23:01:42 +04:00
|
|
|
import ValidationEngine from 'ghost/mixins/validation-engine';
|
|
|
|
|
2016-01-19 16:03:27 +03:00
|
|
|
const {
|
|
|
|
computed,
|
|
|
|
inject: {service}
|
|
|
|
} = Ember;
|
2015-10-28 14:36:45 +03:00
|
|
|
const {equal, empty} = computed;
|
|
|
|
|
|
|
|
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'}),
|
|
|
|
meta_title: attr('string'),
|
|
|
|
meta_description: attr('string'),
|
|
|
|
last_login: attr('moment-date'),
|
|
|
|
created_at: attr('moment-date'),
|
|
|
|
created_by: attr('number'),
|
|
|
|
updated_at: attr('moment-date'),
|
|
|
|
updated_by: attr('number'),
|
|
|
|
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'),
|
2014-05-15 03:36:13 +04:00
|
|
|
|
2016-01-19 16:03:27 +03:00
|
|
|
ghostPaths: service(),
|
|
|
|
ajax: service(),
|
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'),
|
|
|
|
|
|
|
|
isPasswordValid: empty('passwordValidationErrors.[]'),
|
|
|
|
|
|
|
|
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;
|
|
|
|
}),
|
|
|
|
|
|
|
|
pending: equal('status', 'invited-pending').property('status'),
|
|
|
|
|
|
|
|
passwordValidationErrors: computed('password', 'newPassword', 'ne2Password', function () {
|
|
|
|
let validationErrors = [];
|
|
|
|
|
|
|
|
if (!validator.equals(this.get('newPassword'), this.get('ne2Password'))) {
|
|
|
|
validationErrors.push({message: 'Your new passwords do not match'});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validator.isLength(this.get('newPassword'), 8)) {
|
|
|
|
validationErrors.push({message: 'Your password is not long enough. It must be at least 8 characters long.'});
|
|
|
|
}
|
|
|
|
|
|
|
|
return validationErrors;
|
|
|
|
}),
|
2015-05-26 05:10:50 +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.
|
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');
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2016-01-18 18:37:14 +03:00
|
|
|
return this.get('ajax').put(url, {
|
2014-07-13 08:01:26 +04:00
|
|
|
data: {
|
|
|
|
password: [{
|
2014-12-11 23:23:07 +03:00
|
|
|
user_id: this.get('id'),
|
2014-10-25 01:09:50 +04:00
|
|
|
oldPassword: this.get('password'),
|
|
|
|
newPassword: this.get('newPassword'),
|
|
|
|
ne2Password: this.get('ne2Password')
|
2014-07-13 08:01:26 +04:00
|
|
|
}]
|
|
|
|
}
|
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
|
|
|
});
|