Extension:EmailDomainCheck

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
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 GNU General Public License 2.0 or later
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.

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]

  • 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
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Configuration[edit]

$wgEmailDomain - the email domain that is allowed to register

Code[edit]

EmailDomainCheck.php
<?php
/**
 * Extension checks that registering users
 * are from a specific email domain
 * during account creation.
 *
 * @package MediaWiki
 * @subpackage Extensions
 * @author wookienz <wookienz@gmail.com>
 */

/**
 * 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 = wfMessage( 'emaildomaincheck-error' )->rawParams( $wgEmailDomain )->escaped();
                        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.',
);

/** Dutch (Nederlands)
 * @author Ralpha9
 */
$messages['nl'] = array(
        'emaildomaincheck-desc'  => 'Limiteer een specifiek email domain tijdens registratie',
        'emaildomaincheck-error' => 'Uw email domain is niet geldig. Uw email address moet eindigen in $1.',
);