From 520d38cd452c52be8d8e7c4ba6792c66795ae73a Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Wed, 17 Mar 2010 11:34:17 -0300 Subject: [PATCH] Allowing same login/e-mail in independent environments --- app/controllers/public/account_controller.rb | 2 +- app/models/person.rb | 2 +- app/models/user.rb | 7 ++++--- test/fixtures/users.yml | 9 +++++++++ test/functional/account_controller_test.rb | 6 ++++++ test/unit/person_test.rb | 21 ++++++++++++++++----- test/unit/user_test.rb | 6 +++++- 7 files changed, 42 insertions(+), 11 deletions(-) diff --git a/app/controllers/public/account_controller.rb b/app/controllers/public/account_controller.rb index a2b5f9e..27fad63 100644 --- a/app/controllers/public/account_controller.rb +++ b/app/controllers/public/account_controller.rb @@ -20,7 +20,7 @@ class AccountController < ApplicationController @person = @user.build_person store_location(request.referer) unless session[:return_to] return unless request.post? - self.current_user = User.authenticate(params[:user][:login], params[:user][:password]) if params[:user] + self.current_user = User.authenticate(params[:user][:login], params[:user][:password], environment) if params[:user] if logged_in? if params[:remember_me] == "1" self.current_user.remember_me diff --git a/app/models/person.rb b/app/models/person.rb index e89005c..50ee6d6 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -157,7 +157,7 @@ class Person < Profile end validates_each :email, :on => :update do |record,attr,value| - if User.find(:first, :conditions => ['email = ? and id != ?', value, record.user.id]) + if User.find(:first, :conditions => ['email = ? and id != ? and environment_id = ?', value, record.user.id, record.environment.id]) record.errors.add(attr, _('%{fn} is already used by other user')) end end diff --git a/app/models/user.rb b/app/models/user.rb index 6d75ba9..aaff0fa 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -78,15 +78,16 @@ class User < ActiveRecord::Base validates_confirmation_of :password, :if => :password_required? validates_length_of :login, :within => 2..40, :if => (lambda {|user| !user.login.blank?}) validates_length_of :email, :within => 3..100, :if => (lambda {|user| !user.email.blank?}) - validates_uniqueness_of :login, :email, :case_sensitive => false + validates_uniqueness_of :login, :email, :case_sensitive => false, :scope => :environment_id before_save :encrypt_password validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?}) validates_inclusion_of :terms_accepted, :in => [ '1' ], :if => lambda { |u| ! u.terms_of_use.blank? }, :message => N_('%{fn} must be checked in order to signup.') # Authenticates a user by their login name and unencrypted password. Returns the user or nil. - def self.authenticate(login, password) - u = find_by_login(login) # need to get the salt + def self.authenticate(login, password, environment = nil) + environment ||= Environment.default + u = find_by_login_and_environment_id(login, environment.id) # need to get the salt u && u.authenticated?(password) ? u : nil end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 1cb6fce..deac2b6 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -26,3 +26,12 @@ ze: # activation_code: aaronscode # only if you're activating new signups created_at: <%= 1.days.ago.to_s :db %> environment_id: 1 +other_ze: + id: 4 + login: ze + email: ze@localhost.localdomain + salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd + crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test + # activation_code: aaronscode # only if you're activating new signups + created_at: <%= 1.days.ago.to_s :db %> + environment_id: 2 diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index 81a87fc..fe2ad18 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -42,6 +42,12 @@ class AccountControllerTest < Test::Unit::TestCase assert_redirected_to '/bli' end + should 'authenticate on the current environment' do + User.expects(:authenticate).with('fake', 'fake', is_a(Environment)) + @request.env["HTTP_REFERER"] = '/bli' + post :login, :user => { :login => 'fake', :password => 'fake' } + end + should 'redirect to where was when login on other environment' do e = Environment.create!(:name => 'other_environment') e.domains << Domain.new(:name => 'other.environment') diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 5571b3a..c9ca2fd 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -127,11 +127,22 @@ class PersonTest < Test::Unit::TestCase 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) + create_user('firstuser', :email => 'user@domain.com') + + other = create_user('seconduser', :email => 'other@domain.com').person + other.email = 'user@domain.com' + other.valid? + assert other.errors.invalid?(:email) + end + + should 'be able to use an e-mail already used in other environment' do + first = create_user('user', :email => 'user@example.com') + + other_env = fast_create(Environment) + other = create_user('user', :email => 'other@example.com', :environment => other_env).person + other.email = 'user@example.com' + other.valid? + assert !other.errors.invalid?(:email) end should 'be an admin if have permission of environment administration' do diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 911cb0b..d0af632 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -4,7 +4,7 @@ class UserTest < Test::Unit::TestCase # Be sure to include AuthenticatedTestHelper in test/test_helper.rb instead. # Then, you can remove it from this and the functional test. include AuthenticatedTestHelper - fixtures :users + fixtures :users, :environments def test_should_create_user assert_difference User, :count do @@ -55,6 +55,10 @@ class UserTest < Test::Unit::TestCase assert_equal users(:johndoe), User.authenticate('johndoe', 'test') end + def test_should_authenticate_user_of_nondefault_environment + assert_equal users(:other_ze), User.authenticate('ze', 'test', environments(:anhetegua_net)) + end + def test_should_set_remember_token users(:johndoe).remember_me assert_not_nil users(:johndoe).remember_token -- libgit2 0.21.2