You are here

domain_registration.module in Restrict Domain Registration 7

Same filename and directory in other branches
  1. 8 domain_registration.module

Domain Registration module file.

File

domain_registration.module
View source
<?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.'),
    ),
  );
}

/**
 * Implements hook_variable_info().
 */
function domain_registration_variable_info($options) {
  $variables = array();
  $variables['domain_registration_message'] = array(
    'type' => 'string',
    'title' => t('Domain Registration Error message'),
    'description' => t('This error message will be displayed when the email the user is trying to register with does not validate'),
    'localize' => TRUE,
  );
  return $variables;
}

/**
 * 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'),
    '#maxlength' => 512,
    '#size' => 120,
    '#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);
}

Functions

Namesort descending Description
domain_registration_admin_form Administration form for domain registration.
domain_registration_form_user_register_form_alter Implements hook_form_form_id_form_alter().
domain_registration_menu Implements hook_menu().
domain_registration_permission Implements hook_permission().
domain_registration_user_register_validate Custom validation function.
domain_registration_variable_info Implements hook_variable_info().
domain_registration_wildcard_match Pattern Match wildcard addresses.

Constants

Namesort descending Description
DOMAIN_REGISTRATION_ALLOW @file Domain Registration module file.
DOMAIN_REGISTRATION_DENY