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) {
|
2022-10-07 22:13:42 +03:00
|
|
|
return (model.status === 'active');
|
2014-07-11 23:01:42 +04:00
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
name(model) {
|
2022-10-07 22:13:42 +03:00
|
|
|
let name = model.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)) {
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.add('name', 'Please enter a name.');
|
2015-07-07 20:14:23 +03:00
|
|
|
this.invalidate();
|
2017-11-09 17:31:25 +03:00
|
|
|
} else if (!validator.isLength(name, 0, 191)) {
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.add('name', 'Name is too long');
|
2015-06-07 06:19:19 +03:00
|
|
|
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) {
|
2022-10-07 22:13:42 +03:00
|
|
|
let bio = model.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)) {
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.add('bio', 'Bio is too long');
|
2015-06-07 06:19:19 +03:00
|
|
|
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) {
|
2022-10-07 22:13:42 +03:00
|
|
|
let email = model.email;
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2018-03-19 20:56:09 +03:00
|
|
|
if (!validator.isEmail(email || '')) {
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.add('email', 'Please supply a valid email address');
|
2015-06-07 06:19:19 +03:00
|
|
|
this.invalidate();
|
|
|
|
}
|
2017-11-16 15:47:56 +03:00
|
|
|
|
2018-03-19 20:56:09 +03:00
|
|
|
if (!validator.isLength(email || '', 0, 191)) {
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.add('email', 'Email is too long');
|
2017-11-16 15:47:56 +03:00
|
|
|
this.invalidate();
|
|
|
|
}
|
2015-06-07 06:19:19 +03:00
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
location(model) {
|
2022-10-07 22:13:42 +03:00
|
|
|
let location = model.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)) {
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.add('location', 'Location is too long');
|
2015-06-07 06:19:19 +03:00
|
|
|
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) {
|
2022-10-07 22:13:42 +03:00
|
|
|
let website = model.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) {
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.add('website', 'Website is not a valid url');
|
2015-06-07 06:19:19 +03:00
|
|
|
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)) {
|
2022-10-07 22:13:42 +03:00
|
|
|
let roles = model.roles;
|
2014-07-14 20:32:55 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (roles.length < 1) {
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.add('role', 'Please select a role');
|
2015-06-07 06:19:19 +03:00
|
|
|
this.invalidate();
|
|
|
|
}
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
2016-06-14 14:46:24 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
passwordChange(model) {
|
2022-10-07 22:13:42 +03:00
|
|
|
let newPassword = model.newPassword;
|
|
|
|
let ne2Password = model.ne2Password;
|
2016-06-14 14:46:24 +03:00
|
|
|
|
|
|
|
// validation only marks the requested property as validated so we
|
|
|
|
// have to add properties manually
|
2022-10-07 22:13:42 +03:00
|
|
|
model.hasValidated.addObject('newPassword');
|
|
|
|
model.hasValidated.addObject('ne2Password');
|
2016-06-14 14:46:24 +03:00
|
|
|
|
|
|
|
if (isBlank(newPassword) && isBlank(ne2Password)) {
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.add('newPassword', 'Sorry, passwords can\'t be blank');
|
2016-06-14 14:46:24 +03:00
|
|
|
this.invalidate();
|
|
|
|
} else {
|
2018-03-19 20:56:09 +03:00
|
|
|
if (!validator.equals(newPassword, ne2Password || '')) {
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.add('ne2Password', 'Your new passwords do not match');
|
2016-06-14 14:46:24 +03:00
|
|
|
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) {
|
2022-10-07 22:13:42 +03:00
|
|
|
let oldPassword = model.password;
|
2016-06-14 14:46:24 +03:00
|
|
|
|
|
|
|
this.passwordChange(model);
|
|
|
|
|
|
|
|
// validation only marks the requested property as validated so we
|
|
|
|
// have to add properties manually
|
2022-10-07 22:13:42 +03:00
|
|
|
model.hasValidated.addObject('password');
|
2016-06-14 14:46:24 +03:00
|
|
|
|
|
|
|
if (isBlank(oldPassword)) {
|
2022-10-07 22:13:42 +03:00
|
|
|
model.errors.add('password', 'Your current password is required to set a new one');
|
2016-06-14 14:46:24 +03:00
|
|
|
this.invalidate();
|
|
|
|
}
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
|
|
|
});
|
2018-09-17 17:52:07 +03:00
|
|
|
|
|
|
|
export default userValidator.create();
|