Loading

Author Topic: restrict registration to specific email addresses  (Read 179 times)

nic

  • Newbie
  • *
  • Posts: 2
restrict registration to specific email addresses
« on: February 21, 2012, 01:16:31 am »
Hi,
Just starting using osclass and I'm liking it so far - I'm hoping to be able to migrate from another classifieds system that has given me ridiculous amounts of grief over the last couple of years.

I need to be able to restrict registration to only allow users from a particular domain to register.  Anyone needs to be able to respond to ads, but only users from universityname.ac.uk can register and post ads.

On my existing site, I've written the following regex on the registration form.
ereg("^[[:alnum:]]([-_.]?[[:alnum:]])+_?@([[:alnum:]]*\.)?universityname\.ac\.uk$",$email )

I'm trying to work out how I would achieve the same end result here.  Can anyone help?

Cheers,

N.

_CONEJO

  • Administrator
  • Hero Member
  • *****
  • Posts: 1922
Re: restrict registration to specific email addresses
« Reply #1 on: February 21, 2012, 11:51:23 am »
Hi nic,

I think the easiest way is to create a plugin. I did NOT test this, but it "should" work

Create a folder in oc-content/plugins called "myemail", inside it, create a file called index.php and copy this into it
Code: [Select]
<?php
/*
Plugin Name: Custom email
Plugin URI: http://www.osclass.org/
Description: -
Version: 0.1
Author: OSClass
Author URI: http://www.osclass.org/
Short Name: myemail
*/


    
function myemail_form() {
        include_once 
'form.php';
    }


    function 
myemail_before_register() {
        
$email Params::getParam('s_email');
        if(!
preg_match("|^[[:alnum:]]([-_.]?[[:alnum:]])+_?@([[:alnum:]]*\.)?universityname\.ac\.uk$|",$email$m )) {
            
osc_add_flash_error_message__('Your email is not valid''myemail')) ;
            
header('Location: ' osc_register_account_url());
            exit;
        }
    }
    

    
/**
     * ADD HOOKS
     */
    
osc_register_plugin(osc_plugin_path(__FILE__), '');
    
osc_add_hook(osc_plugin_path(__FILE__)."_uninstall"'');

    
// run at registration form
    
osc_add_hook('user_register_form''myemail_form');
    
    
osc_add_hook('before_user_register''myemail_before_register');

    
?>


Also, create a file called, "form.php" and write something "similar" to this (I'm not sure about this part) This code is using JQuery validation plugin ( http://bassistance.de/jquery-plugins/jquery-plugin-validation/ ) You HAVE TO find a proper code to validate only emails from the domain.

Code: [Select]
<script>
    $(document).ready(function(){
        $("#s_email").rules("add", {required: true, messages: { required: "<?php _e('Email not valid''myemail'); ?>" }});
    });
</script>


EXPLANATION:
We're gonna create a plugin (myemail). The plugin will add a PHP rule (I changed from ereg which is deprecated to preg_match, which is also faster function http://php.net/manual/es/function.ereg.php , but the syntax is almost the same). That check is ok, but that will happen only AFTER the form is submitted. We want to prevent also that happening and display a JS error BEFORE (form.php file) since we're already using jquery validation plugin, best way is to add a new rule to "s_email" field. You should modify that rule to perform the check you want to.  I'm not sure if this will work of not, since we already had added rules to that field before, anyway, test it.


Of course, it will be easier to just add the regexp on the core, but, with a plugin you could safely update OSClass when needed.


Thanks




nic

  • Newbie
  • *
  • Posts: 2
Re: restrict registration to specific email addresses
« Reply #2 on: February 23, 2012, 12:54:23 am »
Huge thanks,
I really appreciate you taking the time to provide a clear, well explained answer.

Predictably, the php rule works like a charm.  Unfortunately I'm struggling to get the jquery validator to play - but this could well be user error on my part rather than it not being possible...

But its been a long day and I'm rather tired...  I'll have another blast tomorrow.