Commit 79d84c0139203f8c1107f83e38bb14eeee37539c
1 parent
d34432ad
Exists in
web_steps_improvements
and in
11 other branches
Setup multi-tenancy before loading the user's session
These changes avoid searching for sessions with a wrong schema in a multi domain setup.
Showing
5 changed files
with
55 additions
and
29 deletions
Show diff stats
app/controllers/application_controller.rb
| @@ -3,7 +3,6 @@ require 'noosfero/multi_tenancy' | @@ -3,7 +3,6 @@ require 'noosfero/multi_tenancy' | ||
| 3 | class ApplicationController < ActionController::Base | 3 | class ApplicationController < ActionController::Base |
| 4 | protect_from_forgery | 4 | protect_from_forgery |
| 5 | 5 | ||
| 6 | - before_filter :setup_multitenancy | ||
| 7 | before_filter :detect_stuff_by_domain | 6 | before_filter :detect_stuff_by_domain |
| 8 | before_filter :init_noosfero_plugins | 7 | before_filter :init_noosfero_plugins |
| 9 | before_filter :allow_cross_domain_access | 8 | before_filter :allow_cross_domain_access |
| @@ -105,10 +104,6 @@ class ApplicationController < ActionController::Base | @@ -105,10 +104,6 @@ class ApplicationController < ActionController::Base | ||
| 105 | super || form_authenticity_token == request.headers['X-XSRF-TOKEN'] | 104 | super || form_authenticity_token == request.headers['X-XSRF-TOKEN'] |
| 106 | end | 105 | end |
| 107 | 106 | ||
| 108 | - def setup_multitenancy | ||
| 109 | - Noosfero::MultiTenancy.setup!(request.host) | ||
| 110 | - end | ||
| 111 | - | ||
| 112 | def boxes_editor? | 107 | def boxes_editor? |
| 113 | false | 108 | false |
| 114 | end | 109 | end |
config/application.rb
| @@ -15,6 +15,9 @@ module Noosfero | @@ -15,6 +15,9 @@ module Noosfero | ||
| 15 | 15 | ||
| 16 | require 'noosfero/plugin' | 16 | require 'noosfero/plugin' |
| 17 | 17 | ||
| 18 | + require 'noosfero/multi_tenancy' | ||
| 19 | + config.middleware.use Noosfero::MultiTenancy::Middleware | ||
| 20 | + | ||
| 18 | config.action_controller.include_all_helpers = false | 21 | config.action_controller.include_all_helpers = false |
| 19 | 22 | ||
| 20 | # Settings in config/environments/* take precedence over those specified here. | 23 | # Settings in config/environments/* take precedence over those specified here. |
lib/noosfero/multi_tenancy.rb
| @@ -22,6 +22,18 @@ module Noosfero | @@ -22,6 +22,18 @@ module Noosfero | ||
| 22 | end | 22 | end |
| 23 | end | 23 | end |
| 24 | 24 | ||
| 25 | + class Middleware | ||
| 26 | + def initialize(app) | ||
| 27 | + @app = app | ||
| 28 | + end | ||
| 29 | + | ||
| 30 | + def call(env) | ||
| 31 | + request = Rack::Request.new(env) | ||
| 32 | + Noosfero::MultiTenancy.setup!(request.host) | ||
| 33 | + @app.call(env) | ||
| 34 | + end | ||
| 35 | + end | ||
| 36 | + | ||
| 25 | private | 37 | private |
| 26 | 38 | ||
| 27 | def self.load_map | 39 | def self.load_map |
test/functional/application_controller_test.rb
| @@ -478,30 +478,6 @@ class ApplicationControllerTest < ActionController::TestCase | @@ -478,30 +478,6 @@ class ApplicationControllerTest < ActionController::TestCase | ||
| 478 | assert_response :forbidden | 478 | assert_response :forbidden |
| 479 | end | 479 | end |
| 480 | 480 | ||
| 481 | - if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL' | ||
| 482 | - | ||
| 483 | - should 'change postgresql schema' do | ||
| 484 | - uses_host 'schema1.com' | ||
| 485 | - Noosfero::MultiTenancy.expects(:on?).returns(true) | ||
| 486 | - Noosfero::MultiTenancy.expects(:mapping).returns({ 'schema1.com' => 'schema1' }).at_least_once | ||
| 487 | - exception = assert_raise(ActiveRecord::StatementInvalid) { get :index } | ||
| 488 | - | ||
| 489 | - # we have switched to a new database schema; depending on the PostgreSQL | ||
| 490 | - # version, we will receive either an error message because the schema | ||
| 491 | - # does not exist, or an error saying that whatever table we need can't be | ||
| 492 | - # found. | ||
| 493 | - assert_match /(SET search_path TO schema1|PG::UndefinedTable)/, exception.message | ||
| 494 | - end | ||
| 495 | - | ||
| 496 | - should 'not change postgresql schema if multitenancy is off' do | ||
| 497 | - uses_host 'schema1.com' | ||
| 498 | - Noosfero::MultiTenancy.stubs(:on?).returns(false) | ||
| 499 | - Noosfero::MultiTenancy.stubs(:mapping).returns({ 'schema1.com' => 'schema1' }) | ||
| 500 | - assert_nothing_raised(ActiveRecord::StatementInvalid) { get :index } | ||
| 501 | - end | ||
| 502 | - | ||
| 503 | - end | ||
| 504 | - | ||
| 505 | should 'register search_term occurrence on find_by_contents' do | 481 | should 'register search_term occurrence on find_by_contents' do |
| 506 | controller = ApplicationController.new | 482 | controller = ApplicationController.new |
| 507 | controller.stubs(:environment).returns(Environment.default) | 483 | controller.stubs(:environment).returns(Environment.default) |
| @@ -0,0 +1,40 @@ | @@ -0,0 +1,40 @@ | ||
| 1 | +require_relative "../test_helper" | ||
| 2 | + | ||
| 3 | +class MultiTenancyTest < ActionDispatch::IntegrationTest | ||
| 4 | + | ||
| 5 | + should 'change postgresql schema' do | ||
| 6 | + host! 'schema1.com' | ||
| 7 | + Noosfero::MultiTenancy.expects(:on?).at_least_once.returns(true) | ||
| 8 | + Noosfero::MultiTenancy.expects(:mapping).returns({ 'schema1.com' => 'schema1' }).at_least_once | ||
| 9 | + exception = assert_raise(ActiveRecord::StatementInvalid) { get '/' } | ||
| 10 | + | ||
| 11 | + # we have switched to a new database schema; depending on the PostgreSQL | ||
| 12 | + # version, we will receive either an error message because the schema | ||
| 13 | + # does not exist, or an error saying that whatever table we need can't be | ||
| 14 | + # found. | ||
| 15 | + assert_match /(SET search_path TO schema1|PG::UndefinedTable)/, exception.message | ||
| 16 | + end | ||
| 17 | + | ||
| 18 | + should 'not change postgresql schema if multitenancy is off' do | ||
| 19 | + host! 'schema1.com' | ||
| 20 | + Noosfero::MultiTenancy.stubs(:on?).returns(false) | ||
| 21 | + Noosfero::MultiTenancy.stubs(:mapping).returns({ 'schema1.com' => 'schema1' }) | ||
| 22 | + assert_nothing_raised(ActiveRecord::StatementInvalid) { get '/' } | ||
| 23 | + end | ||
| 24 | + | ||
| 25 | + should 'find session from the correct database schema' do | ||
| 26 | + Noosfero::MultiTenancy.expects(:on?).at_least_once.returns(true) | ||
| 27 | + Noosfero::MultiTenancy.expects(:mapping).returns({ 'schema2.com' => 'public', 'schema1.com' => 'schema1' }).at_least_once | ||
| 28 | + | ||
| 29 | + user = create_user | ||
| 30 | + session_obj = create(Session, user_id: user.id, session_id: 'some_id', data: {}) | ||
| 31 | + person_identifier = user.person.identifier | ||
| 32 | + | ||
| 33 | + Noosfero::MultiTenancy.setup!('schema1.com') | ||
| 34 | + host! 'schema2.com' | ||
| 35 | + cookies[:_noosfero_session] = session_obj.session_id | ||
| 36 | + assert_nothing_raised { get "/myprofile/#{person_identifier}" } | ||
| 37 | + assert_equal 'public', ActiveRecord::Base.connection.schema_search_path | ||
| 38 | + end | ||
| 39 | + | ||
| 40 | +end |