We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19328
    • 433 Posts
    I want to compare a users email address to a long list (about 1000) email addresses and only allow registration if the email address exists in the list. What would be a good and easy to maintain way to do this?

    1) create a text file with the list, each address on a new line, and create a preHook for the Register snippet to read the text file line by line until the address is found, and return true or false - would this be too slow because of the 1000 lines in the text file?
    2) create a chunk that contains all the email addresses and check that contents with a preHook - would this be faster than the first option? Would it be too big for a chunk?
    3) is there a better way?

    It would be most useful if all addresses can be pasted, one address per line in a chunk or file, this is easiest for my client.

    OR a different approach: somehow automate the creation of users for all the 1000 email addresses and email a password to each of them. Is there a snippet for that?

    Any thoughts and ideas would be appreciated!

    This question has been answered by multiple community members. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      Probably the easiest thing for the client would be to list the names in a chunk called whiteList.

      I think this would be fast enough, but I'm not sure the email will be available in a preHook. I think Register runs its preHooks after validation, but before anything else, so it might work:

      $email = $hook->getValue('email');
      $whiteList = array_flip(array_map('trim', explode("\n", $modx->getChunk('whiteList'))));
      return isset($whiteList[$email]);
      
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
      • discuss.answer
        • 19328
        • 433 Posts
        Thanks BobRay, that works like a charm!

        This is my final code (with an error message):

        <?php
        $email = $hook->getValue('email');
        $whiteList = array_flip(array_map('trim', explode("\n", $modx->getChunk('whiteList'))));
        
        if ( isset($whiteList[$email]) ) {
        	return true;
        } else {
        	$errorMsg = 'Sorry, you cannot register because this email address is not whitelisted.';
        	$hook->addError('email',$errorMsg);
        	return false;
        }
          • 3749
          • 24,544 Posts
          I'm glad it works. smiley

          Nice job of putting the logic for failed searches into the snippet.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 3749
            • 24,544 Posts
            On further thought, this might be a little faster, since you're only testing a single value. Using array_flip is faster if you're going to be looking for multiple values. The time difference would probably be negligible unless you have a very long whitelist.


            $whiteList = array_map('trim', explode("\n", $modx->getChunk('whiteList')));
            
            if (in_array($email, $whiteList)) {
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 19328
              • 433 Posts
              Thanks! What would be 'very long'? (this one could potentially have a 1000 email addresses in it.. is that very long?)
                • 3749
                • 24,544 Posts
                Good question, I don't really know. I'd say if things slow down enough to notice when the list gets longer, try it the other way and see if it speeds things up. wink

                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting