Commit 520d38cd452c52be8d8e7c4ba6792c66795ae73a

Authored by Antonio Terceiro
1 parent 8fc8a20b

Allowing same login/e-mail in independent environments

(ActionItem1448)
app/controllers/public/account_controller.rb
... ... @@ -20,7 +20,7 @@ class AccountController < ApplicationController
20 20 @person = @user.build_person
21 21 store_location(request.referer) unless session[:return_to]
22 22 return unless request.post?
23   - self.current_user = User.authenticate(params[:user][:login], params[:user][:password]) if params[:user]
  23 + self.current_user = User.authenticate(params[:user][:login], params[:user][:password], environment) if params[:user]
24 24 if logged_in?
25 25 if params[:remember_me] == "1"
26 26 self.current_user.remember_me
... ...
app/models/person.rb
... ... @@ -157,7 +157,7 @@ class Person < Profile
157 157 end
158 158  
159 159 validates_each :email, :on => :update do |record,attr,value|
160   - if User.find(:first, :conditions => ['email = ? and id != ?', value, record.user.id])
  160 + if User.find(:first, :conditions => ['email = ? and id != ? and environment_id = ?', value, record.user.id, record.environment.id])
161 161 record.errors.add(attr, _('%{fn} is already used by other user'))
162 162 end
163 163 end
... ...
app/models/user.rb
... ... @@ -78,15 +78,16 @@ class User < ActiveRecord::Base
78 78 validates_confirmation_of :password, :if => :password_required?
79 79 validates_length_of :login, :within => 2..40, :if => (lambda {|user| !user.login.blank?})
80 80 validates_length_of :email, :within => 3..100, :if => (lambda {|user| !user.email.blank?})
81   - validates_uniqueness_of :login, :email, :case_sensitive => false
  81 + validates_uniqueness_of :login, :email, :case_sensitive => false, :scope => :environment_id
82 82 before_save :encrypt_password
83 83 validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?})
84 84  
85 85 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.')
86 86  
87 87 # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
88   - def self.authenticate(login, password)
89   - u = find_by_login(login) # need to get the salt
  88 + def self.authenticate(login, password, environment = nil)
  89 + environment ||= Environment.default
  90 + u = find_by_login_and_environment_id(login, environment.id) # need to get the salt
90 91 u && u.authenticated?(password) ? u : nil
91 92 end
92 93  
... ...
test/fixtures/users.yml
... ... @@ -26,3 +26,12 @@ ze:
26 26 # activation_code: aaronscode # only if you're activating new signups
27 27 created_at: <%= 1.days.ago.to_s :db %>
28 28 environment_id: 1
  29 +other_ze:
  30 + id: 4
  31 + login: ze
  32 + email: ze@localhost.localdomain
  33 + salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd
  34 + crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test
  35 + # activation_code: aaronscode # only if you're activating new signups
  36 + created_at: <%= 1.days.ago.to_s :db %>
  37 + environment_id: 2
... ...
test/functional/account_controller_test.rb
... ... @@ -42,6 +42,12 @@ class AccountControllerTest &lt; Test::Unit::TestCase
42 42 assert_redirected_to '/bli'
43 43 end
44 44  
  45 + should 'authenticate on the current environment' do
  46 + User.expects(:authenticate).with('fake', 'fake', is_a(Environment))
  47 + @request.env["HTTP_REFERER"] = '/bli'
  48 + post :login, :user => { :login => 'fake', :password => 'fake' }
  49 + end
  50 +
45 51 should 'redirect to where was when login on other environment' do
46 52 e = Environment.create!(:name => 'other_environment')
47 53 e.domains << Domain.new(:name => 'other.environment')
... ...
test/unit/person_test.rb
... ... @@ -127,11 +127,22 @@ class PersonTest &lt; Test::Unit::TestCase
127 127 end
128 128  
129 129 should 'not be able to change e-mail to an e-mail of other user' do
130   - first = create_user('firstuser', :email => 'user@domain.com')
131   - second = create_user('seconduser', :email => 'other@domain.com')
132   - second.email = 'user@domain.com'
133   - second.valid?
134   - assert second.errors.invalid?(:email)
  130 + create_user('firstuser', :email => 'user@domain.com')
  131 +
  132 + other = create_user('seconduser', :email => 'other@domain.com').person
  133 + other.email = 'user@domain.com'
  134 + other.valid?
  135 + assert other.errors.invalid?(:email)
  136 + end
  137 +
  138 + should 'be able to use an e-mail already used in other environment' do
  139 + first = create_user('user', :email => 'user@example.com')
  140 +
  141 + other_env = fast_create(Environment)
  142 + other = create_user('user', :email => 'other@example.com', :environment => other_env).person
  143 + other.email = 'user@example.com'
  144 + other.valid?
  145 + assert !other.errors.invalid?(:email)
135 146 end
136 147  
137 148 should 'be an admin if have permission of environment administration' do
... ...
test/unit/user_test.rb
... ... @@ -4,7 +4,7 @@ class UserTest &lt; Test::Unit::TestCase
4 4 # Be sure to include AuthenticatedTestHelper in test/test_helper.rb instead.
5 5 # Then, you can remove it from this and the functional test.
6 6 include AuthenticatedTestHelper
7   - fixtures :users
  7 + fixtures :users, :environments
8 8  
9 9 def test_should_create_user
10 10 assert_difference User, :count do
... ... @@ -55,6 +55,10 @@ class UserTest &lt; Test::Unit::TestCase
55 55 assert_equal users(:johndoe), User.authenticate('johndoe', 'test')
56 56 end
57 57  
  58 + def test_should_authenticate_user_of_nondefault_environment
  59 + assert_equal users(:other_ze), User.authenticate('ze', 'test', environments(:anhetegua_net))
  60 + end
  61 +
58 62 def test_should_set_remember_token
59 63 users(:johndoe).remember_me
60 64 assert_not_nil users(:johndoe).remember_token
... ...