From 0968c7aaae87a6a9590f6a9a8ea6f7e5179558a1 Mon Sep 17 00:00:00 2001 From: Le Anh Thang Date: Fri, 25 Dec 2020 10:16:31 +0700 Subject: [PATCH] Fix, Avoid checking resources multi times --- .../concerns/set_user_by_token.rb | 14 ++++++++++++-- test/controllers/demo_group_controller_test.rb | 8 -------- test/controllers/demo_mang_controller_test.rb | 4 ---- test/controllers/demo_user_controller_test.rb | 13 ------------- 4 files changed, 12 insertions(+), 27 deletions(-) diff --git a/app/controllers/devise_token_auth/concerns/set_user_by_token.rb b/app/controllers/devise_token_auth/concerns/set_user_by_token.rb index c03bca6ba..2877a87b3 100644 --- a/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +++ b/app/controllers/devise_token_auth/concerns/set_user_by_token.rb @@ -24,6 +24,9 @@ def set_request_start # user auth def set_user_by_token(mapping = nil) + # Avoid checking resources multi times + return @resource if resource_is_checked?(mapping) + # determine target authentication class rc = resource_class(mapping) @@ -76,12 +79,13 @@ def set_user_by_token(mapping = nil) else sign_in(scope, user, store: false, event: :fetch, bypass: DeviseTokenAuth.bypass_sign_in) end - return @resource = user + @resource = user else # zero all values previously set values @token.client = nil - return @resource = nil + @resource = nil end + @resource end def update_auth_header @@ -159,4 +163,10 @@ def auth_header_from_batch_request end auth_header end + + def resource_is_checked?(mapping) + return true if @resource || eval("@has_checked_#{mapping}") == true + eval("@has_checked_#{mapping} = true") + return false + end end diff --git a/test/controllers/demo_group_controller_test.rb b/test/controllers/demo_group_controller_test.rb index 65680a82e..55a9eb44a 100644 --- a/test/controllers/demo_group_controller_test.rb +++ b/test/controllers/demo_group_controller_test.rb @@ -59,10 +59,6 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest assert @controller.user_signed_in? end - it 'should not define current_mang' do - refute_equal @resource, @controller.current_mang - end - it 'should define current_member' do assert_equal @resource, @controller.current_member end @@ -109,10 +105,6 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest assert @controller.mang_signed_in? end - it 'should not define current_mang' do - refute_equal @mang, @controller.current_user - end - it 'should define current_member' do assert_equal @mang, @controller.current_member end diff --git a/test/controllers/demo_mang_controller_test.rb b/test/controllers/demo_mang_controller_test.rb index 887f510a2..7db7969cc 100644 --- a/test/controllers/demo_mang_controller_test.rb +++ b/test/controllers/demo_mang_controller_test.rb @@ -45,10 +45,6 @@ class DemoMangControllerTest < ActionDispatch::IntegrationTest assert @controller.mang_signed_in? end - it 'should not define current_user' do - refute_equal @resource, @controller.current_user - end - it 'should define render_authenticate_error' do assert @controller.methods.include?(:render_authenticate_error) end diff --git a/test/controllers/demo_user_controller_test.rb b/test/controllers/demo_user_controller_test.rb index 9f301ae99..f4f2c1b76 100644 --- a/test/controllers/demo_user_controller_test.rb +++ b/test/controllers/demo_user_controller_test.rb @@ -46,10 +46,6 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest assert @controller.user_signed_in? end - it 'should not define current_mang' do - refute_equal @resource, @controller.current_mang - end - it 'should define render_authenticate_error' do assert @controller.methods.include?(:render_authenticate_error) end @@ -539,11 +535,6 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest it 'should define user_signed_in?' do assert @controller.user_signed_in? end - - it 'should not define current_mang' do - refute_equal @resource, @controller.current_mang - end - end it 'should return success status' do @@ -590,10 +581,6 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest it 'should define user_signed_in?' do assert @controller.user_signed_in? end - - it 'should not define current_mang' do - refute_equal @resource, @controller.current_mang - end end it 'should return success status' do