From 6ff4bd80e0394260c8cd300cfb051451fc7e358f Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 13 Dec 2012 15:47:08 +0100 Subject: ensure users do not have duplicate email aliases nor aliases that are the same as the original email for that matter --- users/app/models/user.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'users/app') diff --git a/users/app/models/user.rb b/users/app/models/user.rb index e5e388b..0eb244c 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -33,6 +33,10 @@ class User < CouchRest::Model::Base validates :email_forward, :format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"} + validate :no_duplicate_email_aliases + + validate :email_aliases_differ_from_email + timestamps! design do @@ -115,6 +119,23 @@ class User < CouchRest::Model::Base end end + ## + # Validation Functions + ## + + # TODO: How do we handle these errors? + def no_duplicate_email_aliases + if email_aliases.count != email_aliases.map(&:email).uniq.count + errors.add(:email_aliases, "include a duplicate") + end + end + + def email_aliases_differ_from_email + if email_aliases.map(&:email).include?(email) + errors.add(:email_aliases, "include the original email address") + end + end + protected def password password_verifier -- cgit v1.2.3