domain_registration.module in Restrict Domain Registration 8
Domain Registration module file.
File
domain_registration.module
View source
<?php
const DOMAIN_REGISTRATION_ALLOW = 0;
const DOMAIN_REGISTRATION_DENY = 1;
function domain_registration_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['#validate'][] = 'domain_registration_user_register_validate';
}
function domain_registration_user_register_validate(&$form, &$form_state) {
$errors = $form_state
->getErrors();
if (!empty($errors['mail'])) {
return;
}
$mail = explode('@', $form_state
->getValue('mail'));
$domains = \Drupal::service('domain_registration.pattern')
->getPatterns();
if ($domains) {
$match = count(array_filter($domains, function ($domain) use (&$mail) {
return domain_registration_wildcard_match($domain, $mail[1]);
}));
switch (\Drupal::config('domain_registration.settings')
->get('method')) {
case DOMAIN_REGISTRATION_ALLOW:
if (!$match) {
$form_state
->setErrorByName('account', \Drupal::config('domain_registration.settings')
->get('message'));
}
break;
case DOMAIN_REGISTRATION_DENY:
if ($match) {
$form_state
->setErrorByName('account', \Drupal::config('domain_registration.settings')
->get('message'));
}
break;
}
}
}
function domain_registration_wildcard_match($pattern, $string) {
return preg_match("#^" . strtr(preg_quote($pattern, '#'), [
'\\*' => '.*',
'\\?' => '.',
]) . "\$#i", $string);
}