rls_multi_tenant 0.2.7 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 50eff8f3ced091cac7b277aff79cfd435a0ce0701ee2ca3ca7163d57e6792406
4
- data.tar.gz: 7739392cf8b866e5d6e86ccebeeccb3a3e913b7efbccf89a4441d79dfbc93f84
3
+ metadata.gz: 1ac751dd2c642fe57321ed7af2122805db1654353d933b12aba216cad22c0160
4
+ data.tar.gz: 947a7c120d823380bf29cb80881c64739bf662e284021805576fbf0baa733138
5
5
  SHA512:
6
- metadata.gz: a8c6c0d9323251de3b6bc8321cb8bae781ae8248ea37dd9cf643aa7e4e0441234c42084d7391dcd73d4735e20080c27025969359e5dfe1c9f0bea0d30e5aa34d
7
- data.tar.gz: 7788deda9e7deb6c49e7fd9359cfb06e648044c48d78ac9e1823b3cf91eb0deff90db6d95e94f2433adc726b0d6fd924b822958cab0c7882da0667d7af606def
6
+ metadata.gz: 27c78853768c5fd7e978184455ff9ba8b60401314d752e8b7140b775bf2b897820dcebca6eb7efc9467317b08de8adabbde8e49a36c0edcf3e09e775520871f9
7
+ data.tar.gz: ac3e1092d6a177323f08fcf7e2fb371bd90502af21afba051fb0c90917edc4497fabc950325035614acd8918a41f3076cd6d1e9e02132a599fb03d4dc8854806
@@ -8,19 +8,28 @@ module RlsMultiTenant
8
8
  SET_TENANT_ID_SQL = 'SET %s = %s'
9
9
  RESET_TENANT_ID_SQL = 'RESET %s'
10
10
 
11
+ # rubocop:disable Metrics/BlockLength
11
12
  class_methods do
12
13
  def tenant_session_var
13
14
  "rls.#{RlsMultiTenant.tenant_id_column}"
14
15
  end
15
16
 
17
+ def tenant_stack
18
+ Thread.current[:"#{name}_tenant_stack"] ||= []
19
+ end
20
+
16
21
  # Switch tenant context for a block
17
22
  def switch(tenant_or_id)
18
23
  tenant_id = extract_tenant_id(tenant_or_id)
19
24
  validate_tenant_exists!(tenant_id)
25
+
26
+ previous_tenant_id = current_tenant_id
27
+ tenant_stack.push(previous_tenant_id)
28
+
20
29
  connection.execute format(SET_TENANT_ID_SQL, tenant_session_var, connection.quote(tenant_id))
21
30
  yield
22
31
  ensure
23
- reset!
32
+ restore_tenant_context!
24
33
  end
25
34
 
26
35
  # Switch tenant context permanently (until reset)
@@ -32,6 +41,7 @@ module RlsMultiTenant
32
41
 
33
42
  # Reset tenant context
34
43
  def reset!
44
+ tenant_stack.clear
35
45
  connection.execute format(RESET_TENANT_ID_SQL, tenant_session_var)
36
46
  end
37
47
 
@@ -51,6 +61,25 @@ module RlsMultiTenant
51
61
 
52
62
  private
53
63
 
64
+ def current_tenant_id
65
+ return nil unless connection.active?
66
+
67
+ result = connection.execute("SHOW #{tenant_session_var}")
68
+ result.first&.dig(tenant_session_var)
69
+ rescue ActiveRecord::StatementInvalid, PG::Error
70
+ nil
71
+ end
72
+
73
+ def restore_tenant_context!
74
+ previous_tenant_id = tenant_stack.pop
75
+
76
+ if previous_tenant_id.present?
77
+ connection.execute format(SET_TENANT_ID_SQL, tenant_session_var, connection.quote(previous_tenant_id))
78
+ else
79
+ connection.execute format(RESET_TENANT_ID_SQL, tenant_session_var)
80
+ end
81
+ end
82
+
54
83
  def extract_tenant_id(tenant_or_id)
55
84
  case tenant_or_id
56
85
  when ->(obj) { obj.is_a?(RlsMultiTenant.tenant_class) }
@@ -71,6 +100,7 @@ module RlsMultiTenant
71
100
  raise StandardError, "#{RlsMultiTenant.tenant_class_name} with id '#{tenant_id}' not found"
72
101
  end
73
102
  end
103
+ # rubocop:enable Metrics/BlockLength
74
104
 
75
105
  # Instance methods
76
106
  def switch(tenant_or_id, &block)
@@ -104,7 +104,7 @@ module RlsMultiTenant
104
104
 
105
105
  def excluded_subdomain?(subdomain)
106
106
  return false if subdomain.blank?
107
-
107
+
108
108
  excluded = RlsMultiTenant.excluded_subdomains
109
109
  excluded.is_a?(Array) && excluded.include?(subdomain.to_s.downcase)
110
110
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RlsMultiTenant
4
- VERSION = '0.2.7'
4
+ VERSION = '0.2.8'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rls_multi_tenant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Coding Ways