2015-10-28 11:36:45 +00:00
|
|
|
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
|
2015-02-12 21:22:32 -07:00
|
|
|
import Ember from 'ember';
|
2016-01-12 19:23:01 +00:00
|
|
|
import Model from 'ember-data/model';
|
|
|
|
import attr from 'ember-data/attr';
|
|
|
|
import { hasMany } from 'ember-data/relationships';
|
2014-07-11 19:01:42 +00:00
|
|
|
import ValidationEngine from 'ghost/mixins/validation-engine';
|
|
|
|
|
2016-01-19 07:03:27 -06:00
|
|
|
const {
|
|
|
|
computed,
|
|
|
|
inject: {service}
|
|
|
|
} = Ember;
|
2015-10-28 11:36:45 +00:00
|
|
|
const {equal, empty} = computed;
|
|
|
|
|
|
|
|
export default Model.extend(ValidationEngine, {
|
2014-07-11 19:01:42 +00:00
|
|
|
validationType: 'user',
|
2014-07-14 16:32:55 +00:00
|
|
|
|
2015-10-28 11:36:45 +00: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 19:12:22 +01:00
|
|
|
metaTitle: attr('string'),
|
|
|
|
metaDescription: attr('string'),
|
|
|
|
lastLogin: attr('moment-date'),
|
|
|
|
createdAt: attr('moment-date'),
|
|
|
|
createdBy: attr('number'),
|
|
|
|
updatedAt: attr('moment-date'),
|
|
|
|
updatedBy: attr('number'),
|
2015-10-28 11:36:45 +00:00
|
|
|
roles: hasMany('role', {
|
2015-09-03 12:06:50 +01:00
|
|
|
embedded: 'always',
|
|
|
|
async: false
|
|
|
|
}),
|
2016-01-12 19:23:01 +00:00
|
|
|
count: attr('raw'),
|
2014-05-14 18:36:13 -05:00
|
|
|
|
2016-01-19 07:03:27 -06:00
|
|
|
ghostPaths: service(),
|
|
|
|
ajax: service(),
|
2015-10-28 11:36:45 +00: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-25 21:10:50 -05:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
role: computed('roles', {
|
|
|
|
get() {
|
2015-06-02 20:56:42 -06:00
|
|
|
return this.get('roles.firstObject');
|
|
|
|
},
|
2015-10-28 11:36:45 +00:00
|
|
|
set(key, value) {
|
2014-10-24 21:09:50 +00:00
|
|
|
// Only one role per user, so remove any old data.
|
2014-07-29 18:11:02 -06:00
|
|
|
this.get('roles').clear();
|
|
|
|
this.get('roles').pushObject(value);
|
2014-10-24 21:09:50 +00:00
|
|
|
|
2014-07-29 18:11:02 -06:00
|
|
|
return value;
|
|
|
|
}
|
2014-07-21 14:19:46 -07:00
|
|
|
}),
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
saveNewPassword() {
|
|
|
|
let url = this.get('ghostPaths.url').api('users', 'password');
|
2014-10-24 21:09:50 +00:00
|
|
|
|
2016-01-18 09:37:14 -06:00
|
|
|
return this.get('ajax').put(url, {
|
2014-07-13 00:01:26 -04:00
|
|
|
data: {
|
|
|
|
password: [{
|
2014-12-11 21:23:07 +01:00
|
|
|
user_id: this.get('id'),
|
2014-10-24 21:09:50 +00:00
|
|
|
oldPassword: this.get('password'),
|
|
|
|
newPassword: this.get('newPassword'),
|
|
|
|
ne2Password: this.get('ne2Password')
|
2014-07-13 00:01:26 -04:00
|
|
|
}]
|
|
|
|
}
|
2014-03-22 22:31:45 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
resendInvite() {
|
|
|
|
let fullUserData = this.toJSON();
|
|
|
|
let userData = {
|
|
|
|
email: fullUserData.email,
|
|
|
|
roles: fullUserData.roles
|
|
|
|
};
|
2016-01-18 09:37:14 -06:00
|
|
|
let inviteUrl = this.get('ghostPaths.url').api('users');
|
2014-07-08 01:24:07 -04:00
|
|
|
|
2016-01-18 09:37:14 -06:00
|
|
|
return this.get('ajax').post(inviteUrl, {
|
2014-07-08 01:24:07 -04:00
|
|
|
data: JSON.stringify({users: [userData]}),
|
|
|
|
contentType: 'application/json'
|
|
|
|
});
|
2015-10-28 11:36:45 +00:00
|
|
|
}
|
2014-03-22 22:31:45 -04:00
|
|
|
});
|