Commit c281083a72661e746eac9844070f5e537a0df785
1 parent
e59874da
Exists in
master
and in
29 other branches
ActionItem865: adding duplicated e-mail validation in the person model
Showing
2 changed files
with
14 additions
and
0 deletions
Show diff stats
app/models/person.rb
@@ -141,6 +141,12 @@ class Person < Profile | @@ -141,6 +141,12 @@ class Person < Profile | ||
141 | self.user.nil? ? nil : self.user.email | 141 | self.user.nil? ? nil : self.user.email |
142 | end | 142 | end |
143 | 143 | ||
144 | + validates_each :email, :on => :update do |record,attr,value| | ||
145 | + if User.find(:first, :conditions => ['email = ? and id != ?', value, record.user.id]) | ||
146 | + record.errors.add(attr, _('%{fn} is already used by other user')) | ||
147 | + end | ||
148 | + end | ||
149 | + | ||
144 | # Returns the user e-mail. | 150 | # Returns the user e-mail. |
145 | def contact_email | 151 | def contact_email |
146 | 152 |
test/unit/person_test.rb
@@ -129,6 +129,14 @@ class PersonTest < Test::Unit::TestCase | @@ -129,6 +129,14 @@ class PersonTest < Test::Unit::TestCase | ||
129 | assert_equal 'damnit@example.com', u.email | 129 | assert_equal 'damnit@example.com', u.email |
130 | end | 130 | end |
131 | 131 | ||
132 | + should 'not be able to change e-mail to an e-mail of other user' do | ||
133 | + first = create_user('firstuser', :email => 'user@domain.com') | ||
134 | + second = create_user('seconduser', :email => 'other@domain.com') | ||
135 | + second.email = 'user@domain.com' | ||
136 | + second.valid? | ||
137 | + assert second.errors.invalid?(:email) | ||
138 | + end | ||
139 | + | ||
132 | should 'be an admin if have permission of environment administration' do | 140 | should 'be an admin if have permission of environment administration' do |
133 | role = Role.create!(:name => 'just_another_admin_role') | 141 | role = Role.create!(:name => 'just_another_admin_role') |
134 | env = Environment.create!(:name => 'blah') | 142 | env = Environment.create!(:name => 'blah') |