Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread safety in with_accessible_by_strategy block #871

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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][])
Expand Down Expand Up @@ -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
12 changes: 4 additions & 8 deletions lib/cancan/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down