Extension:EmailDomainCheck

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png
EmailDomainCheck

Release status: beta

Implementation access (invalid type)
Description Allows only users with a specific e-mail-domain to register
Author(s) wookienztalk
Latest version 0.3.1 (2013-10-17)
Database changes No
License GPLv2+
Download See the code section
Parameters
  • $wgEmailDomain
Hooks used
AbortNewAccount

Translate the EmailDomainCheck extension if it is available at translatewiki.net

Check usage and version matrix; code metrics

The EmailDomainCheck extension allows only users with a specific e-mail-domain to register. It is especially handy if you only allow access once the email has been verified. Therefore you can effectively limit access to know companies as they need to have access to the email address to verify themselves.

Installation[edit | edit source]

  • Copy the code into files and extract the tarball in your extensions/ folder. It should generate a new folder called EmailDomainCheck directly inside your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
require_once "$IP/extensions/EmailDomainCheck/EmailDomainCheck.php";
$wgEmailDomain = 'somedomain.org'; // set "somedomain.org" to a domain as required
  • Done! Navigate to "Special:Version" on your wiki to verify that the extension is successfully installed.

Configuration[edit | edit source]

$wgEmailDomain - the email domain that is allowed to register

Code[edit | edit source]

EmailDomainCheck.php
<?php
/**
 * Extension checks that registering users
 * are from a specific email domain
 * during account creation.
 *
 * @package MediaWiki
 * @subpackage Extensions
 * @author wookienz <[email protected]>
 */
 
/**
 * email domain that user must come from
 */
 
// Check if we are being called directly
if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is an extension to MediaWiki and thus not a valid entry point.' );
}
 
// Tell the whereabouts of the other files
$wgExtensionMessagesFiles['EmailDomainCheck'] = dirname(__FILE__) . '/EmailDomainCheck.i18n.php';
 
// Extension credits that will show up on Special:Version
$wgExtensionCredits['other'][] = array(
       'path' => __FILE__,
       'name' => 'Email Domain Check',
       'author' => 'Wookienz',
       'version' => '0.3.1',
       'url' => 'https://www.mediawiki.org/wiki/Extension:EmailDomainCheck',
       'descriptionmsg' => 'emaildomaincheck-desc',
);
 
// Register hooks
$wgHooks['AbortNewAccount'][] = 'efEmailDomainCheck';
 
/**
 * Hooks the account creation process,
 * will cancel the prcoess if the dmain is not correct.
 *
 * @param User $user User object being created
 * @param string $error Reference to the error message to show
 * @return bool
 */
 
// Do the extension's job
/* From http://stackoverflow.com/questions/834303/php-startswith-and-endswith-functions */
function efEndsWith($haystack, $needle)
{
    return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}
 
function efEmailDomainCheck( $user, &$error ) {
        global $wgEmailDomain;
 
        if ( isset( $wgEmailDomain ) ) {
 
                list( $name, $host ) = explode( "@", $user->getEmail() );
                //if ( efEndsWith( $host, $wgEmailDomain ) ) { // use this line to allow subdomains of $wgEmailDomain
                if ( $host == $wgEmailDomain ) {
                        return true;
                } else {
                        $error = wfMsgHtml( 'emaildomaincheck-error', $wgEmailDomain );
                        return false;
                }
        }
}
EmailDomainCheck.i18n.php
<?php
/**
 * Internationalisation file for extension EmailDomainCheck
 *
 * @addtogroup Extensions
 */
 
$messages = array();
 
/** English
 * @author Wookienz
 */
$messages['en'] = array(
        'emaildomaincheck-desc'  => 'Enforces a specific email domain during registration',
        'emaildomaincheck-error' => 'Your email domain is invalid. Your email address must end in $1.',
);
 
/** German (Deutsch)
 * @author SVG
 * @author kghbln
 */
$messages['de'] = array(
        'emaildomaincheck-desc'  => 'Erzwingt bei der Registrierung eine bestimmte E-Mail-Domain',
        'emaildomaincheck-error' => 'Die Domain deiner E-Mail-Adresse ist ungültig. Deine E-Mail-Adresse muss mit $1 enden.',
);
 
/** German (formal address) (Deutsch (Sie-Form))
 * @author SVG
 * @author kghbln
 */
$messages['de-formal'] = array(
        'emaildomaincheck-error' => 'Die Domain Ihrer E-Mail-Adresse ist ungültig. Ihre E-Mail-Adresse muss mit $1 enden.',
);