2018-09-17 17:52:07 +03:00
|
|
|
import BaseValidator from './base';
|
|
|
|
import PasswordValidatorMixin from './mixins/password';
|
2019-01-22 16:09:38 +03:00
|
|
|
import validator from 'validator';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2018-09-17 17:52:07 +03:00
|
|
|
const userValidator = BaseValidator.extend(PasswordValidatorMixin, {
|
|
|
|
init() {
|
|
|
|
this.properties = this.properties || ['name', 'bio', 'email', 'location', 'website', 'roles'];
|
|
|
|
this._super(...arguments);
|
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
isActive(model) {
|
2015-06-07 06:19:19 +03:00
|
|
|
return (model.get('status') === 'active');
|
2014-07-11 23:01:42 +04:00
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
name(model) {
|
|
|
|
let name = model.get('name');
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (this.isActive(model)) {
|
2018-03-19 20:56:09 +03:00
|
|
|
if (isBlank(name)) {
|
2015-07-07 20:14:23 +03:00
|
|
|
model.get('errors').add('name', 'Please enter a name.');
|
|
|
|
this.invalidate();
|
2017-11-09 17:31:25 +03:00
|
|
|
} else if (!validator.isLength(name, 0, 191)) {
|
2015-06-07 06:19:19 +03:00
|
|
|
model.get('errors').add('name', 'Name is too long');
|
|
|
|
this.invalidate();
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
2015-06-07 06:19:19 +03:00
|
|
|
}
|
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
bio(model) {
|
|
|
|
let bio = model.get('bio');
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (this.isActive(model)) {
|
2018-03-19 20:56:09 +03:00
|
|
|
if (!validator.isLength(bio || '', 0, 200)) {
|
2015-06-07 06:19:19 +03:00
|
|
|
model.get('errors').add('bio', 'Bio is too long');
|
|
|
|
this.invalidate();
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
2015-06-07 06:19:19 +03:00
|
|
|
}
|
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
email(model) {
|
|
|
|
let email = model.get('email');
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2018-03-19 20:56:09 +03:00
|
|
|
if (!validator.isEmail(email || '')) {
|
2015-06-07 06:19:19 +03:00
|
|
|
model.get('errors').add('email', 'Please supply a valid email address');
|
|
|
|
this.invalidate();
|
|
|
|
}
|
2017-11-16 15:47:56 +03:00
|
|
|
|
2018-03-19 20:56:09 +03:00
|
|
|
if (!validator.isLength(email || '', 0, 191)) {
|
2017-11-16 15:47:56 +03:00
|
|
|
model.get('errors').add('email', 'Email is too long');
|
|
|
|
this.invalidate();
|
|
|
|
}
|
2015-06-07 06:19:19 +03:00
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
location(model) {
|
|
|
|
let location = model.get('location');
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (this.isActive(model)) {
|
2018-03-19 20:56:09 +03:00
|
|
|
if (!validator.isLength(location || '', 0, 150)) {
|
2015-06-07 06:19:19 +03:00
|
|
|
model.get('errors').add('location', 'Location is too long');
|
|
|
|
this.invalidate();
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
2015-06-07 06:19:19 +03:00
|
|
|
}
|
|
|
|
},
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
website(model) {
|
|
|
|
let website = model.get('website');
|
2016-11-14 16:16:51 +03:00
|
|
|
// eslint-disable-next-line camelcase
|
2018-03-19 20:56:09 +03:00
|
|
|
let isInvalidWebsite = !validator.isURL(website || '', {require_protocol: false})
|
|
|
|
|| !validator.isLength(website || '', 0, 2000);
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (this.isActive(model)) {
|
2018-03-19 20:56:09 +03:00
|
|
|
if (!isBlank(website) && isInvalidWebsite) {
|
2015-06-07 06:19:19 +03:00
|
|
|
model.get('errors').add('website', 'Website is not a valid url');
|
|
|
|
this.invalidate();
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
2015-06-07 06:19:19 +03:00
|
|
|
}
|
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
roles(model) {
|
2015-06-07 06:19:19 +03:00
|
|
|
if (!this.isActive(model)) {
|
2015-10-28 14:36:45 +03:00
|
|
|
let roles = model.get('roles');
|
2014-07-14 20:32:55 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (roles.length < 1) {
|
|
|
|
model.get('errors').add('role', 'Please select a role');
|
|
|
|
this.invalidate();
|
|
|
|
}
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
2016-06-14 14:46:24 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
passwordChange(model) {
|
|
|
|
let newPassword = model.get('newPassword');
|
|
|
|
let ne2Password = model.get('ne2Password');
|
|
|
|
|
|
|
|
// validation only marks the requested property as validated so we
|
|
|
|
// have to add properties manually
|
|
|
|
model.get('hasValidated').addObject('newPassword');
|
|
|
|
model.get('hasValidated').addObject('ne2Password');
|
|
|
|
|
|
|
|
if (isBlank(newPassword) && isBlank(ne2Password)) {
|
|
|
|
model.get('errors').add('newPassword', 'Sorry, passwords can\'t be blank');
|
|
|
|
this.invalidate();
|
|
|
|
} else {
|
2018-03-19 20:56:09 +03:00
|
|
|
if (!validator.equals(newPassword, ne2Password || '')) {
|
2016-06-14 14:46:24 +03:00
|
|
|
model.get('errors').add('ne2Password', 'Your new passwords do not match');
|
|
|
|
this.invalidate();
|
|
|
|
}
|
|
|
|
|
2017-10-26 13:02:17 +03:00
|
|
|
this.passwordValidation(model, newPassword, 'newPassword');
|
2016-06-14 14:46:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
ownPasswordChange(model) {
|
|
|
|
let oldPassword = model.get('password');
|
|
|
|
|
|
|
|
this.passwordChange(model);
|
|
|
|
|
|
|
|
// validation only marks the requested property as validated so we
|
|
|
|
// have to add properties manually
|
|
|
|
model.get('hasValidated').addObject('password');
|
|
|
|
|
|
|
|
if (isBlank(oldPassword)) {
|
|
|
|
model.get('errors').add('password', 'Your current password is required to set a new one');
|
|
|
|
this.invalidate();
|
|
|
|
}
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
|
|
|
});
|
2018-09-17 17:52:07 +03:00
|
|
|
|
|
|
|
export default userValidator.create();
|