From c281083a72661e746eac9844070f5e537a0df785 Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Tue, 27 Jan 2009 17:10:12 -0300 Subject: [PATCH] ActionItem865: adding duplicated e-mail validation in the person model --- app/models/person.rb | 6 ++++++ test/unit/person_test.rb | 8 ++++++++ 2 files changed, 14 insertions(+), 0 deletions(-) diff --git a/app/models/person.rb b/app/models/person.rb index 9c3890c..bfc8cba 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -141,6 +141,12 @@ class Person < Profile self.user.nil? ? nil : self.user.email end + validates_each :email, :on => :update do |record,attr,value| + if User.find(:first, :conditions => ['email = ? and id != ?', value, record.user.id]) + record.errors.add(attr, _('%{fn} is already used by other user')) + end + end + # Returns the user e-mail. def contact_email email diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index fe1f5ac..6c434f6 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -129,6 +129,14 @@ class PersonTest < Test::Unit::TestCase assert_equal 'damnit@example.com', u.email end + should 'not be able to change e-mail to an e-mail of other user' do + first = create_user('firstuser', :email => 'user@domain.com') + second = create_user('seconduser', :email => 'other@domain.com') + second.email = 'user@domain.com' + second.valid? + assert second.errors.invalid?(:email) + end + should 'be an admin if have permission of environment administration' do role = Role.create!(:name => 'just_another_admin_role') env = Environment.create!(:name => 'blah') -- libgit2 0.21.2