multi-tenant-support 1.0.5 → 1.1.0
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 +4 -4
- data/README.md +28 -10
- data/lib/multi_tenant_support/concern/controller_concern.rb +8 -3
- data/lib/multi_tenant_support/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7761ff1721fab8904557ae7af54aca1875d19fb6076bbe5038493c945009eec
|
4
|
+
data.tar.gz: 43df41f9e15014829828918d4703e77fe6f321bad2f10495e103860abe57cbbf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7103aec88b69d628baed7e783495cc5e06b8fe49f5751d40aa5e676f2586aa7a0fcfe4d4f6015bfa4409017e533a3fff3a71b945d5426feb2ba5d7ba177f1ede
|
7
|
+
data.tar.gz: f2e5c63298eee511690622da0d0c47bf0fcdeb80aff543704da2a2a7a047165d5d6eebfda3d44862003329fcc739a3d8278d3ffb7616667087d540c7f9e49006
|
data/README.md
CHANGED
@@ -11,7 +11,7 @@ Keep your data secure with multi-tenant-support. Prevent most ActiveRecord CRUD
|
|
11
11
|
- Prevent most ActiveRecord CRUD methods from acting across tenants.
|
12
12
|
- Support Row-level Multitenancy
|
13
13
|
- Build on ActiveSupport::CurrentAttributes offered by rails
|
14
|
-
- Auto set current tenant through subdomain and domain in controller
|
14
|
+
- Auto set current tenant through subdomain and domain in controller (overrideable)
|
15
15
|
- Support ActiveJob and Sidekiq
|
16
16
|
|
17
17
|
|
@@ -219,7 +219,7 @@ Below are the behaviour of all ActiveRecord CRUD methods under abvove scenarios:
|
|
219
219
|
|
220
220
|
## Usage
|
221
221
|
|
222
|
-
|
222
|
+
### Get current
|
223
223
|
|
224
224
|
Get current tenant through:
|
225
225
|
|
@@ -227,7 +227,7 @@ Get current tenant through:
|
|
227
227
|
MultiTenantSupport.current_tenant
|
228
228
|
```
|
229
229
|
|
230
|
-
|
230
|
+
### Switch tenant
|
231
231
|
|
232
232
|
You can switch to another tenant temporary through:
|
233
233
|
|
@@ -237,7 +237,7 @@ MultiTenantSupport.under_tenant amazon do
|
|
237
237
|
end
|
238
238
|
```
|
239
239
|
|
240
|
-
|
240
|
+
### Disallow read across tenant by default
|
241
241
|
|
242
242
|
This gem disallow read across tenant by default. You can check current state through:
|
243
243
|
|
@@ -245,7 +245,7 @@ This gem disallow read across tenant by default. You can check current state thr
|
|
245
245
|
MultiTenantSupport.disallow_read_across_tenant?
|
246
246
|
```
|
247
247
|
|
248
|
-
|
248
|
+
### Allow read across tenant for super admin
|
249
249
|
|
250
250
|
You can turn on the permission to read records across tenant through:
|
251
251
|
|
@@ -260,7 +260,7 @@ end
|
|
260
260
|
|
261
261
|
You can put it in a before action in SuperAdmin's controllers
|
262
262
|
|
263
|
-
|
263
|
+
### Disallow modify records tenant
|
264
264
|
|
265
265
|
This gem disallow modify record across tenant no matter you are super admin or not.
|
266
266
|
|
@@ -274,23 +274,41 @@ If `MultiTenantSupport.current_tenant` exist, you can only modify those records
|
|
274
274
|
|
275
275
|
If `MultiTenantSupport.current_tenant` is missing, you cannot modify or create any tenanted records.
|
276
276
|
|
277
|
-
|
277
|
+
### Set current tenant acccount in controller by default
|
278
278
|
|
279
279
|
This gem has set a before action `set_current_tenant_account` on ActionController. It search tenant by subdomain or domain. Do remember to `skip_before_action :set_current_tenant_account` in super admin controllers.
|
280
280
|
|
281
281
|
Feel free to override it, if the finder behaviour is not what you want.
|
282
282
|
|
283
|
-
|
283
|
+
### Override current tenant finder method if domain/subdomain is not the way you want
|
284
|
+
|
285
|
+
You can override `find_current_tenant_account` in any controller with your own tenant finding strategy. Just make sure this method return the tenat account record or nil.
|
286
|
+
|
287
|
+
For example, say you only want to find tenant with domain not subdomain. It's very simple:
|
288
|
+
|
289
|
+
```ruby
|
290
|
+
class ApplicationController < ActionController::Base
|
291
|
+
private
|
292
|
+
|
293
|
+
def find_current_tenant_account
|
294
|
+
Account.find_by(domain: request.domain)
|
295
|
+
end
|
296
|
+
end
|
297
|
+
```
|
298
|
+
|
299
|
+
Then your tenant finding strategy has changed from domain/subdomain to domain only.
|
300
|
+
|
301
|
+
### upsert_all
|
284
302
|
|
285
303
|
Currently, we don't have a good way to protect this method. So please use `upser_all` carefully.
|
286
304
|
|
287
|
-
|
305
|
+
### Unscoped
|
288
306
|
|
289
307
|
This gem has override `unscoped` to prevent the default tenant scope be scoped out. But if you really want to scope out the default tenant scope, you can use `unscope_tenant`.
|
290
308
|
|
291
309
|
## Code Example
|
292
310
|
|
293
|
-
|
311
|
+
### Database Schema
|
294
312
|
|
295
313
|
```ruby
|
296
314
|
create_table "accounts", force: :cascade do |t|
|
@@ -15,12 +15,17 @@ module MultiTenantSupport
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def set_current_tenant_account
|
18
|
-
tenant_account =
|
18
|
+
tenant_account = find_current_tenant_account
|
19
|
+
MultiTenantSupport::Current.tenant_account = tenant_account
|
20
|
+
instance_variable_set("@#{MultiTenantSupport.current_tenant_account_method}", tenant_account)
|
21
|
+
end
|
22
|
+
|
23
|
+
# A user can override this method, if he need a customize way
|
24
|
+
def find_current_tenant_account
|
25
|
+
MultiTenantSupport::FindTenantAccount.call(
|
19
26
|
subdomains: request.subdomains,
|
20
27
|
domain: request.domain
|
21
28
|
)
|
22
|
-
MultiTenantSupport::Current.tenant_account = tenant_account
|
23
|
-
instance_variable_set("@#{MultiTenantSupport.current_tenant_account_method}", tenant_account)
|
24
29
|
end
|
25
30
|
end
|
26
31
|
end
|