2014-07-11 23:01:42 +04:00
|
|
|
import ValidationEngine from 'ghost/mixins/validation-engine';
|
2014-07-06 00:44:19 +04:00
|
|
|
import NProgressSaveMixin from 'ghost/mixins/nprogress-save';
|
2014-07-26 09:12:37 +04:00
|
|
|
import SelectiveSaveMixin from 'ghost/mixins/selective-save';
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2014-07-26 09:12:37 +04:00
|
|
|
var User = DS.Model.extend(NProgressSaveMixin, SelectiveSaveMixin, ValidationEngine, {
|
2014-07-11 23:01:42 +04:00
|
|
|
validationType: 'user',
|
2014-07-14 20:32:55 +04:00
|
|
|
|
2014-05-09 09:00:10 +04:00
|
|
|
uuid: DS.attr('string'),
|
|
|
|
name: DS.attr('string'),
|
|
|
|
slug: DS.attr('string'),
|
|
|
|
email: DS.attr('string'),
|
|
|
|
image: DS.attr('string'),
|
|
|
|
cover: DS.attr('string'),
|
|
|
|
bio: DS.attr('string'),
|
|
|
|
website: DS.attr('string'),
|
|
|
|
location: DS.attr('string'),
|
|
|
|
accessibility: DS.attr('string'),
|
|
|
|
status: DS.attr('string'),
|
2014-07-05 12:06:10 +04:00
|
|
|
language: DS.attr('string', {defaultValue: 'en_US'}),
|
2014-05-09 09:00:10 +04:00
|
|
|
meta_title: DS.attr('string'),
|
|
|
|
meta_description: DS.attr('string'),
|
2014-06-05 20:04:59 +04:00
|
|
|
last_login: DS.attr('moment-date'),
|
|
|
|
created_at: DS.attr('moment-date'),
|
2014-07-23 10:13:20 +04:00
|
|
|
created_by: DS.attr('number'),
|
|
|
|
updated_at: DS.attr('moment-date'),
|
|
|
|
updated_by: DS.attr('number'),
|
2014-10-25 01:09:50 +04:00
|
|
|
roles: DS.hasMany('role', {embedded: 'always'}),
|
2014-05-15 03:36:13 +04:00
|
|
|
|
2014-07-30 04:11:02 +04:00
|
|
|
role: Ember.computed('roles', function (name, value) {
|
|
|
|
if (arguments.length > 1) {
|
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-10-25 01:09:50 +04:00
|
|
|
|
2014-07-30 04:11:02 +04:00
|
|
|
return this.get('roles.firstObject');
|
2014-07-22 01:19:46 +04:00
|
|
|
}),
|
|
|
|
|
2014-07-22 01:48:45 +04:00
|
|
|
// TODO: Once client-side permissions are in place,
|
|
|
|
// remove the hard role check.
|
2014-07-30 04:11:02 +04:00
|
|
|
isAuthor: Ember.computed.equal('role.name', 'Author'),
|
|
|
|
isEditor: Ember.computed.equal('role.name', 'Editor'),
|
|
|
|
isAdmin: Ember.computed.equal('role.name', 'Administrator'),
|
|
|
|
isOwner: Ember.computed.equal('role.name', 'Owner'),
|
2014-07-22 01:48:45 +04:00
|
|
|
|
2014-07-13 08:01:26 +04:00
|
|
|
saveNewPassword: function () {
|
|
|
|
var url = this.get('ghostPaths.url').api('users', 'password');
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-05-09 09:00:10 +04:00
|
|
|
return ic.ajax.request(url, {
|
2014-07-13 08:01:26 +04:00
|
|
|
type: 'PUT',
|
|
|
|
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
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-07-08 09:24:07 +04:00
|
|
|
resendInvite: function () {
|
2014-07-25 06:42:55 +04:00
|
|
|
var fullUserData = this.toJSON(),
|
|
|
|
userData = {
|
2014-10-25 01:09:50 +04:00
|
|
|
email: fullUserData.email,
|
|
|
|
roles: fullUserData.roles
|
|
|
|
};
|
2014-07-08 09:24:07 +04:00
|
|
|
|
2014-07-13 08:01:26 +04:00
|
|
|
return ic.ajax.request(this.get('ghostPaths.url').api('users'), {
|
2014-07-08 09:24:07 +04:00
|
|
|
type: 'POST',
|
|
|
|
data: JSON.stringify({users: [userData]}),
|
|
|
|
contentType: 'application/json'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-07-30 05:57:19 +04:00
|
|
|
passwordValidationErrors: Ember.computed('password', 'newPassword', 'ne2Password', function () {
|
2014-03-23 06:31:45 +04:00
|
|
|
var validationErrors = [];
|
|
|
|
|
2014-07-13 08:01:26 +04:00
|
|
|
if (!validator.equals(this.get('newPassword'), this.get('ne2Password'))) {
|
|
|
|
validationErrors.push({message: 'Your new passwords do not match'});
|
2014-03-23 06:31:45 +04:00
|
|
|
}
|
|
|
|
|
2014-07-13 08:01:26 +04:00
|
|
|
if (!validator.isLength(this.get('newPassword'), 8)) {
|
|
|
|
validationErrors.push({message: 'Your password is not long enough. It must be at least 8 characters long.'});
|
2014-03-23 06:31:45 +04:00
|
|
|
}
|
|
|
|
|
2014-05-09 09:00:10 +04:00
|
|
|
return validationErrors;
|
2014-07-30 05:57:19 +04:00
|
|
|
}),
|
2014-07-13 08:01:26 +04:00
|
|
|
|
2014-07-19 04:58:27 +04:00
|
|
|
isPasswordValid: Ember.computed.empty('passwordValidationErrors.[]'),
|
2014-07-30 05:57:19 +04:00
|
|
|
|
2014-08-01 00:29:35 +04:00
|
|
|
active: function () {
|
|
|
|
return ['active', 'warn-1', 'warn-2', 'warn-3', 'warn-4', 'locked'].indexOf(this.get('status')) > -1;
|
|
|
|
}.property('status'),
|
2014-10-04 20:38:15 +04:00
|
|
|
|
2014-08-01 00:29:35 +04:00
|
|
|
invited: function () {
|
|
|
|
return ['invited', 'invited-pending'].indexOf(this.get('status')) > -1;
|
|
|
|
}.property('status'),
|
|
|
|
|
|
|
|
pending: Ember.computed.equal('status', 'invited-pending').property('status')
|
2014-03-23 06:31:45 +04:00
|
|
|
});
|
2013-08-05 21:26:44 +04:00
|
|
|
|
2014-05-09 09:00:10 +04:00
|
|
|
export default User;
|