Magento: Restrict user registration by email domain

One of my clients has put up a new Magento store which is restricted to only a few businesses they deal with. To come around the problem of only allowing employees of these businesses to register i came up with the following solution.

Only allow people who own an email with the business domain name to register.

Now what i am doing is a little hack to the core files which is highly not recomended so i suggest copying all these files i edit to the code/local file and edit them there so as to not modify core files.

firstly we want to have it so you can have a field in your magento system settings to put in the allowed email domains and change it or add to it at any time nice and easy.

so find the file:
Magento Folder: app/code/core/Mage/Customer/etc/system.xml

around line 195 look for: “</email_domain>”
and put in after that:

Now this part you don’t totally need. But because i have made this work with a maulti stores magento for my client and they wanted a different error message for each store, past this in after the above.

once done. save the file, refresh you magento cache, and in your Magento go to System > Configuration > Customer Configuration.
Under Create New Account Options you will see the 2 new fields we added.
For Allowed Signup Email Domains enter some domains you want to allow for signup. example: domain1.com, domain2.com
Enter an error message in the next field that the user will get if they try signing up with an email not from an allowed domain.

Now we need to get the code to check the email domain when someone registers on your magento store.

so find the file:
Magento Folder: app/code/core/Mage/Customer/Model/Customer.php

look for the line: “public function validate()”
somewhere around line 792.

this is where the email field from the signup form gets validated. If an error gets returned the signup will not go through.

so after the “Invalid email address” error i added.

What the above is doing is checking the email they entered against the allowed email domains you entered into you settings, if it finds the domain in the list it will allow the validation to continue. If it does not find it in the list it will give an error ( either an error message you put in the setting, or a default one ).

I am also going to post up soon a way to restrict your magento store to only logged in users. So if a user is not logged in they will be redirected to the login / register page. This can work well with the above code to restrict your store.