Skip to content

Commit 837d8d1

Browse files
committed
fix: use current attributes instead of thread
1 parent 26fc469 commit 837d8d1

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

app/controllers/concerns/authenticatable.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ def authenticate_request!
2929
@current_user = User.unscoped.find(@jwt_payload[:user_id])
3030
@current_organization = @current_user.organization
3131

32-
# Set thread-local variables for OrganizationScoped models
33-
# This is needed early for update_last_login! and will be maintained by set_organization_context
34-
Thread.current[:current_organization_id] = @current_organization.id
35-
Thread.current[:current_user_id] = @current_user.id
36-
Thread.current[:current_user_role] = @current_user.role
32+
# Set request-scoped attributes for OrganizationScoped models (thread-safe)
33+
Current.organization_id = @current_organization.id
34+
Current.user_id = @current_user.id
35+
Current.user_role = @current_user.role
3736

3837
# Update last login time (uses update_column which skips callbacks/audit logs)
3938
@current_user.update_last_login! if should_update_last_login?

app/models/concerns/organization_scoped.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
# Concern para aplicar scoping automático de organização
44
# Como o RLS do PostgreSQL não funciona com o usuário owner (postgres),
5-
# implementamos o scoping no nível da aplicação Rails
5+
# implementamos o scoping no nível da aplicação Rails usando CurrentAttributes
66
module OrganizationScoped
77
extend ActiveSupport::Concern
88

99
included do
1010
# Aplicar default_scope apenas se houver uma organização no contexto
1111
default_scope lambda {
12-
if Thread.current[:current_organization_id].present?
13-
where(organization_id: Thread.current[:current_organization_id])
12+
if Current.organization_id.present?
13+
where(organization_id: Current.organization_id)
1414
else
1515
all
1616
end

app/models/current.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
# Thread-safe storage for request-scoped data
4+
# Use Current.organization_id instead of Thread.current[:organization_id]
5+
class Current < ActiveSupport::CurrentAttributes
6+
attribute :organization_id, :user_id, :user_role
7+
end

0 commit comments

Comments
 (0)