#398: implement idna domain conversion for emails

This commit is contained in:
ErickSkrauch 2018-04-21 16:07:09 +03:00
parent bcff944be2
commit 03bd5ec144
2 changed files with 19 additions and 2 deletions

View File

@ -38,6 +38,11 @@ class EmailValidator extends Validator {
$tempmail = new TempmailValidator();
$tempmail->message = E::EMAIL_IS_TEMPMAIL;
$idnaDomain = new validators\FilterValidator(['filter' => function(string $value): string {
[$name, $domain] = explode('@', $value);
return $name . '@' . idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46);
}]);
$unique = new validators\UniqueValidator();
$unique->message = E::EMAIL_NOT_AVAILABLE;
$unique->targetClass = Account::class;
@ -53,12 +58,12 @@ class EmailValidator extends Validator {
$this->executeValidation($length, $model, $attribute) &&
$this->executeValidation($email, $model, $attribute) &&
$this->executeValidation($tempmail, $model, $attribute) &&
$this->executeValidation($idnaDomain, $model, $attribute) &&
$this->executeValidation($unique, $model, $attribute);
}
protected function executeValidation(Validator $validator, Model $model, string $attribute) {
private function executeValidation(Validator $validator, Model $model, string $attribute): bool {
$validator->validateAttribute($model, $attribute);
return !$model->hasErrors($attribute);
}

View File

@ -84,6 +84,18 @@ class EmailValidatorTest extends TestCase {
$this->assertNotEquals(['error.email_is_tempmail'], $model->getErrors('field'));
}
public function testValidateAttributeIdna() {
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->times(2)->andReturnTrue();
$model = $this->createModel('qdushyantasunassm@❕.gq');
$this->validator->validateAttribute($model, 'field');
$this->assertSame('qdushyantasunassm@xn--bei.gq', $model->field);
$model = $this->createModel('valid-email@gmail.com');
$this->validator->validateAttribute($model, 'field');
$this->assertSame('valid-email@gmail.com', $model->field);
}
public function testValidateAttributeUnique() {
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->times(3)->andReturnTrue();