summaryrefslogtreecommitdiff
path: root/users/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'users/app/models')
-rw-r--r--users/app/models/local_email.rb19
-rw-r--r--users/app/models/user.rb18
2 files changed, 34 insertions, 3 deletions
diff --git a/users/app/models/local_email.rb b/users/app/models/local_email.rb
index c654fcb..d23df71 100644
--- a/users/app/models/local_email.rb
+++ b/users/app/models/local_email.rb
@@ -3,15 +3,22 @@ class LocalEmail < Email
validate :unique_on_server
validate :unique_alias_for_user
validate :differs_from_main_email
+ before_validation :add_domain_if_needed
+ validates :email,
+ :presence => true,
+ :format => { :with => /@#{APP_CONFIG[:domain]}\Z/,
+ :message => "needs to end in @#{APP_CONFIG[:domain]}"}
validates :casted_by, :presence => true
def to_partial_path
"emails/email"
end
+ protected
+
def unique_on_server
- has_email = User.find_by_email_or_alias(email)
- if has_email && has_email != self.base_doc
+ has_email = User.find_by_email_or_alias(email)
+ if has_email && has_email != self.base_doc
errors.add :email, "has already been taken"
end
end
@@ -24,11 +31,19 @@ class LocalEmail < Email
end
def differs_from_main_email
+ # If this has not changed but the email let's mark the email invalid instead.
+ return if self.persisted?
user = self.casted_by
if user.email == self.email
errors.add :email, "may not be the same as your email address"
end
end
+ def add_domain_if_needed
+ if email.blank?
+ errors.add :email, "may not be empty."
+ end
+ self.email += "@" + APP_CONFIG[:domain] unless self.email.include?("@")
+ end
end
diff --git a/users/app/models/user.rb b/users/app/models/user.rb
index 1999a28..2a8a57b 100644
--- a/users/app/models/user.rb
+++ b/users/app/models/user.rb
@@ -32,6 +32,12 @@ class User < CouchRest::Model::Base
validates :email,
:format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"}
+ validates :email,
+ :format => { :with => /\A(.+@#{APP_CONFIG[:domain]})?\Z/,
+ :message => "needs to end in @#{APP_CONFIG[:domain]}"}
+
+ validate :email_unique_on_server
+
validates :email_forward,
:format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"}
@@ -121,18 +127,28 @@ class User < CouchRest::Model::Base
email_aliases.build(attrs.values.first) if attrs
end
+ protected
+
##
# Validation Functions
##
def email_differs_from_email_aliases
+ # If this has not changed but the email aliases let's not mark this invalid.
return if email_aliases.any? and email_aliases.last.errors.any?
if email_aliases.map(&:email).include?(email)
errors.add(:email, "may not be the same as an alias")
end
end
- protected
+ def email_unique_on_server
+ return unless email
+ has_email = User.find_by_email_or_alias(email)
+ if has_email && has_email != self.base_doc
+ errors.add :email, "has already been taken"
+ end
+ end
+
def password
password_verifier
end