Ghost/core/client/models/user.js
Harry Wolff 81eb705a37 Create SettingsUserController
addresses #2422

- creates settings user controller

- creates user model object

- updates user fixture to be compatible with user model

- updates settings/user template

- add validator to Ember Admin

- use validator to validate user model is valid

- add mock response to /users/me/ path

- creates models/base file for all models to inherit from

- add mock response to /ghost/changepw/ path
2014-03-23 22:51:43 -04:00

80 lines
2.3 KiB
JavaScript

import BaseModel from 'ghost/models/base';
var UserModel = BaseModel.extend({
url: BaseModel.apiRoot + '/users/me/',
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"});
}
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"});
}
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"});
}
}
if (validationErrors.length > 0) {
this.set('isValid', false);
} else {
this.set('isValid', true);
}
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 = [];
if (!validator.equals(password.newpassword, password.ne2password)) {
validationErrors.push("Your new passwords do not match");
}
if (!validator.isLength(password.newpassword, 8)) {
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;
}
});
export default UserModel;