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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1ac751dd2c642fe57321ed7af2122805db1654353d933b12aba216cad22c0160
|
|
4
|
+
data.tar.gz: 947a7c120d823380bf29cb80881c64739bf662e284021805576fbf0baa733138
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
|
|
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)
|