summaryrefslogtreecommitdiff
path: root/users
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2013-07-18 17:17:36 +0200
committerAzul <azul@leap.se>2013-07-24 10:55:51 +0200
commit0acace58c31c96fc1f8836167aeb4f204f72617f (patch)
tree4738cf2fab5f67afdcdae7373914b9bd66cfd95e /users
parentd96fac2de074bbe3a44d888af5ceaff45b1b9b27 (diff)
allow available and unique forwards only
Diffstat (limited to 'users')
-rw-r--r--users/app/models/identity.rb22
-rw-r--r--users/app/models/local_email.rb30
-rw-r--r--users/app/models/user.rb8
-rw-r--r--users/test/unit/identity_test.rb18
4 files changed, 43 insertions, 35 deletions
diff --git a/users/app/models/identity.rb b/users/app/models/identity.rb
index c9c8b73..4dff93a 100644
--- a/users/app/models/identity.rb
+++ b/users/app/models/identity.rb
@@ -7,7 +7,29 @@ class Identity < CouchRest::Model::Base
property :address, LocalEmail
property :destination, Email
+ validate :unique_forward
+ validate :alias_available
+
design do
view :by_user_id
+ view :by_address_and_destination
+ view :by_address
+ end
+
+ protected
+
+ def unique_forward
+ same = Identity.find_by_address_and_destination([address, destination])
+ if same && same != self
+ errors.add :base, "This alias already exists"
+ end
end
+
+ def alias_available
+ same = Identity.find_by_address(address)
+ if same && same.user != self.user
+ errors.add :base, "This email has already been taken"
+ end
+ end
+
end
diff --git a/users/app/models/local_email.rb b/users/app/models/local_email.rb
index e71d494..c1f7c11 100644
--- a/users/app/models/local_email.rb
+++ b/users/app/models/local_email.rb
@@ -1,10 +1,5 @@
class LocalEmail < Email
-=begin
- validate :unique_on_server
- validate :unique_alias_for_user
- validate :differs_from_login
-=end
def self.domain
APP_CONFIG[:domain]
@@ -16,8 +11,6 @@ class LocalEmail < Email
:message => "needs to end in @#{domain}"
}
-
-
def initialize(s)
super
append_domain_if_needed
@@ -37,29 +30,6 @@ class LocalEmail < Email
protected
- def unique_on_server
- has_email = User.find_by_login_or_alias(username)
- if has_email && has_email != self.casted_by
- errors.add :username, "has already been taken"
- end
- end
-
- def unique_alias_for_user
- aliases = self.casted_by.email_aliases
- if aliases.select{|a|a.username == self.username}.count > 1
- errors.add :username, "is already your alias"
- end
- end
-
- def differs_from_login
- # 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.login == self.username
- errors.add :username, "may not be the same as your email address"
- end
- end
-
def append_domain_if_needed
unless self.index('@')
self << '@' + domain
diff --git a/users/app/models/user.rb b/users/app/models/user.rb
index dda5a41..5707f24 100644
--- a/users/app/models/user.rb
+++ b/users/app/models/user.rb
@@ -74,7 +74,7 @@ class User < CouchRest::Model::Base
end
def create_identity(attribs = {}, &block)
- build_identity(attribs, &block)
+ identity = build_identity(attribs, &block)
identity.save
identity
end
@@ -135,12 +135,10 @@ class User < CouchRest::Model::Base
##
def login_is_unique_alias
- has_alias = User.find_by_login_or_alias(username)
+ alias_identity = Identity.find_by_address(self.email_address)
return if has_alias.nil?
- if has_alias != self
+ if alias_identity.user != self
errors.add(:login, "has already been taken")
- elsif has_alias.login != self.login
- errors.add(:login, "may not be the same as one of your aliases")
end
end
diff --git a/users/test/unit/identity_test.rb b/users/test/unit/identity_test.rb
index a5d30b0..4ebd72e 100644
--- a/users/test/unit/identity_test.rb
+++ b/users/test/unit/identity_test.rb
@@ -39,6 +39,24 @@ class IdentityTest < ActiveSupport::TestCase
id.save
end
+ test "prevents duplicates" do
+ id = @user.create_identity address: alias_name, destination: forward_address
+ dup = @user.build_identity address: alias_name, destination: forward_address
+ assert !dup.valid?
+ assert_equal ["This alias already exists"], dup.errors[:base]
+ end
+
+ test "validates availability" do
+ other_user = FactoryGirl.create(:user)
+ id = @user.create_identity address: alias_name, destination: forward_address
+ taken = other_user.build_identity address: alias_name
+ assert !taken.valid?
+ assert_equal ["This email has already been taken"], taken.errors[:base]
+ other_user.destroy
+ end
+
+
+
def alias_name
@alias_name ||= Faker::Internet.user_name
end