From 30f2ee174db42d80227370f1231a922fb49cb6e9 Mon Sep 17 00:00:00 2001 From: NikosVlagoidis Date: Fri, 25 Oct 2024 12:39:52 +0300 Subject: [PATCH 1/2] Thread safety in with_accessible_by_strategy block --- lib/cancan/config.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/cancan/config.rb b/lib/cancan/config.rb index 0fd8893f..f192cf44 100644 --- a/lib/cancan/config.rb +++ b/lib/cancan/config.rb @@ -48,9 +48,7 @@ def self.with_rules_compressor_enabled(value) # `distinct` is not reliable in some cases. See # https://github.com/CanCanCommunity/cancancan/pull/605 def self.accessible_by_strategy - return @accessible_by_strategy if @accessible_by_strategy - - @accessible_by_strategy = default_accessible_by_strategy + Thread.current[:can_can_accessible_by_strategy] ||= default_accessible_by_strategy end def self.default_accessible_by_strategy @@ -71,20 +69,18 @@ def self.accessible_by_strategy=(value) raise ArgumentError, 'accessible_by_strategy = :subquery requires ActiveRecord 5 or newer' end - @accessible_by_strategy = value + Thread.current[:can_can_accessible_by_strategy] = value end def self.with_accessible_by_strategy(value) return yield if value == accessible_by_strategy - validate_accessible_by_strategy!(value) - begin strategy_was = accessible_by_strategy - @accessible_by_strategy = value + self.accessible_by_strategy = value yield ensure - @accessible_by_strategy = strategy_was + self.accessible_by_strategy = strategy_was end end From fa2c52c24ff15b6be56d984cae752d5a151858a4 Mon Sep 17 00:00:00 2001 From: NikosVlagoidis Date: Fri, 25 Oct 2024 12:46:12 +0300 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc34c198..df3a2563 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 3.6.0 +* [#871](https://github.com/CanCanCommunity/cancancan/pull/871): Make `with_accessible_by_strategy` thread safe ([@NikosVlagoidis][]) * [#849](https://github.com/CanCanCommunity/cancancan/pull/849): Update tests matrix. ([@coorasse][]) * [#843](https://github.com/CanCanCommunity/cancancan/pull/843): Compress duplicate rules. ([@MrChoclate][]) * [#841](https://github.com/CanCanCommunity/cancancan/pull/841): New https://cancancan.dev website. ([@pandermatt][]) @@ -716,3 +717,4 @@ Please read the [guide on migrating from CanCanCan 2.x to 3.0](https://github.co [@MrChoclate]: https://github.com/MrChoclate [@pandermatt]: https://github.com/pandermatt [@kalsan]: https://github.com/kalsan +[@NikosVlagoidis]: https://github.com/NikosVlagoidis