File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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?
Original file line number Diff line number Diff line change 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
66module 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments