2014-03-23 06:31:45 +04:00
|
|
|
import BaseModel from 'ghost/models/base';
|
2013-08-05 21:26:44 +04:00
|
|
|
|
2014-03-23 06:31:45 +04:00
|
|
|
var UserModel = BaseModel.extend({
|
2014-05-15 03:36:13 +04:00
|
|
|
id: null,
|
|
|
|
name: null,
|
|
|
|
image: null,
|
|
|
|
|
|
|
|
isSignedIn: Ember.computed.bool('id'),
|
|
|
|
|
2014-03-23 06:31:45 +04:00
|
|
|
url: BaseModel.apiRoot + '/users/me/',
|
2014-03-26 03:34:30 +04:00
|
|
|
forgottenUrl: BaseModel.apiRoot + '/forgotten/',
|
2014-03-31 13:57:50 +04:00
|
|
|
resetUrl: BaseModel.apiRoot + '/reset/',
|
2014-04-29 00:42:38 +04:00
|
|
|
|
2014-03-23 06:31:45 +04:00
|
|
|
save: function () {
|
|
|
|
return ic.ajax.request(this.url, {
|
|
|
|
type: 'POST',
|
|
|
|
data: this.getProperties(Ember.keys(this))
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
validate: function () {
|
|
|
|
var validationErrors = [];
|
|
|
|
|
|
|
|
if (!validator.isLength(this.get('name'), 0, 150)) {
|
|
|
|
validationErrors.push({message: "Name is too long"});
|
|
|
|
}
|
2014-04-29 00:42:38 +04:00
|
|
|
|
2014-03-23 06:31:45 +04:00
|
|
|
if (!validator.isLength(this.get('bio'), 0, 200)) {
|
|
|
|
validationErrors.push({message: "Bio is too long"});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validator.isEmail(this.get('email'))) {
|
|
|
|
validationErrors.push({message: "Please supply a valid email address"});
|
|
|
|
}
|
2014-04-29 00:42:38 +04:00
|
|
|
|
2014-03-23 06:31:45 +04:00
|
|
|
if (!validator.isLength(this.get('location'), 0, 150)) {
|
|
|
|
validationErrors.push({message: "Location is too long"});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get('website').length) {
|
|
|
|
if (!validator.isURL(this.get('website')) ||
|
|
|
|
!validator.isLength(this.get('website'), 0, 2000)) {
|
|
|
|
validationErrors.push({message: "Please use a valid url"});
|
2014-04-29 00:42:38 +04:00
|
|
|
}
|
2014-03-23 06:31:45 +04:00
|
|
|
}
|
2014-04-29 00:42:38 +04:00
|
|
|
|
2014-03-23 06:31:45 +04:00
|
|
|
if (validationErrors.length > 0) {
|
|
|
|
this.set('isValid', false);
|
|
|
|
} else {
|
|
|
|
this.set('isValid', true);
|
2014-04-29 00:42:38 +04:00
|
|
|
}
|
2013-08-05 21:26:44 +04:00
|
|
|
|
2014-03-23 06:31:45 +04:00
|
|
|
this.set('errors', validationErrors);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
saveNewPassword: function (password) {
|
|
|
|
return ic.ajax.request(BaseModel.subdir + '/ghost/changepw/', {
|
|
|
|
type: 'POST',
|
|
|
|
data: password
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
validatePassword: function (password) {
|
|
|
|
var validationErrors = [];
|
|
|
|
|
2014-03-31 13:57:50 +04:00
|
|
|
if (!validator.equals(password.newPassword, password.ne2Password)) {
|
2014-03-23 06:31:45 +04:00
|
|
|
validationErrors.push("Your new passwords do not match");
|
|
|
|
}
|
|
|
|
|
2014-03-31 13:57:50 +04:00
|
|
|
if (!validator.isLength(password.newPassword, 8)) {
|
2014-03-23 06:31:45 +04:00
|
|
|
validationErrors.push("Your password is not long enough. It must be at least 8 characters long.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (validationErrors.length > 0) {
|
|
|
|
this.set('passwordIsValid', false);
|
|
|
|
} else {
|
|
|
|
this.set('passwordIsValid', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('passwordErrors', validationErrors);
|
|
|
|
|
|
|
|
return this;
|
2014-03-26 03:34:30 +04:00
|
|
|
},
|
2014-05-08 01:28:29 +04:00
|
|
|
|
2014-03-26 03:34:30 +04:00
|
|
|
fetchForgottenPasswordFor: function (email) {
|
|
|
|
var self = this;
|
|
|
|
return new Ember.RSVP.Promise(function (resolve, reject) {
|
|
|
|
if (!validator.isEmail(email)) {
|
|
|
|
reject(new Error('Please enter a correct email address.'));
|
|
|
|
} else {
|
|
|
|
resolve(ic.ajax.request(self.forgottenUrl, {
|
|
|
|
type: 'POST',
|
|
|
|
headers: {
|
|
|
|
// @TODO Find a more proper way to do this.
|
|
|
|
'X-CSRF-Token': $('meta[name="csrf-param"]').attr('content')
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
email: email
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
2014-03-31 13:57:50 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
resetPassword: function (passwords, token) {
|
|
|
|
var self = this;
|
|
|
|
return new Ember.RSVP.Promise(function (resolve, reject) {
|
|
|
|
if (!self.validatePassword(passwords).get('passwordIsValid')) {
|
|
|
|
reject(new Error('Errors found! ' + JSON.stringify(self.get('passwordErrors'))));
|
|
|
|
} else {
|
|
|
|
resolve(ic.ajax.request(self.resetUrl, {
|
|
|
|
type: 'POST',
|
|
|
|
headers: {
|
|
|
|
// @TODO: find a more proper way to do this.
|
|
|
|
'X-CSRF-Token': $('meta[name="csrf-param"]').attr('content')
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
newpassword: passwords.newPassword,
|
|
|
|
ne2password: passwords.ne2Password,
|
|
|
|
token: token
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
2014-03-23 06:31:45 +04:00
|
|
|
}
|
|
|
|
});
|
2013-08-05 21:26:44 +04:00
|
|
|
|
2014-03-23 06:31:45 +04:00
|
|
|
export default UserModel;
|