Commit 549a70a7 authored by bgm's avatar bgm Committed by Aegir user

Add domain_registration to block spam registrations

parent 64e0306e
This diff is collapsed.
name = Domain Registration
description = Restrict user registration to email addresses of particular domains
core = 7.x
configure = admin/config/system/domain_register
files[] = domain_registration.module
files[] = domain_registration.test
; Information added by Drupal.org packaging script on 2015-11-20
version = "7.x-1.3"
core = "7.x"
project = "domain_registration"
datestamp = "1448052130"
<?php
/**
* @file
* Domain Registration module file.
*/
define('DOMAIN_REGISTRATION_ALLOW', 0);
define('DOMAIN_REGISTRATION_DENY', 1);
/**
* Implements hook_menu().
*/
function domain_registration_menu() {
$items['admin/config/system/domain_register'] = array(
'title' => 'Domain registration',
'description' => 'Set domains for registration',
'page callback' => 'drupal_get_form',
'page arguments' => array('domain_registration_admin_form'),
'access arguments' => array('administer domain registration'),
);
return $items;
}
/**
* Implements hook_permission().
*/
function domain_registration_permission() {
return array(
'administer domain registration' => array(
'title' => t('Administer domain registration'),
'description' => t('Allows a user to administer the domain registration settings.'),
),
);
}
/**
* Administration form for domain registration.
*/
function domain_registration_admin_form($form, &$form_state) {
$options = array(
DOMAIN_REGISTRATION_ALLOW => t('Allow only domains listed below to register'),
DOMAIN_REGISTRATION_DENY => t('Prevent domains listed below from registering'),
);
$form['domain_registration_method'] = array(
'#type' => 'radios',
'#required' => TRUE,
'#options' => $options,
'#title' => t('Restriction Type'),
'#default_value' => variable_get('domain_registration_method', DOMAIN_REGISTRATION_ALLOW),
'#description' => t('Choose which method you would like the domains list to operate. Only allow domains listed to register, or prevent domains listed from registering.'),
);
$form['domain_registration'] = array(
'#type' => 'textarea',
'#required' => TRUE,
'#title' => t('Email domains'),
'#default_value' => variable_get('domain_registration'),
'#description' => t('Enter the domains you wish to restrict registration. One entry per line in the format (e.g. something.com). Wildcards are also supported (e.g. *.something.com) to match any subdomain.'),
);
$form['domain_registration_message'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#title' => t('Error message'),
'#default_value' => variable_get('domain_registration_message', t('You are not allowed to register for this site.')),
'#description' => t('Enter the error message you want the user to see if the email address does not validate.'),
);
return system_settings_form($form);
}
/**
* Implements hook_form_form_id_form_alter().
*/
function domain_registration_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['#validate'][] = 'domain_registration_user_register_validate';
}
/**
* Custom validation function.
*
* Checks if the domain in the email address is on a list of allowed domains.
*/
function domain_registration_user_register_validate(&$form, &$form_state) {
// Ignore validation if mail already has an error.
$errors = form_get_errors();
if (!empty($errors['mail'])) {
return;
}
$default_message = t('You are not allowed to register for this site.');
$mail = explode('@', $form_state['values']['mail']);
$domains = variable_get('domain_registration', array());
// Only attempt to validate if we have a list of domains.
if (!empty($domains)) {
$domains = explode("\r\n", $domains);
// Determine if we have matches.
$match = count(array_filter($domains,
function($domain) use (&$mail){
return domain_registration_wildcard_match($domain, $mail[1]);
}
));
switch (variable_get('domain_registration_method', DOMAIN_REGISTRATION_ALLOW)) {
// Allow only domains listed to register.
case 0:
if (!$match) {
form_set_error('account', variable_get('domain_registration_message', $default_message));
}
break;
// Prevent domains listed from registering.
case 1:
if ($match) {
form_set_error('account', variable_get('domain_registration_message', $default_message));
}
break;
}
}
}
/**
* Pattern Match wildcard addresses.
*/
function domain_registration_wildcard_match($pattern, $string) {
return preg_match("#^" . strtr(preg_quote($pattern, '#'), array('\*' => '.*', '\?' => '.')) . "$#i", $string);
}
<?php
/**
* @file
* Tests for the Domain Registration Module
*/
class DomainRegistrationTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Domain Registration',
'description' => 'Test if the Domain Registration module properly allows and denies specified domains',
'group' => 'Domain Registration',
);
}
public function setUp() {
parent::setUp(array('domain_registration'));
variable_set('user_register', USER_REGISTER_VISITORS);
variable_set('user_email_verification', TRUE);
variable_set('domain_registration', 'example.com');
}
public function testAllowExactMatch() {
variable_set('domain_registration_method', DOMAIN_REGISTRATION_ALLOW);
variable_set('domain_registration', 'example.com');
// Get the user data for registration.
$edit = array(
'name' => $this->randomName(),
'mail' => $this->randomName() . '@example.com',
);
// Attempt to register a user with a whitelist email.
$this->drupalPost('user/register', $edit, t('Create new account'));
$this->assertText(t('A welcome message'), t('User registered successfully.'));
}
public function testAllowEmptyPatternMatch() {
variable_set('domain_registration_method', DOMAIN_REGISTRATION_ALLOW);
variable_set('domain_registration', '');
// Get the user data for registration.
$edit = array(
'name' => $this->randomName(),
'mail' => $this->randomName() . '@example.com',
);
// Attempt to register a user with a whitelist email.
$this->drupalPost('user/register', $edit, t('Create new account'));
$this->assertText(t('A welcome message'), t('User registered successfully.'));
}
public function testAllowWildcardMatch() {
variable_set('domain_registration_method', DOMAIN_REGISTRATION_ALLOW);
variable_set('domain_registration', '*.example.com');
// Get the user data for registration.
$edit = array(
'name' => $this->randomName(),
'mail' => $this->randomName() . '@subdomain.example.com',
);
// Attempt to register a user with a whitelist email.
$this->drupalPost('user/register', $edit, t('Create new account'));
$this->assertText(t('A welcome message'), t('User registered successfully.'));
}
public function testDenyNoMatch() {
variable_set('domain_registration', 'example.com');
variable_set('domain_registration_method', DOMAIN_REGISTRATION_ALLOW);
// Get the user data for registration.
$edit = array(
'name' => $this->randomName(),
'mail' => $this->randomName() . '@otherexample.com',
);
// Attempt to register a user with a non whitelist email.
$this->drupalPost('user/register', $edit, t('Create new account'));
$this->assertText(t('You are not allowed to register for this site.'), t('User was successfully denied registration.'));
}
public function testDenyExactMatch() {
variable_set('domain_registration', 'example.com');
variable_set('domain_registration_method', DOMAIN_REGISTRATION_DENY);
// Get the user data for registration.
$edit = array(
'name' => $this->randomName(),
'mail' => $this->randomName() . '@example.com',
);
// Attempt to register a user with a blacklist email.
$this->drupalPost('user/register', $edit, t('Create new account'));
$this->assertText(t('You are not allowed to register for this site.'), t('User was successfully denied registration.'));
}
public function testDenyWildcardMatch() {
variable_set('domain_registration', '*.example.com');
variable_set('domain_registration_method', DOMAIN_REGISTRATION_DENY);
// Get the user data for registration.
$edit = array(
'name' => $this->randomName(),
'mail' => $this->randomName() . '@subdomain.example.com',
);
// Attempt to register a user with blacklist email.
$this->drupalPost('user/register', $edit, t('Create new account'));
$this->assertText(t('You are not allowed to register for this site.'), t('User was successfully denied registration.'));
}
public function testDenyCustomMessage() {
variable_set('domain_registration', 'example.com');
variable_set('domain_registration_method', DOMAIN_REGISTRATION_DENY);
variable_set('domain_registration_message', 'foo bar baz');
// Get the user data for registration.
$edit = array(
'name' => $this->randomName(),
'mail' => $this->randomName() . '@example.com',
);
// Attempt to register a user with a blacklist email.
$this->drupalPost('user/register', $edit, t('Create new account'));
$this->assertText(t('foo bar baz'), t('Custom denial message was shown.'));
}
public function testAllowNoMatch() {
variable_set('domain_registration', 'example.com');
variable_set('domain_registration_method', DOMAIN_REGISTRATION_DENY);
// Get the user data for registration.
$edit = array(
'name' => $this->randomName(),
'mail' => $this->randomName() . '@otherexample.com',
);
// Attempt to register a user with a non blacklist email.
$this->drupalPost('user/register', $edit, t('Create new account'));
$this->assertText(t('A welcome message'), t('User registered successfully.'));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment