Jump to content

Topic on Project:Support desk/Flow

Allow account creation only from specific email domains.

6
MarkJurgens (talkcontribs)

I'd like to allow teachers from my school board to create an account on our wiki, but ONLY if they have an email addresses from a specific domain. (Ex. ___@schoolboard.com). The end result would be only teachers from specific school boards would be able to create accounts and edit.

An array to add multiple school board domains would be great.

Any ideas? Thanks!

MarkAHershberger (talkcontribs)

This is easy to do by using the AbortNewAccount hook. Something like the following (untested, but based on code I use):

function abortOnBadDomain($user, &$message) {
	global $wgRequest;
	$allowedDomains = array( "school.edu", "k12.us" );
	$email = $wgRequest->getText( 'wpEmail' );
	$emailSplitList = explode("@", $email, 2);

	if ( isset( $emailSplitList[1] ) ) {
		foreach( $allowedDomains as $domain ) {
			if ( $emailSplitList[1] === $domain ) ) {
				return true;
			}
		}
	}
	$message = "Domain blocked";

	return false;
}
$wgHooks['AbortNewAccount'][] = 'abortOnBadDomain';
MarkJurgens (talkcontribs)

Thanks Mark,

I may have sounded like a programmer in my initial request, and I've got a good handle on configuring Mediawiki, so excuse my ignorance:

Where do I put this code?

On another note, are there a way to find a coder(s) who might want to help me with an educational project? I can float server costs but developer might tank my non-existent budget. :)

Thanks again! P.s. Nice blog.

MarkAHershberger (talkcontribs)

You can put the code at the bottom of your LocalSettings.php file. It should work there. Let me know if you run into problems with it.

I'm not sure how to find a volunteer coder, but you might find someone who has some interest, or who can offer you pointers by writing about your project on mail:mediawiki-l.

Thanks!

FabienAndre (talkcontribs)

Due to the deprecation of AbortNewAccount hook, the previous the previous answer from @MarkAHershberger does not work anymore (so as the EmailDomainCheck extension).

Here is an updated version that you can include in LocalSettings.php (tested on 1.41)

class AllowedDomainPreAuthenticationProvider extends \MediaWiki\Auth\AbstractPreAuthenticationProvider {
    public function testForAccountCreation( $user, $creator, array $reqs ) {
        $allowedDomains = array( "school.edu", "k12.us" );
        $email = $user->mEmail;
        $emailSplitList = explode("@", $email, 2);

        if ( isset( $emailSplitList[1] ) ) {
            foreach( $allowedDomains as $domain ) {
                if ( $emailSplitList[1] === $domain ) {
                    return \StatusValue::newGood();
                }
            }
        }

        $message = "Blocked domain ($emailSplitList[1]). Allowed domains are : " . implode(', ', $allowedDomains);
        return \StatusValue::newFatal($message);
    }
}

$wgAuthManagerAutoConfig['preauth'] = [
    'AllowedDomainPreAuthenticationProvider' => [
        'class' => 'AllowedDomainPreAuthenticationProvider',
        'sort' => 1,
    ],
];
Reply to "Allow account creation only from specific email domains."