0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Add validation to invite new user form.

Closes #3246
- Add a UserValidator to the validation engine that runs a set
  of validations based on the user status.
- Added validations for invited users and active users.
This commit is contained in:
Jason Williams 2014-07-11 19:01:42 +00:00
parent ef1207cc0d
commit af51e94cf0
4 changed files with 79 additions and 39 deletions

View file

@ -24,18 +24,22 @@ var InviteNewUserController = Ember.Controller.extend({
self = this, self = this,
newUser; newUser;
this.notifications.closePassive();
newUser = this.store.createRecord('user', { newUser = this.store.createRecord('user', {
'email': email, email: email,
'role': role_id role: role_id,
status: 'invited'
}); });
newUser.save().then(function () { newUser.save().then(function () {
var notificationText = 'Invitation sent! (' + email + ')'; var notificationText = 'Invitation sent! (' + email + ')';
self.notifications.showSuccess(notificationText, false); self.notifications.showSuccess(notificationText, false);
}).fail(function (error) { }).catch(function (errors) {
newUser.deleteRecord();
self.notifications.closePassive(); self.notifications.closePassive();
self.notifications.showAPIError(error); self.notifications.showErrors(errors);
}); });
this.set('email', null); this.set('email', null);

View file

@ -8,6 +8,7 @@ import SigninValidator from 'ghost/validators/signin';
import ForgotValidator from 'ghost/validators/forgotten'; import ForgotValidator from 'ghost/validators/forgotten';
import SettingValidator from 'ghost/validators/setting'; import SettingValidator from 'ghost/validators/setting';
import ResetValidator from 'ghost/validators/reset'; import ResetValidator from 'ghost/validators/reset';
import UserValidator from 'ghost/validators/user';
// our extensions to the validator library // our extensions to the validator library
ValidatorExtensions.init(); ValidatorExtensions.init();
@ -70,7 +71,8 @@ var ValidationEngine = Ember.Mixin.create({
signin: SigninValidator, signin: SigninValidator,
forgotten: ForgotValidator, forgotten: ForgotValidator,
setting: SettingValidator, setting: SettingValidator,
reset: ResetValidator reset: ResetValidator,
user: UserValidator
}, },
/** /**

View file

@ -1,4 +1,8 @@
var User = DS.Model.extend({ import ValidationEngine from 'ghost/mixins/validation-engine';
var User = DS.Model.extend(ValidationEngine, {
validationType: 'user',
uuid: DS.attr('string'), uuid: DS.attr('string'),
name: DS.attr('string'), name: DS.attr('string'),
slug: DS.attr('string'), slug: DS.attr('string'),
@ -20,35 +24,6 @@ var User = DS.Model.extend({
updated_at: DS.attr('moment-date'), updated_at: DS.attr('moment-date'),
updated_by: DS.attr('number'), updated_by: DS.attr('number'),
validationErrors: 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 (!validator.isURL(this.get('website'), { protocols: ['http', 'https'], require_protocol: true }) ||
!validator.isLength(this.get('website'), 0, 2000)) {
validationErrors.push({message: 'Please use a valid url'});
}
return validationErrors;
}.property('name', 'bio', 'email', 'location', 'website'),
isValid: Ember.computed.empty('validationErrors.[]'),
saveNewPassword: function (password) { saveNewPassword: function (password) {
var url = this.get('ghostPaths').adminUrl('changepw'); var url = this.get('ghostPaths').adminUrl('changepw');
return ic.ajax.request(url, { return ic.ajax.request(url, {
@ -81,10 +56,7 @@ var User = DS.Model.extend({
} }
return validationErrors; return validationErrors;
}, }
}); });

View file

@ -0,0 +1,62 @@
var UserValidator = Ember.Object.create({
check: function (model) {
var validator = this.validators[model.get('status')];
if (typeof validator !== 'function') {
return [];
}
return validator(model);
},
validators: {
invited: function (model) {
var validationErrors = [],
email = model.get('email'),
role = model.get('role');
if (!validator.isEmail(email)) {
validationErrors.push({ message: 'Please supply a valid email address' });
}
if (!validator.isLength(role, 1)) {
validationErrors.push({ message: 'Please select a role' });
}
return validationErrors;
},
active: function (model) {
var validationErrors = [],
name = model.get('name'),
bio = model.get('bio'),
email = model.get('email'),
location = model.get('location'),
website = model.get('website');
if (!validator.isLength(name, 0, 150)) {
validationErrors.push({ message: 'Name is too long' });
}
if (!validator.isLength(bio, 0, 200)) {
validationErrors.push({ message: 'Bio is too long' });
}
if (!validator.isEmail(email)) {
validationErrors.push({ message: 'Please supply a valid email address' });
}
if (!validator.isLength(location, 0, 150)) {
validationErrors.push({ message: 'Location is too long' });
}
if (!validator.isURL(website, { protocols: ['http', 'https'], require_protocol: true }) ||
!validator.isLength(website, 0, 2000)) {
validationErrors.push({ message: 'Please use a valid url' });
}
}
}
});
export default UserValidator;