Commit 520d38cd452c52be8d8e7c4ba6792c66795ae73a
1 parent
8fc8a20b
Exists in
master
and in
29 other branches
Allowing same login/e-mail in independent environments
(ActionItem1448)
Showing
7 changed files
with
42 additions
and
11 deletions
Show diff stats
app/controllers/public/account_controller.rb
@@ -20,7 +20,7 @@ class AccountController < ApplicationController | @@ -20,7 +20,7 @@ class AccountController < ApplicationController | ||
20 | @person = @user.build_person | 20 | @person = @user.build_person |
21 | store_location(request.referer) unless session[:return_to] | 21 | store_location(request.referer) unless session[:return_to] |
22 | return unless request.post? | 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 | if logged_in? | 24 | if logged_in? |
25 | if params[:remember_me] == "1" | 25 | if params[:remember_me] == "1" |
26 | self.current_user.remember_me | 26 | self.current_user.remember_me |
app/models/person.rb
@@ -157,7 +157,7 @@ class Person < Profile | @@ -157,7 +157,7 @@ class Person < Profile | ||
157 | end | 157 | end |
158 | 158 | ||
159 | validates_each :email, :on => :update do |record,attr,value| | 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 | record.errors.add(attr, _('%{fn} is already used by other user')) | 161 | record.errors.add(attr, _('%{fn} is already used by other user')) |
162 | end | 162 | end |
163 | end | 163 | end |
app/models/user.rb
@@ -78,15 +78,16 @@ class User < ActiveRecord::Base | @@ -78,15 +78,16 @@ class User < ActiveRecord::Base | ||
78 | validates_confirmation_of :password, :if => :password_required? | 78 | validates_confirmation_of :password, :if => :password_required? |
79 | validates_length_of :login, :within => 2..40, :if => (lambda {|user| !user.login.blank?}) | 79 | validates_length_of :login, :within => 2..40, :if => (lambda {|user| !user.login.blank?}) |
80 | validates_length_of :email, :within => 3..100, :if => (lambda {|user| !user.email.blank?}) | 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 | before_save :encrypt_password | 82 | before_save :encrypt_password |
83 | validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?}) | 83 | validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?}) |
84 | 84 | ||
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.') | 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 | # Authenticates a user by their login name and unencrypted password. Returns the user or nil. | 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 | u && u.authenticated?(password) ? u : nil | 91 | u && u.authenticated?(password) ? u : nil |
91 | end | 92 | end |
92 | 93 |
test/fixtures/users.yml
@@ -26,3 +26,12 @@ ze: | @@ -26,3 +26,12 @@ ze: | ||
26 | # activation_code: aaronscode # only if you're activating new signups | 26 | # activation_code: aaronscode # only if you're activating new signups |
27 | created_at: <%= 1.days.ago.to_s :db %> | 27 | created_at: <%= 1.days.ago.to_s :db %> |
28 | environment_id: 1 | 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 < Test::Unit::TestCase | @@ -42,6 +42,12 @@ class AccountControllerTest < Test::Unit::TestCase | ||
42 | assert_redirected_to '/bli' | 42 | assert_redirected_to '/bli' |
43 | end | 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 | should 'redirect to where was when login on other environment' do | 51 | should 'redirect to where was when login on other environment' do |
46 | e = Environment.create!(:name => 'other_environment') | 52 | e = Environment.create!(:name => 'other_environment') |
47 | e.domains << Domain.new(:name => 'other.environment') | 53 | e.domains << Domain.new(:name => 'other.environment') |
test/unit/person_test.rb
@@ -127,11 +127,22 @@ class PersonTest < Test::Unit::TestCase | @@ -127,11 +127,22 @@ class PersonTest < Test::Unit::TestCase | ||
127 | end | 127 | end |
128 | 128 | ||
129 | should 'not be able to change e-mail to an e-mail of other user' do | 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 | end | 146 | end |
136 | 147 | ||
137 | should 'be an admin if have permission of environment administration' do | 148 | should 'be an admin if have permission of environment administration' do |
test/unit/user_test.rb
@@ -4,7 +4,7 @@ class UserTest < Test::Unit::TestCase | @@ -4,7 +4,7 @@ class UserTest < Test::Unit::TestCase | ||
4 | # Be sure to include AuthenticatedTestHelper in test/test_helper.rb instead. | 4 | # Be sure to include AuthenticatedTestHelper in test/test_helper.rb instead. |
5 | # Then, you can remove it from this and the functional test. | 5 | # Then, you can remove it from this and the functional test. |
6 | include AuthenticatedTestHelper | 6 | include AuthenticatedTestHelper |
7 | - fixtures :users | 7 | + fixtures :users, :environments |
8 | 8 | ||
9 | def test_should_create_user | 9 | def test_should_create_user |
10 | assert_difference User, :count do | 10 | assert_difference User, :count do |
@@ -55,6 +55,10 @@ class UserTest < Test::Unit::TestCase | @@ -55,6 +55,10 @@ class UserTest < Test::Unit::TestCase | ||
55 | assert_equal users(:johndoe), User.authenticate('johndoe', 'test') | 55 | assert_equal users(:johndoe), User.authenticate('johndoe', 'test') |
56 | end | 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 | def test_should_set_remember_token | 62 | def test_should_set_remember_token |
59 | users(:johndoe).remember_me | 63 | users(:johndoe).remember_me |
60 | assert_not_nil users(:johndoe).remember_token | 64 | assert_not_nil users(:johndoe).remember_token |