-2

Having trouble with users registering using our own company email address.

Example: I work at twitter. Users trying to use: user@twitter.com as their email address.

What would I add to the user model to block this from happening?

  • 2
    Have a blacklist of domains and check for it? – Dave Newton Aug 04 '15 at 21:07
  • Should have clarified. Need people within the company to still be able to sign up using test accounts so blacklisting domains wouldn't work. Thanks though – Luke Duncan Aug 04 '15 at 21:09
  • If I were you, I would add warning message on the registration form =) The problem is that it's hard to detect with 100% accuracy that the email is corporative. Yes, you can hardcode a lot of email providers like Gmail, Yahoo. But there are really A LOT of them. – 8f7ca04d817b1696 Aug 04 '15 at 21:10
  • If you need employees to still create accounts with that email, then you will need some sort of flag as well to denote who's an employee and who's not. – jeremywoertink Aug 04 '15 at 21:11
  • @LukeDuncan ... So, how do you determine which is a test account and which isn't? There's your regex + domain. – Dave Newton Aug 04 '15 at 21:13
  • possible duplicate of [How to add a User model validation to block certain email domains](http://stackoverflow.com/questions/9708280/how-to-add-a-user-model-validation-to-block-certain-email-domains) – Brad Werth Aug 04 '15 at 21:21

1 Answers1

0

Try using a custom validation. Here is a super slimmed down example. You may want a bit more power, but this should get you started.

class User < ActiveRecord::Base
  validates :email, blacklist: true
end

class BlacklistValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    record.errors.add attribute, (options[:message] || "is not a valid email") if
  value =~ /twitter\.com/
  end
end
jeremywoertink
  • 2,281
  • 1
  • 23
  • 29