mirror of
https://github.com/elyby/accounts.git
synced 2024-12-23 13:50:06 +05:30
#398: implement idna domain conversion for emails
This commit is contained in:
parent
bcff944be2
commit
03bd5ec144
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user